インドカレーファンクラブ

パソコン、カメラ

【Terraform】【AWS】【RemoteDevelopment】Terraform+AWSな環境をDevContainerでつくった

あらすじ

github.com

Terraformを触ったことがなかったので環境をつくりたかった
当然ローカルを汚さないよう...このRemoteDevelopmentでな!

Githubに載せてるので見れば概ねわかると思いつつも簡単な説明や補足とか

参考文献はReadMeの方もみてね

概要

ベースとするDockerImageはAWS CLI(なんでかというと後述)

その上にTerraformをインストール

AWSの認証情報はDevContainerのホスト側の環境変数に設定(or .devcontainerにベタ書き)しておいてコンテナに伝播させる

というのもファイル見たほうがはやい

作るときのメモ

ベースとするイメージについて

本当はterraformのイメージをベースにして、その上にAWS CLIを載せたかった(AzureとかGCPに切り替えやすそうだし)

しかし...

terraformのイメージにはlight(latest)とfullがあって、後者でないとコンテナにログインできないみたい?
(少なくとも僕は/bin/bashがないと怒られた)

そしてfullのイメージはサイズ大きいしバージョン指定できなそうだった(常にlatestのfullっぽい)
バージョン固定したいので断念

参考:https://qiita.com/cyack2002/items/15216503178f5601e578

ビルド引数への環境変数の伝播について

そこそこ苦労した内容を↓にまとめた

omdwn.hatenablog.com

でも.envの方がよくない?って思い始めたのでいつか変えそう

terraform使ったことなくて拡張機能とか色々あると思うので、そのへん追って追加したい

実行イメージ

記事の内容がスカスカだし折角だからterraformを実行したときの出力でも貼っておこうかな!

bash-4.2# terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v3.36.0...
- Installed hashicorp/aws v3.36.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
bash-4.2# terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_s3_bucket.HogeSampleImage will be created
  + resource "aws_s3_bucket" "HogeSampleImage" {
      + acceleration_status         = (known after apply)
      + acl                         = "private"
      + arn                         = (known after apply)
      + bucket                      = "hoge-sample-images"
      + bucket_domain_name          = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = false
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + region                      = (known after apply)
      + request_payer               = (known after apply)
      + website_domain              = (known after apply)
      + website_endpoint            = (known after apply)

      + versioning {
          + enabled    = (known after apply)
          + mfa_delete = (known after apply)
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_s3_bucket.HogeSampleImage: Creating...
aws_s3_bucket.HogeSampleImage: Creation complete after 3s [id=hoge-sample-images]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.