Skip to content

Commit

Permalink
Merge branch 'master' into use_ipv6_as_options
Browse files Browse the repository at this point in the history
  • Loading branch information
grem11n authored Nov 4, 2024
2 parents fc2c5e3 + fc13f57 commit a0fb620
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 8 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/terratest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
terraform: ['1.5.7', '1.7.3']
terraform: ['1.5.7', '1.9.8']
steps:
- uses: actions/checkout@master
- uses: hashicorp/setup-terraform@v1
Expand All @@ -46,7 +46,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
terraform: ['1.5.7', '1.7.3']
terraform: ['1.5.7', '1.9.8']
steps:
- name: Set up Go
uses: actions/setup-go@v2
Expand All @@ -63,16 +63,18 @@ jobs:
run: >
wget https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_linux_amd64.zip -O /tmp/terraform.zip &&
sudo unzip -d /tmp/ /tmp/terraform.zip &&
sudo mv /tmp/terraform /usr/local/bin/
sudo mv /tmp/terraform /usr/local/bin/ &&
go install github.com/kyoh86/richgo@latest
- name: Start Localstack
run: docker-compose up -d
uses: LocalStack/setup-localstack@v0.2.3
with:
image-tag: "latest"

- name: Terratest
env:
GOPATH: /home/runner/work/terraform-aws-vpc-peering/go
run: >
go mod init github.com/grem11n/terraform-aws-vpc-peering &&
go mod tidy &&
echo 'package main' > main.go &&
go test -v -timeout=30m ./...
richgo test -v -timeout=30m ./...
45 changes: 45 additions & 0 deletions examples/custom-name-tag/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Setting a Custom Name Tag for the Peering Connection

This is a basic configuration example, which creates a peering connection between VPCs in a single region within the same AWS account.

However, here we set a custom name tag for the peering connection using the `name` variable.

**Notice**: You need to declare both providers even with single region peering.

## Code Sample

```hcl
provider "aws" {
region = "eu-west-1"
}
module "single_account_single_region" {
source = "../../"
providers = {
aws.this = aws
aws.peer = aws
}
name = "prod-external"
this_vpc_id = var.this_vpc_id
peer_vpc_id = var.peer_vpc_id
auto_accept_peering = true
tags = {
Environment = "Test"
}
}
```

## Usage

Change the variables to fit your purposes and run:

```bash
terraform init
terraform plan
terraform apply
```
23 changes: 23 additions & 0 deletions examples/custom-name-tag/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Basic Module Example
// Creates a peering between VPCs in the same account in the same region
module "custiom_name" {
source = "../../"

providers = {
aws.this = aws
aws.peer = aws
}

// Required for tests
name = var.name

this_vpc_id = var.this_vpc_id
peer_vpc_id = var.peer_vpc_id

auto_accept_peering = true

tags = {
Name = "tf-single-account-single-region"
Environment = "Test"
}
}
8 changes: 8 additions & 0 deletions examples/custom-name-tag/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Required for tests
output "vpc_peering_accept_status" {
value = module.custiom_name.vpc_peering_accept_status
}

output "vpc_peering_connection" {
value = module.custiom_name.aws_vpc_peering_connection
}
15 changes: 15 additions & 0 deletions examples/custom-name-tag/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This provider example is designed to work with Localstack.
// You need to have a real AWS provider configuration for the production usage.
provider "aws" {
endpoints {
ec2 = "http://localhost:4566"
s3 = "http://localhost:4566"
sts = "http://localhost:4566"
}
region = "eu-west-1"
access_key = "null"
secret_key = "null"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
}
15 changes: 15 additions & 0 deletions examples/custom-name-tag/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Variables are required to pass them via Terratest
// on fixtures creation
variable "this_vpc_id" {
type = string
}

variable "peer_vpc_id" {
type = string
}

