Skip to content

Commit 57494b5

Browse files
authored
Fix broken name tag (#119)
* Fix broken name tag * Do not set name for default * use Localstack action
1 parent 742ce9f commit 57494b5

File tree

9 files changed

+195
-3
lines changed

9 files changed

+195
-3
lines changed

.github/workflows/terratest.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ jobs:
6666
sudo mv /tmp/terraform /usr/local/bin/
6767
6868
- name: Start Localstack
69-
run: docker-compose up -d
69+
uses: LocalStack/[email protected]
70+
with:
71+
image-tag: "latest"
7072

7173
- name: Terratest
7274
env:

examples/custom-name-tag/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Setting a Custom Name Tag for the Peering Connection
2+
3+
This is a basic configuration example, which creates a peering connection between VPCs in a single region within the same AWS account.
4+
5+
However, here we set a custom name tag for the peering connection using the `name` variable.
6+
7+
**Notice**: You need to declare both providers even with single region peering.
8+
9+
## Code Sample
10+
11+
```hcl
12+
provider "aws" {
13+
region = "eu-west-1"
14+
}
15+
16+
module "single_account_single_region" {
17+
source = "../../"
18+
19+
providers = {
20+
aws.this = aws
21+
aws.peer = aws
22+
}
23+
24+
name = "prod-external"
25+
26+
this_vpc_id = var.this_vpc_id
27+
peer_vpc_id = var.peer_vpc_id
28+
29+
auto_accept_peering = true
30+
31+
tags = {
32+
Environment = "Test"
33+
}
34+
}
35+
```
36+
37+
## Usage
38+
39+
Change the variables to fit your purposes and run:
40+
41+
```bash
42+
terraform init
43+
terraform plan
44+
terraform apply
45+
```

examples/custom-name-tag/main.tf

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Basic Module Example
2+
// Creates a peering between VPCs in the same account in the same region
3+
module "custiom_name" {
4+
source = "../../"
5+
6+
providers = {
7+
aws.this = aws
8+
aws.peer = aws
9+
}
10+
11+
// Required for tests
12+
name = var.name
13+
14+
this_vpc_id = var.this_vpc_id
15+
peer_vpc_id = var.peer_vpc_id
16+
17+
auto_accept_peering = true
18+
19+
tags = {
20+
Name = "tf-single-account-single-region"
21+
Environment = "Test"
22+
}
23+
}

examples/custom-name-tag/outputs.tf

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Required for tests
2+
output "vpc_peering_accept_status" {
3+
value = module.custiom_name.vpc_peering_accept_status
4+
}
5+
6+
output "vpc_peering_connection" {
7+
value = module.custiom_name.aws_vpc_peering_connection
8+
}

examples/custom-name-tag/provider.tf

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This provider example is designed to work with Localstack.
2+
// You need to have a real AWS provider configuration for the production usage.
3+
provider "aws" {
4+
endpoints {
5+
ec2 = "http://localhost:4566"
6+
s3 = "http://localhost:4566"
7+
sts = "http://localhost:4566"
8+
}
9+
region = "eu-west-1"
10+
access_key = "null"
11+
secret_key = "null"
12+
skip_credentials_validation = true
13+
skip_metadata_api_check = true
14+
skip_requesting_account_id = true
15+
}

examples/custom-name-tag/variables.tf

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Variables are required to pass them via Terratest
2+
// on fixtures creation
3+
variable "this_vpc_id" {
4+
type = string
5+
}
6+
7+
variable "peer_vpc_id" {
8+
type = string
9+
}
10+
11+
variable "name" {
12+
description = "Name of the VPC Peering Connection"
13+
default = ""
14+
type = string
15+
}

locals.tf

+31
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,35 @@ locals {
9595
create_associated_routes_peer = var.from_peer && var.from_peer_associated
9696
create_routes_this = var.from_this && !local.create_associated_routes_this
9797
create_routes_peer = var.from_peer && !local.create_associated_routes_peer
98+
99+
# Build tags
100+
requester_tags = var.name == "" ? merge(
101+
var.tags,
102+
tomap(
103+
{ "Side" = local.same_account_and_region ? "Both" : "Requester" }
104+
)
105+
) : merge(
106+
var.tags,
107+
tomap(
108+
{ "Name" = var.name }
109+
),
110+
tomap(
111+
{ "Side" = local.same_account_and_region ? "Both" : "Requester" }
112+
)
113+
)
114+
115+
accepter_tags = var.name == "" ? merge(
116+
var.tags,
117+
tomap(
118+
{ "Side" = local.same_account_and_region ? "Both" : "Accepter" }
119+
)
120+
) : merge(
121+
var.tags,
122+
tomap(
123+
{ "Name" = var.name }
124+
),
125+
tomap(
126+
{ "Side" = local.same_account_and_region ? "Both" : "Accepter" }
127+
)
128+
)
98129
}

main.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ resource "aws_vpc_peering_connection" "this" {
77
peer_vpc_id = var.peer_vpc_id
88
vpc_id = var.this_vpc_id
99
peer_region = data.aws_region.peer.name
10-
tags = merge(var.tags, { "Name" = var.name }, tomap({ "Side" = local.same_account_and_region ? "Both" : "Requester" }))
10+
tags = local.requester_tags
1111
# hardcoded
1212
timeouts {
1313
create = "15m"
@@ -22,7 +22,7 @@ resource "aws_vpc_peering_connection_accepter" "peer_accepter" {
2222
provider = aws.peer
2323
vpc_peering_connection_id = aws_vpc_peering_connection.this.id
2424
auto_accept = var.auto_accept_peering
25-
tags = merge(var.tags, { "Name" = var.name }, tomap({ "Side" = local.same_account_and_region ? "Both" : "Accepter" }))
25+
tags = local.accepter_tags
2626
}
2727

2828
#######################

test/peering-active_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package test
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/gruntwork-io/terratest/modules/terraform"
@@ -35,6 +36,58 @@ func TestPeeringActive(t *testing.T) {
3536
}
3637
}
3738

39+
func TestConnectionName(t *testing.T) {
40+
var testCases = []struct {
41+
name string
42+
expected string
43+
}{
44+
{"DefaultName", "tf-single-account-single-region"},
45+
{"CustomName", "tf-custom-name"},
46+
}
47+
48+
for _, tc := range testCases {
49+
t.Run(tc.name, func(t *testing.T) {
50+
var tfVars = make(map[string]interface{})
51+
// Apply the fixtures
52+
fixturesTerraformOptions := &terraform.Options{
53+
TerraformDir: "./fixtures/single-account-single-region", // hardcoded
54+
}
55+
56+
// Remove the fixtures resources in the end of the test
57+
defer terraform.Destroy(t, fixturesTerraformOptions)
58+
59+
// Install Prerequisites
60+
terraform.InitAndApply(t, fixturesTerraformOptions)
61+
62+
// Get the outputs from fixtures
63+
thisVpcID := terraform.Output(t, fixturesTerraformOptions, "this_vpc_id")
64+
peerVpcID := terraform.Output(t, fixturesTerraformOptions, "peer_vpc_id")
65+
66+
tfVars["this_vpc_id"] = thisVpcID
67+
tfVars["peer_vpc_id"] = peerVpcID
68+
// This is a hack, but I'm too tired to figure out something better
69+
if strings.EqualFold(tc.name, "CustomName") {
70+
tfVars["name"] = tc.expected
71+
}
72+
73+
// Terraform Options for module
74+
moduleTerraformOptions := &terraform.Options{
75+
TerraformDir: "../examples/custom-name-tag", // hardcoded
76+
Vars: tfVars,
77+
}
78+
79+
// Remove the module resources in the end of the test
80+
defer terraform.Destroy(t, moduleTerraformOptions)
81+
// Create module resources
82+
terraform.InitAndApply(t, moduleTerraformOptions)
83+
var conn any
84+
terraform.OutputStruct(t, moduleTerraformOptions, "vpc_peering_connection", &conn)
85+
actualName := conn.(map[string]any)["tags_all"].(map[string]any)["Name"].(string)
86+
assert.Equal(t, tc.expected, actualName)
87+
})
88+
}
89+
}
90+
3891
func terratestRun(tc TestCase, t *testing.T) {
3992
var tfVars = make(map[string]interface{})
4093
// Assertions

0 commit comments

Comments
 (0)