【Terraform】Terraform ~ 入門編 ~

■ はじめに

Terraform (テラフォーム)について、学ぶ。

目次

【1】Terraform
 1)使用言語
 2)サポートOS
【2】Core Terraform Workflow
 1)Write
 2)Plan
 3)Apply
【3】主なTerraformコマンド一覧
【4】その他の便利コマンド一覧
 1)terraform state
 2)terraform show
 3)terraform refresh
 4)terraform workspace
 5)terraform fmt
 6)terraform graph
 7)terraform import

【1】Terraform

* Vagrant を開発しているHashiCorp社が提供する
 環境構築するためのIaC(Infrastructure as Code)ツール
 => 独自の設定ファイル (拡張子 .tf) から IaC できる

1)使用言語

* HCL(HashiCorp Configuration Language)という
 HashiCorp社製品で使われている独自の言語で記述

2)サポートOS

* 主要なOSは全て網羅
 + Windows
 + Mac
 + Linux

https://developer.hashicorp.com/terraform/downloads

【2】Core Terraform Workflow

* 以下の公式ドキュメントより
 => こういうシンプルなコンセプトは、非常にいいと思う

https://developer.hashicorp.com/terraform/intro/core-workflow

1)Write

* まずは、環境設定をTerraformコードとして書く。

2)Plan

* 「terraform plan」などで、デプロイ前に差分を見て、計画する

3)Apply

* Terraformコードを適用して、デプロイする

【3】主なTerraformコマンド一覧

Command Explanation Memo
terraform init 初期化処理
terraform plan 変更内容を確認する
terraform apply Terraformコードを実行する
terraform destroy Terraformコードで作成したリソースを削除 実行前に「terraform plan -destroy」がよさげ

terraform destroy
https://developer.hashicorp.com/terraform/cli/commands/destroy

【4】その他の便利コマンド一覧

Command Explanation Memo
terraform state stateに関するコマンド
terraform show 管理しているリソースを表示
terraform refresh stateと実際のInfra状況を同期するようなコマンド
terraform workspace workspaceに関するコマンド
terraform fmt HCLファイルをフォーマット terraform fmt -recursive
terraform graph Terraformの依存関係をグラフ化 dot -Tpng > graph.png
terraform import 既存のインフラストラクチャをTerraformの管理下に置くためのコマンド terraform import [tfファイルのtypeとName] [対象リソース]

1)terraform state

Command Explanation Memo
terraform state list 使用しているリソースを一覧表示
terraform state mv State内のリソース名を変更
terraform state rm . リソースをTerraformの管理から外す ★お勧め★
terraform state pull 現在のStateをBackendからダウンロードし、表示
terraform state push ローカルのStateファイルをリモートのBackendにアップロードする
terraform state show リソースの詳細を表示 terraform showコマンドのリソース指定版

terraform state list
https://developer.hashicorp.com/terraform/cli/commands/state/list

terraform state rm
https://developer.hashicorp.com/terraform/cli/commands/state/rm

* Stateファイルで管理されているリソースを削除(除外)する

https://zenn.dev/toritori0318/articles/983bd73e2d2158

にあるように、不整合が起きて、
terrraform refresh (後述「3)terrraform refresh」)しても
状況が変わらない時に、使用したら解決できた。

2)terraform show

https://developer.hashicorp.com/terraform/cli/commands/show

* stateファイルを元に現在のリソースの状態を参照するコマンド
 => あくまで、現在の状態。terraform plan後の表示じゃない。

3)terrraform refresh

https://developer.hashicorp.com/terraform/cli/commands/refresh

* stateと実際のInfra状況を同期するようなコマンド

4)terraform workspace

https://developer.hashicorp.com/terraform/language/state/workspaces
そもそも「Workspace」とは?

* 同一の tfファイル群を別の tfsate として扱うことができる機能

* 使用例については、以下の関連記事を参照のこと

https://dk521123.hatenablog.com/entry/2023/05/06/003645

Command Explanation Memo
terraform workspace list 既存のworkspaceを一覧表示
terraform workspace new Workspaceの新規作成 terraform workspace new prod
terraform workspace select Workspaceの選択 terraform workspace select prod
terraform workspace delete Workspaceの削除 terraform workspace delete prod
terraform workspace show 選択中のWorkspaceを表示

5)terraform fmt

* TFコードを整形してくれる(インデントの統一など)
 => 改行とかはそのまんまだった

https://developer.hashicorp.com/terraform/cli/commands/fmt

terraform fmt コマンド実行前

data "external" "hello_world" {
  program =         ["python", "hello_world.py"]

  query = {
               name = "Mike"
  }

}

output "result" {
                    value   =   data.external.hello_world.result["result"]
}

terraform fmt コマンド実行後

data "external" "hello_world" {
  program = ["python", "hello_world.py"]

  query = {
    name = "Mike"
  }

}