variable "name" {
description = "Name of the VPC Peering Connection"
default = ""
type = string
}
31 changes: 31 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,35 @@ locals {
create_routes_this_ipv6 = var.from_this && !local.create_associated_routes_this && var.use_ipv6
create_routes_peer = var.from_peer && !local.create_associated_routes_peer
create_routes_peer_ipv6 = var.from_peer && !local.create_associated_routes_peer && var.use_ipv6

# Build tags
requester_tags = var.name == "" ? merge(
var.tags,
tomap(
{ "Side" = local.same_account_and_region ? "Both" : "Requester" }
)
) : merge(
var.tags,
tomap(
{ "Name" = var.name }
),
tomap(
{ "Side" = local.same_account_and_region ? "Both" : "Requester" }
)
)

accepter_tags = var.name == "" ? merge(
var.tags,
tomap(
{ "Side" = local.same_account_and_region ? "Both" : "Accepter" }
)
) : merge(
var.tags,
tomap(
{ "Name" = var.name }
),
tomap(
{ "Side" = local.same_account_and_region ? "Both" : "Accepter" }
)
)
}
4 changes: 2 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ resource "aws_vpc_peering_connection" "this" {
peer_vpc_id = var.peer_vpc_id
vpc_id = var.this_vpc_id
peer_region = data.aws_region.peer.name
tags = merge(var.tags, { "Name" = var.name }, tomap({ "Side" = local.same_account_and_region ? "Both" : "Requester" }))
tags = local.requester_tags
# hardcoded
timeouts {
create = "15m"
Expand All @@ -22,7 +22,7 @@ resource "aws_vpc_peering_connection_accepter" "peer_accepter" {
provider = aws.peer
vpc_peering_connection_id = aws_vpc_peering_connection.this.id
auto_accept = var.auto_accept_peering
tags = merge(var.tags, { "Name" = var.name }, tomap({ "Side" = local.same_account_and_region ? "Both" : "Accepter" }))
tags = local.accepter_tags
}

#######################
Expand Down
53 changes: 53 additions & 0 deletions test/peering-active_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"strings"
"testing"

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

func TestConnectionName(t *testing.T) {
var testCases = []struct {
name string
expected string
}{
{"DefaultName", "tf-single-account-single-region"},
{"CustomName", "tf-custom-name"},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var tfVars = make(map[string]interface{})
// Apply the fixtures
fixturesTerraformOptions := &terraform.Options{
TerraformDir: "./fixtures/single-account-single-region", // hardcoded
}

// Remove the fixtures resources in the end of the test
defer terraform.Destroy(t, fixturesTerraformOptions)

// Install Prerequisites
terraform.InitAndApply(t, fixturesTerraformOptions)

// Get the outputs from fixtures
thisVpcID := terraform.Output(t, fixturesTerraformOptions, "this_vpc_id")
peerVpcID := terraform.Output(t, fixturesTerraformOptions, "peer_vpc_id")

tfVars["this_vpc_id"] = thisVpcID
tfVars["peer_vpc_id"] = peerVpcID
// This is a hack, but I'm too tired to figure out something better
if strings.EqualFold(tc.name, "CustomName") {
tfVars["name"] = tc.expected
}

// Terraform Options for module
moduleTerraformOptions := &terraform.Options{
TerraformDir: "../examples/custom-name-tag", // hardcoded
Vars: tfVars,
}

// Remove the module resources in the end of the test
defer terraform.Destroy(t, moduleTerraformOptions)
// Create module resources
terraform.InitAndApply(t, moduleTerraformOptions)
var conn any
terraform.OutputStruct(t, moduleTerraformOptions, "vpc_peering_connection", &conn)
actualName := conn.(map[string]any)["tags_all"].(map[string]any)["Name"].(string)
assert.Equal(t, tc.expected, actualName)
})
}
}

func terratestRun(tc TestCase, t *testing.T) {
var tfVars = make(map[string]interface{})
// Assertions
Expand Down

0 comments on commit a0fb620

Please sign in to comment.