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

Commit

Permalink
Add Tags (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Puneeth-n authored Jun 16, 2020
1 parent bd67ab0 commit 522ea0f
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 49 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ MIT Licensed. See [LICENSE](LICENSE) for full details.
| enable\_dns\_hostnames | Enable DNS hostnames | `bool` | `true` | no |
| enable\_dns\_support | Enable DNS support | `bool` | `true` | no |
| nat\_az\_number | Subnet number to deploy NAT gateway in | `number` | `0` | no |
| private\_subnet\_tags | n/a | `map` | `{}` | no |
| public\_subnet\_tags | n/a | `map` | `{}` | no |
| subdomain | Subdomain name | `string` | `""` | no |
| tags | n/a | `map` | `{}` | no |

## Outputs

Expand Down
131 changes: 82 additions & 49 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ resource "aws_vpc" "vpc" {
enable_dns_hostnames = var.enable_dns_hostnames
assign_generated_ipv6_cidr_block = false

tags = {
Name = "${var.vpc_name}-vpc"
environment = var.environment
depends_id = var.depends_id
}
tags = merge(
var.tags,
{
Name = "${var.vpc_name}-vpc"
environment = var.environment
depends_id = var.depends_id
}
)
}

resource "aws_route53_zone" "net0ps" {
Expand All @@ -41,32 +44,41 @@ resource "aws_route53_zone" "net0ps" {

comment = "Private hosted zone for ${var.environment}"

tags = {
Name = "${var.vpc_name}-private-zone"
environment = var.environment
}
tags = merge(
var.tags,
{
Name = "${var.vpc_name}-private-zone"
environment = var.environment
}
)
}

resource "aws_route53_zone" "subdomain" {
count = var.enable && var.subdomain != "" ? 1 : 0
name = var.subdomain
comment = "Public hosted zone for ${var.environment} subdomain"

tags = {
Name = "${var.vpc_name}-public-zone"
environment = var.environment
}
tags = merge(
var.tags,
{
Name = "${var.vpc_name}-public-zone"
environment = var.environment
}
)
}

# Internet gateway
resource "aws_internet_gateway" "igw" {
count = local.enable_count
vpc_id = aws_vpc.vpc[0].id

tags = {
Name = "${var.vpc_name}-igw"
environment = var.environment
}
tags = merge(
var.tags,
{
Name = "${var.vpc_name}-igw"
environment = var.environment
}
)
}

# NAT gateway
Expand All @@ -80,10 +92,13 @@ resource "aws_nat_gateway" "nat" {
aws_eip.nat,
]

tags = {
Name = "${var.vpc_name}-nat-gateway"
environment = var.environment
}
tags = merge(
var.tags,
{
Name = "${var.vpc_name}-nat-gateway"
environment = var.environment
}
)
}

# Elastic IP for NAT
Expand Down Expand Up @@ -115,21 +130,27 @@ resource "aws_default_network_acl" "acl" {
to_port = 0
}

tags = {
Name = "${var.vpc_name}-acl"
environment = var.environment
}
tags = merge(
var.tags,
{
Name = "${var.vpc_name}-acl"
environment = var.environment
}
)
}

resource "aws_default_route_table" "private" {
count = local.enable_count
default_route_table_id = aws_vpc.vpc[0].default_route_table_id

tags = {
Name = "${var.vpc_name}-private-rt"
environment = var.environment
depends_id = var.depends_id
}
tags = merge(
var.tags,
{
Name = "${var.vpc_name}-private-rt"
environment = var.environment
depends_id = var.depends_id
}
)
}

resource "aws_route" "private-nat" {
Expand All @@ -145,11 +166,14 @@ resource "aws_route_table" "public" {
count = local.enable_count
vpc_id = aws_vpc.vpc[0].id

tags = {
Name = "${var.vpc_name}-public-rt"
environment = var.environment
depends_id = var.depends_id
}
tags = merge(
var.tags,
{
Name = "${var.vpc_name}-public-rt"
environment = var.environment
depends_id = var.depends_id
}
)
}

resource "aws_route" "public-igw" {
Expand All @@ -167,11 +191,14 @@ resource "aws_subnet" "public" {
availability_zone = element(var.azs, count.index)
map_public_ip_on_launch = true

tags = {
Name = "${var.vpc_name}-public-subnet"
environment = var.environment
az = element(var.azs, count.index)
}
tags = merge(
var.public_subnet_tags,
{
Name = "${var.vpc_name}-public-subnet"
environment = var.environment
az = element(var.azs, count.index)
}
)
}

resource "aws_subnet" "private" {
Expand All @@ -181,11 +208,14 @@ resource "aws_subnet" "private" {
availability_zone = element(var.azs, count.index)
map_public_ip_on_launch = false

tags = {
Name = "${var.vpc_name}-private-subnet"
environment = var.environment
az = element(var.azs, count.index)
}
tags = merge(
var.private_subnet_tags,
{
Name = "${var.vpc_name}-private-subnet"
environment = var.environment
az = element(var.azs, count.index)
}
)
}

resource "aws_route_table_association" "public" {
Expand Down Expand Up @@ -219,10 +249,13 @@ resource "aws_default_security_group" "vpc-default-sg" {
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "${var.vpc_name}-default-sg"
environment = var.environment
}
tags = merge(
var.tags,
{
Name = "${var.vpc_name}-default-sg"
environment = var.environment
}
)
}

resource "null_resource" "dummy_dependency" {
Expand Down
35 changes: 35 additions & 0 deletions tests/vpc_localstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,41 @@ func TestVPCApplyEnabledBasic(t *testing.T) {
ValidateTerraformModuleOutputs(t, terraformOptions)
}

func TestVPCApplyEnabledBasic_tags(t *testing.T) {
t.Parallel()

vpc_name := fmt.Sprintf("vpc_enabled-%s", random.UniqueId())

terraformModuleVars := map[string]interface{}{
"enable": true,
"vpc_name": vpc_name,
"subdomain": "foo.bar.baz",
"cidr": "10.10.0.0/16",
"azs": []string{"us-east-1a", "us-east-1b", "us-east-1c"},
"nat_az_number": 1,
"environment": vpc_name,
"replication_factor": 3,
"tags": map[string]string{
"kubernetes.io/cluster/foo": "shared",
},
"public_subnet_tags": map[string]string{
"kubernetes.io/cluster/foo": "shared",
"kubernetes.io/role/elb": "1",
},
"private_subnet_tags": map[string]string{
"kubernetes.io/cluster/foo": "shared",
"kubernetes.io/role/internal-elb": "1",
},
}

terraformOptions := SetupTestCase(t, terraformModuleVars)
t.Logf("Terraform module inputs: %+v", *terraformOptions)
// defer terraform.Destroy(t, terraformOptions)

TerraformApplyAndVerifyResourcesCreated(t, terraformOptions, 25)
ValidateTerraformModuleOutputs(t, terraformOptions)
}

func TestVPCApplyEnabledReplicationFactor(t *testing.T) {
t.Parallel()

Expand Down
15 changes: 15 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,18 @@ locals {
locals {
replication_count = local.enable_count * var.replication_factor
}

variable private_subnet_tags {
type = "map"
default = {}
}

variable public_subnet_tags {
type = "map"
default = {}
}

variable tags {
type = "map"
default = {}
}

0 comments on commit 522ea0f

Please sign in to comment.