Help us understand the problem. What is going on with this article?

aws-sdk-coreでcredentialsのプロファイルを切り替える

More than 5 years have passed since last update.

credentialsファイル

Linux/Macでは~/.aws/credentialsに置く。
プロファイルごとにアクセスキーID/シークレットアクセスキーを設定できる。
デフォルトのプロファイルはdefaultになる。

~/.aws/credentials
[default]
aws_access_key_id = DEFAULT_ACCESS_KEY_ID
aws_secret_access_key = DEFAULT_SECRET_ACCESS_KEY
aws_session_token = DEFAULT_SESSION_TOKEN

[dev]
aws_access_key_id = DEV_ACCESS_KEY_ID
aws_secret_access_key = DEV_SECRET_ACCESS_KEY

オブジェクトごとに切り替える

~/.aws/credentialsに設定ファイルがあるなら:profileにプロファイル名を指定するだけ。

require 'aws-sdk-core'

ec2 = Aws::EC2::Client.new region: 'ap-northeast-1', profile: 'dev'

別のパスを指定するならAws::SharedCredentialsを作成して渡す。

require 'aws-sdk-core'

creds = Aws::SharedCredentials.new path: '/path/to/creds', profile_name: 'dev'
ec2 = Aws::EC2::Client.new region: 'ap-northeast-1', credentials: creds

まとめて切り替える

~/.aws/credentialsにファイルがある場合。

require 'aws-sdk-core'

Aws.config[:profile] = 'dev'
Aws.config[:region] = 'ap-northeast-1'
ec2 = Aws::EC2::Client.new

別のパスを指定する場合。

require 'aws-sdk-core'

creds = Aws::SharedCredentials.new path: '/path/to/creds', profile_name: 'dev'
Aws.config[:credentials] = creds
Aws.config[:region] = 'ap-northeast-1'
ec2 = Aws::EC2::Client.new
ryo0301
lifull
日本最大級の不動産・住宅情報サイト「LIFULL HOME'S」を始め、人々の生活に寄り添う様々な情報サービス事業を展開しています。
https://lifull.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした