1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

terraformでforeachを使う方法

Last updated at Posted at 2020-07-11

バージョン


$ terraform --version
Terraform v0.12.24

変数

  • map型で作成
tfvars.tf
albs = {
  demo = "vamdemic-development-demo-alb"
  dev2 = "vamdemic-development-dev2-alb"
}

foreachで取り扱うソース

  • リソース内にfor_eachを使うことで、その要素分ループする
  • each.valueでvariablesで定義した値を使うことができる
  • each.keyでリソース本体を取得
  • data.aws_lb.lb["${each.key}"].arnのようにすることで、data型で取得したリソースのarnを呼び出している
wafv2.tf
# Get ALB arn
data "aws_lb" "lb" {
 for_each = var.albs
 name = each.value
}

# WAFv2 assosiation to ALB
resource "aws_wafv2_web_acl_association" "main" {
  for_each = data.aws_lb.lb
  resource_arn = data.aws_lb.lb["${each.key}"].arn
  web_acl_arn  = aws_wafv2_web_acl.example.arn
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?