🐳

docker-composeでTerraformをローカル実行する

2021/07/22に公開

概要

Terraform をローカルにインストールするのでもいいけど docker-compose にしておくこともやっときたいと思ってやってみた備忘録です.
ググったらいろんな情報がありましたがこちらを参考にさせていただき環境作りしました.

Mac はこんな環境

sw_vers 
ProductName:    Mac OS X
ProductVersion: 10.15.7
BuildVersion:   19H1217

ディレクトリ構成

下みたいな感じです.

.
├── README.md
├── docker-compose.yml
├── terraform
│   ├── aws.tf
│   ├── variable.tf
│   └── vpc.tf

docker-compose の中身

中身はこんな感じ

version: '3'

services: 
    terraform:
        image: hashicorp/terraform:1.0.3
        container_name: terraform
        volumes:
            - ./terraform:/terraform
        env_file: .env
        working_dir: /terraform

vpc.tfにサブネット追加をし、試しにterraform planを実行してみたらいい感じになりました.

docker-compose run --rm terraform plan
~中略~

Terraform used the selected providers to generate the
following execution plan. Resource actions are indicated
with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_subnet.tf_private_subnet_1c will be created
  + resource "aws_subnet" "tf_private_subnet_1c" {
      + arn                             = (known after apply)
      + assign_ipv6_address_on_creation = false
      + availability_zone               = "ap-northeast-1c"
      + availability_zone_id            = (known after apply)
      + cidr_block                      = "10.10.4.0/24"
      + id                              = (known after apply)
      + ipv6_cidr_block_association_id  = (known after apply)
      + map_public_ip_on_launch         = false
      + owner_id                        = (known after apply)
      + tags                            = {
          + "Name"  = "tf-private-subnet-1c"
          + "Owner" = "tada"
        }
      + tags_all                        = {
          + "Name"  = "tf-private-subnet-1c"
          + "Owner" = "tada"
        }
      + vpc_id                          = "vpc-xxxx"
    }

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

──────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so
Terraform can't guarantee to take exactly these actions if
you run "terraform apply" now.

Discussion