output "result" {
  value = data.external.hello_world.result["result"]
}

6)terraform graph

* terraform graphで、Terraformの依存関係をグラフ化することが可能

https://developer.hashicorp.com/terraform/cli/commands/graph

terraform graph | dot -Tsvg > graph.svg

# PNGだと、システムがでかくなると画像が荒くて文字が見れない可能性あり
terraform graph | dot -Tpng > graph.png

# それ以外の対応画像形式は、以下を参照のこと。

https://www.graphviz.org/docs/outputs/

補足:GraphViz

* ただし、GraphViz が必要。
* DOT言語/GraphViz に関する詳細は、以下の関連記事を参照のこと

Python + DOT言語で図作成するには
https://dk521123.hatenablog.com/entry/2023/06/14/174104
DOT言語 ~ 環境設定編 ~
https://dk521123.hatenablog.com/entry/2023/07/18/120407

7)terraform import

# terraform import [tfファイルのtypeとName] [対象リソース]
terraform import aws_s3_bucket.sample_bucket sample-bucket

https://go-journey.club/archives/17121
https://tech.layerx.co.jp/entry/improve-iac-development-with-terraform-import

参考文献

https://minegishirei.hatenablog.com/entry/2023/01/27/120400

関連記事

Terraform ~ 環境構築編 ~
https://dk521123.hatenablog.com/entry/2023/04/05/000224
Terraform ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2023/05/03/000000
Terraform ~ 基本編 / 変数 ~
https://dk521123.hatenablog.com/entry/2023/12/24/173633
Terraform ~ 基本編 / Data Sources ~
https://dk521123.hatenablog.com/entry/2024/01/16/191006
Terraform ~ 基本編 / Module ~
https://dk521123.hatenablog.com/entry/2023/05/19/113544
Terraform ~ 基本編 / tfstateファイル ~
https://dk521123.hatenablog.com/entry/2023/05/05/004939
Terraform ~ テンプレート ~
https://dk521123.hatenablog.com/entry/2023/05/22/101325
Terraform ~ Terraformあれこれ ~
https://dk521123.hatenablog.com/entry/2023/05/15/205352
Terraform ~ AWS S3 ~
https://dk521123.hatenablog.com/entry/2023/04/09/104204
Terraform ~ AWS IAM ~
https://dk521123.hatenablog.com/entry/2023/04/12/214311
Terraform ~ AWS Glue ~
https://dk521123.hatenablog.com/entry/2023/04/08/220411
Terraform ~ AWS KMS ~
https://dk521123.hatenablog.com/entry/2023/05/26/000000
Terraform ~ AWS Secrets Manager ~
https://dk521123.hatenablog.com/entry/2023/04/11/152801
Terraform ~ AWS CloudWatch ~
https://dk521123.hatenablog.com/entry/2023/05/17/123335
Terraform ~ AWS EC2 ~
https://dk521123.hatenablog.com/entry/2023/05/21/003048
Terraform ~ AWS MWAA ~
https://dk521123.hatenablog.com/entry/2023/12/25/000152
Terraform ~ AWS ECR ~
https://dk521123.hatenablog.com/entry/2023/05/23/002314
Terraform ~ AWS CodeArtifact ~
https://dk521123.hatenablog.com/entry/2024/01/26/232109
Terraform ~ AWS MSK ~
https://dk521123.hatenablog.com/entry/2023/05/14/122215
Terraform ~ AWS MSK Connect ~
https://dk521123.hatenablog.com/entry/2023/05/25/000000
AWS MSK Connect 内の 接続情報を設定を考える
https://dk521123.hatenablog.com/entry/2023/06/04/230737
Terraform ~ tag あれこれ ~
https://dk521123.hatenablog.com/entry/2023/06/05/224944
Terraform ~ Docker ~
https://dk521123.hatenablog.com/entry/2023/04/10/193239
Terraform ~ Datadog ~
https://dk521123.hatenablog.com/entry/2023/05/12/000000
Terraform ~ 特定リソースのみデプロイ・削除する ~
https://dk521123.hatenablog.com/entry/2023/04/20/172823
Terraform ~ 複数環境へデプロイすることを考える ~
https://dk521123.hatenablog.com/entry/2023/05/06/003645
Terraform ~ 機密情報の扱いを考える ~
https://dk521123.hatenablog.com/entry/2023/05/18/005103
Terraform ~ tfenv / バージョン管理 ~
https://dk521123.hatenablog.com/entry/2023/01/14/000000
Terraform ~ direnv / プロジェクト隔離 ~
https://dk521123.hatenablog.com/entry/2023/01/15/000000
AWS CloudFormation ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2021/10/26/224812
Python + DOT言語で図作成するには
https://dk521123.hatenablog.com/entry/2023/06/14/174104
DOT言語 ~ 環境設定編 ~
https://dk521123.hatenablog.com/entry/2023/07/18/120407