自由気ままに書いちゃおう

好きなことをつらつらと・・・

【Terraform】(初心者向け)よく使う関数とメタ引数_その1

Terraformを使う上で利用頻度が多い関数(function)とメタ引数(MetaArguments)を独断と偏見でピックアップしたものです。

チートシート的な役割で自分の備忘になればという内容です。

整理すると思いの外たくさん使っていたみたいなので、本記事は1回目です。

よく使うfunction/MetaArguments

1. count

  • リソースのインスタンス数を制御するために使用されます。この属性を使用することで、同じ設定のリソースを複数作成できます。
  • MetaArgumentsです。
   resource "aws_instance" "example" {
     count = 3 # 3つのインスタンスを作成
     # ... その他の設定 ...
   }

2. lookup

  • マップからキーに対応する値を取得します。第3引数にデフォルト値を指定することもできます。
  • Functionです。
   variable "instance_types" {
     type = map(string)
     default = {
       "prod": "t2.large",
       "dev": "t2.micro"
     }
   }

   resource "aws_instance" "example" {
     instance_type = lookup(var.instance_types, var.environment, "t2.micro")
     # ... その他の設定 ...
   }

3. element

  • リストから特定の位置の要素を取得します。count.indexと組み合わせて使用することが一般的です。
  • Functionです。
   variable "subnets" {
     type = list(string)
     default = ["subnet-abcde012", "subnet-bcde012a", "subnet-fghij034"]
   }

   resource "aws_instance" "example" {
     subnet_id = element(var.subnets, count.index)
     count     = 3
     # ... その他の設定 ...
   }

4. concat

  • 複数のリストを結合して新しいリストを作成します。
  • Functionです。
   variable "list1" {
     type = list(string)
     default = ["one", "two"]
   }

   variable "list2" {
     type = list(string)
     default = ["three", "four"]
   }

   output "combined_list" {
     value = concat(var.list1, var.list2)
   }

5. length

  • リストやマップの長さ(要素の数)を取得します。
  • Functionです。
   variable "subnets" {
     type = list(string)
     default = ["subnet-abcde012", "subnet-bcde012a", "subnet-fghij034"]
   }

   output "number_of_subnets" {
     value = length(var.subnets)
   }

6. for_each

  • コレクション(リストやマップ)の各要素に対して繰り返し処理を行います。
  • MetaArgumentsです。
   variable "tags" {
     type = map(string)
     default = {
       "Environment" = "Dev",
       "Project"     = "WebApp"
     }
   }

   resource "aws_instance" "example" {
     for_each = var.tags
     # ... その他の設定 ...
     tags = {
       "Name" = each.key
       "Value" = each.value
     }
   }

7. join

  • リストの要素を特定の区切り文字で結合して文字列を作成します。
  • Functionです。
   variable "security_groups" {
     type = list(string)
     default = ["sg-abcde012", "sg-bcde012a", "sg-fghij034"]
   }

   output "security_group_string" {
     value = join(",", var.security_groups)
   }

8. split

  • 文字列を特定の区切り文字で分割してリストを作成します。
  • Functionです。
   variable "subnet_ids" {
     type = string
     default = "subnet-abcde012,subnet-bcde012a,subnet-fghij034"
   }

   output "subnet_list" {
     value = split(",", var.subnet_ids)
   }

9. map

  • キーと値のペアからなるマップを作成します。
  • Functionです。
   variable "instance_types" {
     type = map(string)
     default = {
       "prod": "t2.large",
       "dev": "t2.micro"
     }
   }

   output "instance_types_map" {
     value = var.instance_types
   }

10. file

- ファイルの内容を読み込んで、その内容を文字列として返します。
- Functionです。

```hcl
resource "aws_instance" "example" {
  # ... その他の設定 ...
  user_data = file("${path.module}/init-script.sh")
}
```

以上です。

2回目は以下記事です。

www.guri2o1667.work