https://www.terraform-best-practices.com/
Terraform Best Practiceを読みましょう。中でもNaming conventionsは非常に有用です。
Terraform設計・運用のノウハウ #AWS - Qiita 2020
命名規則
https://www.terraform-best-practices.com/naming
項目名 命名規則 ファイル名 ケバブケース ディレクトリ名 ケバブケース Terraformリソース名 スネークケース AWSリソース名 ケバブケース Terraform変数名 スネークケース
ケバブケース=ハイフン区切り
スネークケース=アンダースコア区切り
https://github.com/terraform-aws-modules/terraform-aws-vpc/blob/master/main.tf
Terraformリソース名
- Do not repeat resource type in resource name
- Resource name should be named this if there is no more descriptive and general name available, or if the resource module creates a single resource of this type
...
https://www.terraform-best-practices.com/naming#resource-and-data-source-arguments
this
resource "aws_cloudfront_distribution" "this" {
Route53レコード
apex
resource "aws_route53_record" "apex" {
https://registry.terraform.io/providers/fastly/fastly/latest/docs/resources/tls_subscription
_type(Aは省略)
resource "aws_route53_record" "apex" { zone_id = aws_route53_zone.this.zone_id name = local.domain type = "A" ... } resource "aws_route53_record" "apex_txt" { zone_id = aws_route53_zone.this.zone_id name = local.domain type = "TXT" ... }
AWSリソース名
リソースの種類を示すような名前は含めない
TerraformでFargateを構築してGitHub Actionsでデプロイ!Laravel編
AWS VPC Terraform module
Terraform Registry
リソースの種類を示す名前を含める
また、採用する文字は「半角英数字とハイフンなどの一部記号のみ」とすべきです。安易に特殊文字を利用するとプログラムで処理する際に苦しむことになります。
心得その5 「命名規則は、ローカルで変更しちゃえ!」
AWS再入門ブログリレー2022 Terraform Registry Module (AWS VPC Terraform module) 編 | DevelopersIO 2022
https://github.com/terraform-aws-modules/terraform-aws-vpc
-vpc -rtb -igw -subnet- に変更
AWSリソース名の変更
セキュリティグループ
AWS will not allow us to destroy the SG while an Instance is using it.
Terraform apply fails when renaming aws_security_group · Issue #3341 · hashicorp/terraform · GitHub
複数AWS環境
以下の順番で各流派について説明していきます。
Workspace
Module
ディレクトリ分離
Terraformのベストなプラクティスってなんだろうか | フューチャー技術ブログ
terraform workspace
変数
A variables.tf file is used to define the variables type and optionally set a default value.
A terraform.tfvars file is used to set the actual values of the variables.terraform.tfvars vs variables.tf difference - Stack Overflow
TL; DR 今北産業
tfファイル内の変数は基本的にLocal Valuesを使おう
特に判定処理はLocal Valuesで明確な名前をつけよう
Variableを使うのは外部からのインプットにする場合だけ
【モダンTerraform】VariableとLocal Valuesの使い分けについて - febc技術メモ