バージョン
1 2 |
<span class="nv">$ </span>terraform <span class="nt">--version</span> Terraform v0.12.24 |
変数
- map型で作成
tfvars.tf
1 2 3 4 |
<span class="nx">albs</span> <span class="err">=</span> <span class="p">{</span> <span class="nx">demo</span> <span class="p">=</span> <span class="s2">"vamdemic-development-demo-alb"</span> <span class="nx">dev2</span> <span class="p">=</span> <span class="s2">"vamdemic-development-dev2-alb"</span> <span class="p">}</span> |
foreachで取り扱うソース
- リソース内に
for_each
を使うことで、その要素分ループする each.value
でvariablesで定義した値を使うことができるeach.key
でリソース本体を取得data.aws_lb.lb["${each.key}"].arn
のようにすることで、data型で取得したリソースのarnを呼び出している
wafv2.tf
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="c1"># Get ALB arn</span> <span class="nx">data</span> <span class="s2">"aws_lb"</span> <span class="s2">"lb"</span> <span class="p">{</span> <span class="nx">for_each</span> <span class="p">=</span> <span class="nx">var</span><span class="err">.</span><span class="nx">albs</span> <span class="nx">name</span> <span class="p">=</span> <span class="nx">each</span><span class="err">.</span><span class="nx">value</span> <span class="p">}</span> <span class="c1"># WAFv2 assosiation to ALB</span> <span class="nx">resource</span> <span class="s2">"aws_wafv2_web_acl_association"</span> <span class="s2">"main"</span> <span class="p">{</span> <span class="nx">for_each</span> <span class="p">=</span> <span class="nx">data</span><span class="err">.</span><span class="nx">aws_lb</span><span class="err">.</span><span class="nx">lb</span> <span class="nx">resource_arn</span> <span class="p">=</span> <span class="nx">data</span><span class="err">.</span><span class="nx">aws_lb</span><span class="err">.</span><span class="nx">lb</span><span class="p">[</span><span class="s2">"${each.key}"</span><span class="p">]</span><span class="err">.</span><span class="nx">arn</span> <span class="nx">web_acl_arn</span> <span class="p">=</span> <span class="nx">aws_wafv2_web_acl</span><span class="err">.</span><span class="nx">example</span><span class="err">.</span><span class="nx">arn</span> <span class="p">}</span> |