0
0

More than 1 year has passed since last update.

EC2上のterraformからawsのprofile指定して使いたい

Last updated at Posted at 2020-05-12

EC2上でterraformを使う場合--backend-config="profile=プロファイル名"を指定しても、
インスタンスにアタッチされているIAMロールが優先されてしまいました。

$ TF_LOG=DEBUG terraform init --backend-config="profile=terraform_profile"

# - 省略 -

[DEBUG] [aws-sdk-go] <GetCallerIdentityResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
  <GetCallerIdentityResult>
    <Arn>arn:aws:sts::xxxxxxxxxx:assumed-role/ec2-sample-instance-role/i-0bb01f7be2c61dc01</Arn>
    <UserId> …

access_key等をvarで渡したり、.tfvarsを使う方法もありますが、
既にprofileが作成されている場合はAWS_PROFILEが使えます。

$ aws configure list --profile terraform_profile
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                  terraform_profile           manual    --profile
access_key     ****************UOJ5 shared-credentials-file    
secret_key     ****************eNBm shared-credentials-file    
    region           ap-northeast-1      config-file    ~/.aws/config

# これから作成する場合は--profileオプションで指定
$ aws configure --profile terraform_profile
AWS Access Key ID [None]: xxxxxxxxxxxxx
AWS Secret Access Key [None]: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Default region name [None]: ap-northeast-1
Default output format [None]:

terraform init実行(version 0.12で確認)

$ AWS_PROFILE=terraform_profile terraform init

こんな感じでうまくいきました。

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