Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Fix private routing table outputs (#10)
Browse files Browse the repository at this point in the history
* Fix routing table outputs

* fix generate docs command
  • Loading branch information
Puneeth-n authored May 27, 2020
1 parent caf9aa4 commit 09750d5
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ develop:
@$(DOCKER_COMPOSE_DEVELOP) down -v

generate-docs: fmt lint
@terraform-docs --no-escape markdown . > README.md
@$(shell terraform-docs markdown --no-escape . > README.md)

clean-state:
@find . -type f -name 'terraform.tfstate' | xargs rm -rf
Expand Down
73 changes: 72 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,72 @@
Error: unknown flag: --no-escape
# Terraform AWS module for creating VPC resources

## Introduction

This module is used to create VPCs in your AWS account. It is a complete rewrite of our internal Terraform AWS VPC module. see branch (1.x).

_Note on Terraforming elastic IPs outside of the module. The elastic IPs should be Terraformed before specifying the vpc module. So Terraform should be applied in two phases. one for EIPs and then the VPC module._

## Usage
Checkout [example.tf](./examples/example.tf) and [test cases](./test) for how to use this module

## Authors

Module managed by [Comtravo](https://github.com/comtravo).

## License

MIT Licensed. See [LICENSE](LICENSE) for full details.

## Requirements

| Name | Version |
|------|---------|
| terraform | >= 0.12 |
| aws | ~> 2.0 |

## Providers

| Name | Version |
|------|---------|
| aws | ~> 2.0 |
| null | n/a |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| assign_generated_ipv6_cidr_block | Create ipv6 CIDR block | `bool` | `true` | no |
| availability_zones | List of avaliability zones | `list(string)` | n/a | yes |
| cidr | CIDR of the VPC | `string` | n/a | yes |
| depends_id | Inter module dependency id | `string` | `""` | no |
| enable | Enable or disable creation of resources | `bool` | `true` | no |
| enable_dns_hostnames | Enable DNS hostmanes in VPC | `bool` | `true` | no |
| enable_dns_support | Enable DNS support in VPC | `bool` | `true` | no |
| external_elastic_ips | List of elastic IPs to use instead of creating within the module | `list(string)` | `[]` | no |
| nat_gateway | NAT gateway creation behavior. If `one_nat_per_availability_zone` A NAT gateway is created per availability zone. | <pre>object({<br> behavior = string<br> })</pre> | <pre>{<br> "behavior": "one_nat_per_vpc"<br>}</pre> | no |
| private_subnets | Private subnet CIDR ipv4 config | <pre>object({<br> number_of_subnets = number<br> newbits = number<br> netnum_offset = number<br> })</pre> | <pre>{<br> "netnum_offset": 0,<br> "newbits": 8,<br> "number_of_subnets": 3<br>}</pre> | no |
| public_subnets | Public subnet CIDR ipv4 config | <pre>object({<br> number_of_subnets = number<br> newbits = number<br> netnum_offset = number<br> })</pre> | <pre>{<br> "netnum_offset": 100,<br> "newbits": 8,<br> "number_of_subnets": 3<br>}</pre> | no |
| subdomain | Public subdomain name | `string` | `""` | no |
| tags | Map of tags to tag resources | `map` | `{}` | no |
| vpc_name | Name of the VPC | `string` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| depends_id | Dependency id |
| elastic_ips | List of elastic ips |
| nat_gateway_ids | NAT gateway ids |
| net0ps_zone_id | Private hosted zone id |
| private_subdomain | Private hosted zone name |
| private_subnets | List of private subnets |
| private_zone_id | Private hosted zone name |
| public_subdomain | Public hosted zone name |
| public_subdomain_zone_id | Public hosted zone id |
| public_subnets | List of public subnets |
| subdomain_zone_id | Public hosted zone id |
| vpc_default_sg | Default security group |
| vpc_id | VPC id |
| vpc_private_routing_table_ids | Private routing table id |
| vpc_public_routing_table_id | Public routing table id |

28 changes: 14 additions & 14 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
locals {
outputs = {
public_subnets = var.enable ? aws_subnet.public.*.id : []
private_subnets = var.enable ? aws_subnet.private.*.id : []
vpc_id = var.enable ? aws_vpc.vpc[0].id : ""
vpc_default_sg = var.enable ? aws_default_security_group.vpc-default-sg[0].id : ""
net0ps_zone_id = var.enable ? aws_route53_zone.net0ps[0].zone_id : ""
subdomain_zone_id = var.enable && var.subdomain != "" ? aws_route53_zone.subdomain[0].zone_id : ""
vpc_private_routing_table_id = var.enable ? aws_route_table.private[0].id : ""
vpc_public_routing_table_id = var.enable ? aws_route_table.public[0].id : ""
private_subdomain = var.enable ? aws_route53_zone.net0ps[0].name : ""
depends_id = var.enable ? null_resource.dummy_dependency[0].id : ""
nat_gateway_ids = var.enable ? aws_nat_gateway.nat.*.id : []
elastic_ips = var.enable && length(var.external_elastic_ips) > 0 ? var.external_elastic_ips : var.enable ? aws_eip.nat.*.id : []
public_subnets = var.enable ? aws_subnet.public.*.id : []
private_subnets = var.enable ? aws_subnet.private.*.id : []
vpc_id = var.enable ? aws_vpc.vpc[0].id : ""
vpc_default_sg = var.enable ? aws_default_security_group.vpc-default-sg[0].id : ""
net0ps_zone_id = var.enable ? aws_route53_zone.net0ps[0].zone_id : ""
subdomain_zone_id = var.enable && var.subdomain != "" ? aws_route53_zone.subdomain[0].zone_id : ""
vpc_private_routing_table_ids = var.enable ? aws_route_table.private.*.id : []
vpc_public_routing_table_id = var.enable ? aws_route_table.public[0].id : ""
private_subdomain = var.enable ? aws_route53_zone.net0ps[0].name : ""
depends_id = var.enable ? null_resource.dummy_dependency[0].id : ""
nat_gateway_ids = var.enable ? aws_nat_gateway.nat.*.id : []
elastic_ips = var.enable && length(var.external_elastic_ips) > 0 ? var.external_elastic_ips : var.enable ? aws_eip.nat.*.id : []
}
}

Expand Down Expand Up @@ -75,8 +75,8 @@ output "nat_gateway_ids" {
description = "NAT gateway ids"
}

output "vpc_private_routing_table_id" {
value = local.outputs.vpc_private_routing_table_id
output "vpc_private_routing_table_ids" {
value = local.outputs.vpc_private_routing_table_ids
description = "Private routing table id"
}

Expand Down
31 changes: 26 additions & 5 deletions test/vpc_localstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestVPCApplyEnabled_basic(t *testing.T) {
terraform.InitAndApply(t, terraformOptions)
ValidateTerraformModuleOutputs(t, terraformOptions)
ValidateNATGateways(t, terraformOptions, 1)
ValidatePrivateRoutingTables(t, terraformOptions, 1)
ValidateElasticIps(t, terraformOptions, 1)
}

Expand Down Expand Up @@ -93,6 +94,7 @@ func TestVPCApplyEnabled_twoAvailabilityZones(t *testing.T) {
terraform.InitAndApply(t, terraformOptions)
ValidateTerraformModuleOutputs(t, terraformOptions)
ValidateNATGateways(t, terraformOptions, 1)
ValidatePrivateRoutingTables(t, terraformOptions, 1)
ValidateElasticIps(t, terraformOptions, 1)
}

Expand Down Expand Up @@ -135,6 +137,7 @@ func TestVPCApplyEnabled_differentSubnetConfigurations(t *testing.T) {

ValidateTerraformModuleOutputs(t, terraformOptions)
ValidateNATGateways(t, terraformOptions, 1)
ValidatePrivateRoutingTables(t, terraformOptions, 1)
ValidateElasticIps(t, terraformOptions, 1)
}

Expand Down Expand Up @@ -177,6 +180,7 @@ func TestVPCApplyEnabled_noPublicSubdomain(t *testing.T) {

ValidateTerraformModuleOutputs(t, terraformOptions)
ValidateNATGateways(t, terraformOptions, 1)
ValidatePrivateRoutingTables(t, terraformOptions, 1)
ValidateElasticIps(t, terraformOptions, 1)
}

Expand Down Expand Up @@ -219,6 +223,7 @@ func TestVPCApplyEnabled_natPerAZ(t *testing.T) {

ValidateTerraformModuleOutputs(t, terraformOptions)
ValidateNATGateways(t, terraformOptions, 3)
ValidatePrivateRoutingTables(t, terraformOptions, 3)
ValidateElasticIps(t, terraformOptions, 3)
}

Expand Down Expand Up @@ -261,6 +266,7 @@ func TestVPCApplyEnabled_natPerAZInTwoAZ(t *testing.T) {

ValidateTerraformModuleOutputs(t, terraformOptions)
ValidateNATGateways(t, terraformOptions, 2)
ValidatePrivateRoutingTables(t, terraformOptions, 2)
ValidateElasticIps(t, terraformOptions, 2)
}

Expand Down Expand Up @@ -307,6 +313,7 @@ func TestVPCApplyEnabled_externalElasticIPsNatPerAZ(t *testing.T) {

ValidateTerraformModuleOutputs(t, terraformOptions)
ValidateNATGateways(t, terraformOptions, 3)
ValidatePrivateRoutingTables(t, terraformOptions, 3)
ValidateElasticIps(t, terraformOptions, 5)
ValidateExternalElasticIPs(t, terraformOptions)
}
Expand Down Expand Up @@ -354,6 +361,7 @@ func TestVPCApplyEnabled_externalElasticIPsLessThanDesiredNATCount(t *testing.T)

ValidateTerraformModuleOutputs(t, terraformOptions)
ValidateNATGateways(t, terraformOptions, 1)
ValidatePrivateRoutingTables(t, terraformOptions, 1)
ValidateElasticIps(t, terraformOptions, 1)
ValidateExternalElasticIPs(t, terraformOptions)
}
Expand Down Expand Up @@ -401,6 +409,7 @@ func TestVPCApplyEnabled_externalElasticIPsSingleNAT(t *testing.T) {

ValidateTerraformModuleOutputs(t, terraformOptions)
ValidateNATGateways(t, terraformOptions, 1)
ValidatePrivateRoutingTables(t, terraformOptions, 1)
ValidateElasticIps(t, terraformOptions, 5)
ValidateExternalElasticIPs(t, terraformOptions)
}
Expand Down Expand Up @@ -503,17 +512,26 @@ func ValidateTerraformModuleOutputs(t *testing.T, terraformOptions *terraform.Op

func ValidateNATGateways(t *testing.T, terraformOptions *terraform.Options, expectedNumberOfResources int) {
nat_gateway_ids := terraform.OutputList(t, terraformOptions, "nat_gateway_ids")
require.Len(t, nat_gateway_ids, expectedNumberOfResources)
ValidateCount(t, nat_gateway_ids, expectedNumberOfResources)
ValidateEachElementInArray(t, nat_gateway_ids, "nat-*")
}

func ValidateElasticIps(t *testing.T, terraformOptions *terraform.Options, expectedNumberOfResources int) {
elastic_ips := terraform.OutputList(t, terraformOptions, "elastic_ips")
require.Len(t, elastic_ips, expectedNumberOfResources)
ValidateCount(t, elastic_ips, expectedNumberOfResources)
ValidateEachElementInArray(t, elastic_ips, "eip-*")
}

func ValidatePrivateRoutingTables(t *testing.T, terraformOptions *terraform.Options, expectedNumberOfResources int) {
vpc_private_routing_table_ids := terraform.OutputList(t, terraformOptions, "vpc_private_routing_table_ids")
ValidateCount(t, vpc_private_routing_table_ids, expectedNumberOfResources)
ValidateEachElementInArray(t, vpc_private_routing_table_ids, "rtb-*")
}

func ValidateEachElementInArray(t *testing.T, array []string, regularExpression string) {

require.Greater(t, len(array), 0)

for _, element := range array {
require.Regexp(t, regularExpression, element)
}
Expand Down Expand Up @@ -573,12 +591,15 @@ func ValidateVPCRoute53ZoneName(t *testing.T, terraformOptions *terraform.Option
}

func ValidateVPCRoutingTables(t *testing.T, terraformOptions *terraform.Options) {
vpc_private_routing_table_id := terraform.Output(t, terraformOptions, "vpc_private_routing_table_id")
vpc_private_routing_table_ids := terraform.OutputList(t, terraformOptions, "vpc_private_routing_table_ids")
vpc_public_routing_table_id := terraform.Output(t, terraformOptions, "vpc_public_routing_table_id")

require.Regexp(t, "rtb-*", vpc_private_routing_table_id)
ValidateEachElementInArray(t, vpc_private_routing_table_ids, "rtb-*")
require.Regexp(t, "rtb-*", vpc_public_routing_table_id)
require.NotEqual(t, vpc_private_routing_table_id, vpc_public_routing_table_id)
}

func ValidateCount(t *testing.T, array []string, expectedCount int) {
require.Len(t, array, expectedCount)
}

func ValidateDependId(t *testing.T, terraformOptions *terraform.Options) {
Expand Down

0 comments on commit 09750d5

Please sign in to comment.