LoginSignup
2
1

More than 3 years have passed since last update.

docker terraform実行環境を作って、仲間のterraform実行環境を尊重する

Last updated at Posted at 2020-06-21

紹介する内容

  • terraformの環境別HOMEパス/.aws/credentialsからaws profileを取得する設定を利用します

結論

  • 同じaws profile名を設定するルールだけ守ると、MACでterraformインストールした人、dockerでterraformインストールした人はお互いの環境を気にせずにterraform運用できると思います
  • dockerコンテナの中で生成したaws profileの情報をdockerのvolumesでマウントしました。
    • マウントしたディレクトリを消さない限りaws profileをずっと使用できます
    • gitignoreしたから、aws profile情報はgit pushされません

紹介始めます

ディレクトリ構成

全体ソースコードは https://github.com/cheekykorkind/qiita-example/tree/master/terraform/aws-account-management で確認できます

  • 全体図
    allD.png

  • aws profileの情報をマウントするディレクトリ
    dockerコンテナの中で生成したaws profileの情報をマウントするところです。
    awsprofileD.png

  • terraformファイルがあるディレクトリ
    aws profile名my-demoを参照して、cloudwatch logs groupを一つ作る内容が書かれてます
    terraformD.png

試し順番です

  1. docker composeがあるデレクトリー移動に移動します
    • cd qitta-example/terraform/aws-account-management
  2. dockerコンテナをバックグラウンドで起動します
    • DOCKER_UID=$(id -u $USER) DOCKER_GID=$(id -g $USER) docker-compose up -d
  3. dockerコンテナに入ります
    • docker exec -it aws-account-management /bin/sh
  4. aws profile設定します

    • 一回で十分です、HOSTマシンに影響与えません
    • profile名は my-demoにしました。terraformで参照してます
    • aws configure --profile my-demo の実行前のイメージ beforeAfterSetAwsProfile.png
    • aws configure --profile my-demo の実行中のイメージ setAwsProfile.PNG
    • aws configure --profile my-demo の実行後のイメージ afterSetAwsProfile.png
  5. terraformデレクトリーに移動します

    • cd ~/terraform/demo
  6. terraform initします

    • terraform init
  7. terraformを実行します

    • terraform apply
  8. terraformのprofile参照名を変えてエラーになるか確認します

    • main.tfを一時的に修正します
    • terraform apply

awsprofile.gif

2
1
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
2
1