Skip to content

Commit

Permalink
Few enhancements & tests for peering w/ options (#25)
Browse files Browse the repository at this point in the history
* Few enhancements & tests for peering w/ options

* Use test matrix
  • Loading branch information
grem11n committed Jul 11, 2019
1 parent 811e43f commit 847f6f0
Show file tree
Hide file tree
Showing 16 changed files with 258 additions and 64 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ vendor/

# Tests directory
.test-data/

# Workaround for go test
main.go
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Usage
-----

### Examples
Sample configuration is located in [examples](examples/) directory. There are not many of them right now, but I'll add more soon.
Sample configuration is located in [examples](examples/) directory.

### Single Region Peering
**Notice**: You need to declare both providers even with single region peering.
Expand All @@ -58,7 +58,6 @@ module "vpc_single_region_peering" {
peer_vpc_id = "vpc-11111111"
cross_region_peering = false
auto_accept_peering = true
create_peering = true
tags = {
Name = "my-peering-connection"
Expand All @@ -82,7 +81,6 @@ module "vpc_single_region_peering" {
peer_vpc_id = "vpc-11111111"
cross_region_peering = false
auto_accept_peering = true
create_peering = 0
peering_id = "pcx-00000000"
}
Expand All @@ -104,7 +102,6 @@ module "vpc_cross_region_peering" {
peer_vpc_id = "vpc-11111111"
cross_region_peering = true
auto_accept_peering = true
create_peering = true
tags = {
Name = "my-peering-connection"
Expand All @@ -125,9 +122,11 @@ providers = {
peer_account_id = "AAABBBCCC1111" // An ID of the peer AWS account
```

Examples
--------
Complete example is shown above
Testing
----

This module is tested with [Terratest](https://github.com/gruntwork-io/terratest)
You can find existing tests in the [test/](test/) directory.

Authors
-------
Expand Down
30 changes: 30 additions & 0 deletions examples/single-account-single-region-with-options/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Single Account Single Region Peering Connection with Peering Options

Configuration in this directory creates a peering connection between VPCs in a single region within the same AWS account. It also creates connection options:

* Cross-VPC DNS resolution option
* Allow classic link access between VPCs

## Usage

Modify the variables to suite your purposes. Then run:

```bash
terraform init
terraform plan
terraform apply
```

## Testing

This configuration is tested with [Terratest](https://github.com/gruntwork-io/terratest).

You can find tests in [`test/`](../../test) directory.

### Testing notes

I'm unable to properly test VPC peering options because I need to create public subnets (which contain AWS Internet Gateway) and some resources in Classic. These costs money and I don't want to add it to thr CI. Therefore, tests for this module inplementation simply test that module is able to run and peering is created.

## Note

Running the resources in AWS may cost money! Make sure to clean up afterwards. You can use `terraform destroy` to delete the resources spawned by this example.
31 changes: 31 additions & 0 deletions examples/single-account-single-region-with-options/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Single Account single region example
// Additional options are created
module "single_account_single_region_options" {
source = "../../"

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

this_vpc_id = "${var.this_vpc_id}"
peer_vpc_id = "${var.peer_vpc_id}"

create_peering = true
auto_accept_peering = true

// Peering options for requester
this_dns_resolution = true
this_link_to_peer_classic = true
this_link_to_local_classic = true

// Peering options for accepter
peer_dns_resolution = true
peer_link_to_peer_classic = true
peer_link_to_local_classic = true

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

output "accepter_options" {
value = "${module.single_account_single_region_options.accepter_options}"
}

output "requester_options" {
value = "${module.single_account_single_region_options.requester_options}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "aws" {
region = "us-east-1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Variables are required to pass them via Terratest
// on fixtures creation
variable "this_vpc_id" {}

variable "peer_vpc_id" {}
15 changes: 9 additions & 6 deletions examples/single-account-single-region/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# Simple Peering
# Single Account Single Region VPC Peering

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

## Usage

To run this example you need to execute
Change the variables to fit your purposes and run:

```bash
terraform init
terraform plan
terraform apply
```

## Testing

This configuration is tested with [Terratest](https://github.com/gruntwork-io/terratest).

You can find tests in [`test/`](../../test) directory.

## Note

Running the resources in AWS may cost money! Make sure to clean up afterwards. You can use `terraform destroy` to delete the resources spawned by this example.

## TODO:
* Create VPCs and route tables using this example. For now you can use for example [this module](https://github.com/terraform-aws-modules/terraform-aws-vpc/) to seyup prerequisites.
71 changes: 71 additions & 0 deletions test/fixtures/single-account-single-region-with-options/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Fixtures
// VPCs
resource "aws_vpc" "this" {
cidr_block = "172.20.0.0/16"
enable_classiclink = true
enable_dns_support = true
enable_dns_hostnames = true

tags = {
Name = "this_vpc_options"
Environment = "Test"
}
}

resource "aws_vpc" "peer" {
cidr_block = "172.21.0.0/16"
enable_classiclink = true
enable_dns_support = true
enable_dns_hostnames = true

tags = {
Name = "peer_vpc_options"
Environment = "Test"
}
}

// Route Tables
resource "aws_route_table" "this" {
count = "${length(var.this_subnets)}"
vpc_id = "${aws_vpc.this.id}"

tags = {
Name = "This VPC RT"
Environment = "Test"
}
}

resource "aws_route_table" "peer" {
count = "${length(var.peer_subnets)}"
vpc_id = "${aws_vpc.peer.id}"

tags = {
Name = "Peer VPC RT"
Environment = "Test"
}
}

// Subnets
resource "aws_subnet" "this" {
count = "${length(var.this_subnets)}"
vpc_id = "${aws_vpc.this.id}"
cidr_block = "${var.this_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"

tags = {
Name = "This VPC Subnet"
Environment = "Test"
}
}

resource "aws_subnet" "peer" {
count = "${length(var.peer_subnets)}"
vpc_id = "${aws_vpc.peer.id}"
cidr_block = "${var.peer_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"

tags = {
Name = "This VPC Subnet"
Environment = "Test"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "this_vpc_id" {
value = "${aws_vpc.this.id}"
}

output "peer_vpc_id" {
value = "${aws_vpc.peer.id}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "aws" {
region = "us-east-1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
azs = ["us-east-1a", "us-east-1b", "us-east-1c"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Variables
variable "this_subnets" {
description = "Subnet list for _this_ VPC"
type = "list"
default = ["172.20.0.0/24", "172.20.1.0/24", "172.20.2.0/24"]
}

variable "peer_subnets" {
description = "Subnet list for _peer_ VPC"
type = "list"
default = ["172.21.0.0/24", "172.21.1.0/24", "172.21.2.0/24"]
}

variable "azs" {
description = "Availability Zones"
type = "list"
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
// Fixtures
// Variables
variable "this_subnets" {
description = "Subnet list for _this_ VPC"
type = "list"
default = ["172.20.0.0/24", "172.20.1.0/24", "172.20.2.0/24"]
}

variable "peer_subnets" {
description = "Subnet list for _peer_ VPC"
type = "list"
default = ["172.21.0.0/24", "172.21.1.0/24", "172.21.2.0/24"]
}

variable "azs" {
description = "Availability Zones"
type = "list"
}

// VPCs
resource "aws_vpc" "this" {
cidr_block = "172.20.0.0/16"
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/single-account-single-region/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Variables
variable "this_subnets" {
description = "Subnet list for _this_ VPC"
type = "list"
default = ["172.20.0.0/24", "172.20.1.0/24", "172.20.2.0/24"]
}

variable "peer_subnets" {
description = "Subnet list for _peer_ VPC"
type = "list"
default = ["172.21.0.0/24", "172.21.1.0/24", "172.21.2.0/24"]
}

variable "azs" {
description = "Availability Zones"
type = "list"
}
Loading

0 comments on commit 847f6f0

Please sign in to comment.