35
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-11-12

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
35
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?