From 010881d19e1cada9852f1f9bb3d5775c4bf6a6f1 Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Mon, 7 Oct 2024 13:13:29 -0700 Subject: [PATCH 1/5] bugfix when tgw is enabled --- main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.tf b/main.tf index db43566..3be2c47 100644 --- a/main.tf +++ b/main.tf @@ -5,7 +5,7 @@ locals { transit_gateway_enabled = local.enabled && var.transit_gateway_enabled transit_gateway_attachment_id = join("", aws_vpn_connection.default[*].transit_gateway_attachment_id) - vpn_gateway_id = join("", aws_vpn_gateway.default[*].id) + vpn_gateway_id = one(aws_vpn_gateway.default[*].id) customer_gateway_id = join("", aws_customer_gateway.default[*].id) vpn_connection_id = join("", aws_vpn_connection.default[*].id) } @@ -96,7 +96,7 @@ resource "aws_vpn_connection" "default" { # https://www.terraform.io/docs/providers/aws/r/vpn_gateway_route_propagation.html resource "aws_vpn_gateway_route_propagation" "default" { - count = local.enabled ? length(var.route_table_ids) : 0 + count = local.enabled && !var.transit_gateway_enabled ? length(var.route_table_ids) : 0 vpn_gateway_id = local.vpn_gateway_id route_table_id = element(var.route_table_ids, count.index) } From 0d6eeecf819222a3b4cdc2cb58847ff6f63f92c2 Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Mon, 7 Oct 2024 17:14:07 -0700 Subject: [PATCH 2/5] update tests --- test/src/examples_complete_test.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/test/src/examples_complete_test.go b/test/src/examples_complete_test.go index d36fd26..f529254 100644 --- a/test/src/examples_complete_test.go +++ b/test/src/examples_complete_test.go @@ -2,6 +2,7 @@ package test import ( "os" + "os/exec" "strings" "testing" @@ -20,11 +21,26 @@ func cleanup(t *testing.T, terraformOptions *terraform.Options, tempTestFolder s os.RemoveAll(tempTestFolder) } +func detectPlatform() string { + cmd := exec.Command("terraform", "--version") + out, _ := cmd.CombinedOutput() + platform := "" + if strings.Contains(string(out), "Terraform") { + platform = "tf" + } else if strings.Contains(string(out), "OpenTofu") { + platform = "tofu" + } else { + platform = "unknown" + } + return platform +} + // Test the Terraform module in examples/complete using Terratest. func TestExamplesComplete(t *testing.T) { t.Parallel() randID := strings.ToLower(random.UniqueId()) - attributes := []string{randID} + platform := detectPlatform() + attributes := []string{randID, platform} rootFolder := "../../" terraformFolderRelativeToRoot := "examples/complete" @@ -61,7 +77,8 @@ func TestExamplesComplete(t *testing.T) { func TestExamplesCompleteDisabled(t *testing.T) { t.Parallel() randID := strings.ToLower(random.UniqueId()) - attributes := []string{randID} + platform := detectPlatform() + attributes := []string{randID, platform} rootFolder := "../../" terraformFolderRelativeToRoot := "examples/complete" From fea74858140ecb87a4637f8124ec6991ed9d00db Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Mon, 7 Oct 2024 17:24:04 -0700 Subject: [PATCH 3/5] add id to device name of customer gateway --- main.tf | 1 + 1 file changed, 1 insertion(+) diff --git a/main.tf b/main.tf index 3be2c47..36a60e5 100644 --- a/main.tf +++ b/main.tf @@ -21,6 +21,7 @@ resource "aws_vpn_gateway" "default" { # https://www.terraform.io/docs/providers/aws/r/customer_gateway.html resource "aws_customer_gateway" "default" { count = local.enabled && var.customer_gateway_ip_address != null ? 1 : 0 + device_name = module.this.id bgp_asn = var.customer_gateway_bgp_asn ip_address = var.customer_gateway_ip_address type = "ipsec.1" From b0241944a861d4d602ccdffd3d49d5c5ef603ef6 Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Mon, 7 Oct 2024 17:27:59 -0700 Subject: [PATCH 4/5] add id to device name of customer gateway --- main.tf | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/main.tf b/main.tf index 36a60e5..9687684 100644 --- a/main.tf +++ b/main.tf @@ -5,9 +5,9 @@ locals { transit_gateway_enabled = local.enabled && var.transit_gateway_enabled transit_gateway_attachment_id = join("", aws_vpn_connection.default[*].transit_gateway_attachment_id) - vpn_gateway_id = one(aws_vpn_gateway.default[*].id) - customer_gateway_id = join("", aws_customer_gateway.default[*].id) - vpn_connection_id = join("", aws_vpn_connection.default[*].id) + vpn_gateway_id = one(aws_vpn_gateway.default[*].id) + customer_gateway_id = join("", aws_customer_gateway.default[*].id) + vpn_connection_id = join("", aws_vpn_connection.default[*].id) } # https://www.terraform.io/docs/providers/aws/r/vpn_gateway.html @@ -20,12 +20,12 @@ resource "aws_vpn_gateway" "default" { # https://www.terraform.io/docs/providers/aws/r/customer_gateway.html resource "aws_customer_gateway" "default" { - count = local.enabled && var.customer_gateway_ip_address != null ? 1 : 0 + count = local.enabled && var.customer_gateway_ip_address != null ? 1 : 0 device_name = module.this.id - bgp_asn = var.customer_gateway_bgp_asn - ip_address = var.customer_gateway_ip_address - type = "ipsec.1" - tags = module.this.tags + bgp_asn = var.customer_gateway_bgp_asn + ip_address = var.customer_gateway_ip_address + type = "ipsec.1" + tags = module.this.tags } module "logs" { @@ -67,7 +67,8 @@ resource "aws_vpn_connection" "default" { cloudwatch_log_options { log_enabled = var.vpn_connection_tunnel1_cloudwatch_log_enabled log_group_arn = var.vpn_connection_tunnel1_cloudwatch_log_enabled ? module.logs.log_group_arn : null - log_output_format = var.vpn_connection_tunnel1_cloudwatch_log_enabled ? var.vpn_connection_tunnel1_cloudwatch_log_output_format : null + log_output_format = var.vpn_connection_tunnel1_cloudwatch_log_enabled ? + var.vpn_connection_tunnel1_cloudwatch_log_output_format : null } } @@ -88,7 +89,8 @@ resource "aws_vpn_connection" "default" { cloudwatch_log_options { log_enabled = var.vpn_connection_tunnel2_cloudwatch_log_enabled log_group_arn = var.vpn_connection_tunnel2_cloudwatch_log_enabled ? module.logs.log_group_arn : null - log_output_format = var.vpn_connection_tunnel2_cloudwatch_log_enabled ? var.vpn_connection_tunnel2_cloudwatch_log_output_format : null + log_output_format = var.vpn_connection_tunnel2_cloudwatch_log_enabled ? + var.vpn_connection_tunnel2_cloudwatch_log_output_format : null } } @@ -104,8 +106,9 @@ resource "aws_vpn_gateway_route_propagation" "default" { # https://www.terraform.io/docs/providers/aws/r/vpn_connection_route.html resource "aws_vpn_connection_route" "default" { - count = local.enabled && var.vpn_connection_static_routes_only ? length(var.vpn_connection_static_routes_destinations) : 0 - vpn_connection_id = local.vpn_connection_id + count = local.enabled && var.vpn_connection_static_routes_only ? + length(var.vpn_connection_static_routes_destinations) : 0 + vpn_connection_id = local.vpn_connection_id destination_cidr_block = element(var.vpn_connection_static_routes_destinations, count.index) } @@ -140,7 +143,8 @@ resource "aws_ec2_transit_gateway_route_table_propagation" "default" { # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_transit_gateway_route resource "aws_ec2_transit_gateway_route" "default" { - for_each = local.transit_gateway_enabled && var.transit_gateway_route_table_id != null ? var.transit_gateway_routes : {} + for_each = local.transit_gateway_enabled && var.transit_gateway_route_table_id != null ? var.transit_gateway_routes : + {} blackhole = each.value.blackhole destination_cidr_block = each.value.destination_cidr_block From 8f138130ec4e9ce05000c1c4a7358ae35ca7a6ba Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Mon, 7 Oct 2024 17:31:51 -0700 Subject: [PATCH 5/5] terraform fmt --- main.tf | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/main.tf b/main.tf index 9687684..bdef9a3 100644 --- a/main.tf +++ b/main.tf @@ -5,9 +5,9 @@ locals { transit_gateway_enabled = local.enabled && var.transit_gateway_enabled transit_gateway_attachment_id = join("", aws_vpn_connection.default[*].transit_gateway_attachment_id) - vpn_gateway_id = one(aws_vpn_gateway.default[*].id) - customer_gateway_id = join("", aws_customer_gateway.default[*].id) - vpn_connection_id = join("", aws_vpn_connection.default[*].id) + vpn_gateway_id = one(aws_vpn_gateway.default[*].id) + customer_gateway_id = join("", aws_customer_gateway.default[*].id) + vpn_connection_id = join("", aws_vpn_connection.default[*].id) } # https://www.terraform.io/docs/providers/aws/r/vpn_gateway.html @@ -67,8 +67,7 @@ resource "aws_vpn_connection" "default" { cloudwatch_log_options { log_enabled = var.vpn_connection_tunnel1_cloudwatch_log_enabled log_group_arn = var.vpn_connection_tunnel1_cloudwatch_log_enabled ? module.logs.log_group_arn : null - log_output_format = var.vpn_connection_tunnel1_cloudwatch_log_enabled ? - var.vpn_connection_tunnel1_cloudwatch_log_output_format : null + log_output_format = var.vpn_connection_tunnel1_cloudwatch_log_enabled ? var.vpn_connection_tunnel1_cloudwatch_log_output_format : null } } @@ -89,8 +88,7 @@ resource "aws_vpn_connection" "default" { cloudwatch_log_options { log_enabled = var.vpn_connection_tunnel2_cloudwatch_log_enabled log_group_arn = var.vpn_connection_tunnel2_cloudwatch_log_enabled ? module.logs.log_group_arn : null - log_output_format = var.vpn_connection_tunnel2_cloudwatch_log_enabled ? - var.vpn_connection_tunnel2_cloudwatch_log_output_format : null + log_output_format = var.vpn_connection_tunnel2_cloudwatch_log_enabled ? var.vpn_connection_tunnel2_cloudwatch_log_output_format : null } } @@ -106,9 +104,8 @@ resource "aws_vpn_gateway_route_propagation" "default" { # https://www.terraform.io/docs/providers/aws/r/vpn_connection_route.html resource "aws_vpn_connection_route" "default" { - count = local.enabled && var.vpn_connection_static_routes_only ? - length(var.vpn_connection_static_routes_destinations) : 0 - vpn_connection_id = local.vpn_connection_id + count = local.enabled && var.vpn_connection_static_routes_only ? length(var.vpn_connection_static_routes_destinations) : 0 + vpn_connection_id = local.vpn_connection_id destination_cidr_block = element(var.vpn_connection_static_routes_destinations, count.index) } @@ -143,8 +140,7 @@ resource "aws_ec2_transit_gateway_route_table_propagation" "default" { # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_transit_gateway_route resource "aws_ec2_transit_gateway_route" "default" { - for_each = local.transit_gateway_enabled && var.transit_gateway_route_table_id != null ? var.transit_gateway_routes : - {} + for_each = local.transit_gateway_enabled && var.transit_gateway_route_table_id != null ? var.transit_gateway_routes : {} blackhole = each.value.blackhole destination_cidr_block = each.value.destination_cidr_block