Skip to content

Commit a206e43

Browse files
authored
fix: Use lookup() on computed resource attribute lookups in for_each loop (#18)
1 parent 8cdc5b6 commit a206e43

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.77.0
3+
rev: v1.83.5
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate
@@ -23,7 +23,7 @@ repos:
2323
- '--args=--only=terraform_standard_module_structure'
2424
- '--args=--only=terraform_workspace_remote'
2525
- repo: https://github.com/pre-commit/pre-commit-hooks
26-
rev: v4.4.0
26+
rev: v4.5.0
2727
hooks:
2828
- id: check-merge-conflict
2929
- id: end-of-file-fixer

examples/complete/main.tf

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ provider "aws" {
44

55
locals {
66
region = "eu-west-1"
7-
name = "efs-ex-${replace(basename(path.cwd), "_", "-")}"
7+
name = "ex-${basename(path.cwd)}"
88

99
azs = slice(data.aws_availability_zones.available.names, 0, 3)
1010

@@ -135,9 +135,8 @@ module "vpc" {
135135
public_subnets = ["10.99.0.0/24", "10.99.1.0/24", "10.99.2.0/24"]
136136
private_subnets = ["10.99.3.0/24", "10.99.4.0/24", "10.99.5.0/24"]
137137

138-
enable_nat_gateway = false
139-
single_nat_gateway = true
140-
map_public_ip_on_launch = false
138+
enable_nat_gateway = false
139+
single_nat_gateway = true
141140

142141
tags = local.tags
143142
}

main.tf

+8-6
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,12 @@ resource "aws_efs_mount_target" "this" {
129129

130130
locals {
131131
security_group_name = try(coalesce(var.security_group_name, var.name), "")
132+
133+
create_security_group = var.create && var.create_security_group && length(var.mount_targets) > 0
132134
}
133135

134136
resource "aws_security_group" "this" {
135-
count = var.create && var.create_security_group && length(var.mount_targets) > 0 ? 1 : 0
137+
count = local.create_security_group ? 1 : 0
136138

137139
name = var.security_group_use_name_prefix ? null : local.security_group_name
138140
name_prefix = var.security_group_use_name_prefix ? "${local.security_group_name}-" : null
@@ -149,7 +151,7 @@ resource "aws_security_group" "this" {
149151
}
150152

151153
resource "aws_security_group_rule" "this" {
152-
for_each = { for k, v in var.security_group_rules : k => v if var.create && var.create_security_group }
154+
for_each = { for k, v in var.security_group_rules : k => v if local.create_security_group }
153155

154156
security_group_id = aws_security_group.this[0].id
155157

@@ -158,11 +160,11 @@ resource "aws_security_group_rule" "this" {
158160
from_port = try(each.value.from_port, 2049)
159161
to_port = try(each.value.to_port, 2049)
160162
protocol = try(each.value.protocol, "tcp")
161-
cidr_blocks = try(each.value.cidr_blocks, null)
162-
ipv6_cidr_blocks = try(each.value.ipv6_cidr_blocks, null)
163-
prefix_list_ids = try(each.value.prefix_list_ids, null)
163+
cidr_blocks = lookup(each.value, "cidr_blocks", null)
164+
ipv6_cidr_blocks = lookup(each.value, "ipv6_cidr_blocks", null)
165+
prefix_list_ids = lookup(each.value, "prefix_list_ids", null)
164166
self = try(each.value.self, null)
165-
source_security_group_id = try(each.value.source_security_group_id, null)
167+
source_security_group_id = lookup(each.value, "source_security_group_id", null)
166168

167169
lifecycle {
168170
create_before_destroy = true

0 commit comments

Comments
 (0)