[macOS] Terraformのインストール
TerraformをmacOS 12.1にインストールする手順を備忘録として書き留めておく。
環境
- HW: MacBook Pro(14"/2021)
- CPU: Apple M1 Max
- MEM: 32GB
- OS: macOS Monterey 12.1
Terraformのインストール
Download Terraformにしたがって、Homebrewのtapを登録してインストールする。
$ brew tap hashicorp/tap $ brew install hashicorp/tap/terraform $ hash -r $ terraform -v Terraform v1.1.2 on darwin_arm64
動作確認
Quick start tutorialにしたがって動作確認を実施。
Docker Desktopの起動と作業ディレクトリの作成:
$ open -a Docker $ mkdir test && cd test
テスト用のマニフェストファイル(main.tf)の作成:
terraform { required_providers { docker = { source = "kreuzwerker/docker" version = "~> 2.15.0" } } } provider "docker" {} resource "docker_image" "nginx" { name = "nginx:latest" keep_locally = false } resource "docker_container" "nginx" { image = docker_image.nginx.latest name = "tutorial" ports { internal = 80 external = 8000 } }
main.tfでTerraformのInitialize:
$ terraform init
terraform init
を実行すると、Terraformのワーキングディレクトリが作成される:
$ tree -a . . ├── .terraform │ └── providers │ └── registry.terraform.io │ └── kreuzwerker │ └── docker │ └── 2.15.0 │ └── darwin_arm64 │ ├── CHANGELOG.md │ ├── LICENSE │ ├── README.md │ └── terraform-provider-docker_v2.15.0 ├── .terraform.lock.hcl └── main.tf
Docker Desktopを起動してmain.tfをApplyすると、テスト用のNginxコンテナが起動する:
$ open -a Docker $ terraform apply Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create ... 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
Nginxコンテナが起動していることを確認:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 74241ca394eb eeb9db34b331 "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:8000->80/tcp tutorial $ curl -o /dev/null -s -w '%{http_code}\n' https://www.google.co.jp 200
テスト環境の削除
main.tfを配置したディレクトリでterraform destroy
コマンドで環境を削除:
$ terraform destroy docker_image.nginx: Refreshing state... [id=sha256:eeb9db34b33190cac170b27d857e9b23511d396a2609c1a54e93025634afed0anginx:latest] docker_container.nginx: Refreshing state... [id=74241ca394eb095063b2cd2565725b0e72108cc4c6c0f5985b507506a3fc12ed] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy ... Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes ...