Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tags override for all resources (fix for #138) #145

Merged
merged 2 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ Terraform version 0.10.3 or newer is required for this module to work.
| cidr | The CIDR block for the VPC. Default value is a valid CIDR, but not acceptable by AWS and should be overriden | string | `0.0.0.0/0` | no |
| create_database_subnet_group | Controls if database subnet group should be created | string | `true` | no |
| create_vpc | Controls if VPC should be created (it affects almost all resources) | string | `true` | no |
| database_subnet_group_tags | Additional tags for the database subnet group | string | `<map>` | no |
| database_subnet_tags | Additional tags for the database subnets | string | `<map>` | no |
| database_subnets | A list of database subnets | list | `<list>` | no |
| default_route_table_tags | Additional tags for the default route table | string | `<map>` | no |
Expand All @@ -191,14 +192,16 @@ Terraform version 0.10.3 or newer is required for this module to work.
| enable_s3_endpoint | Should be true if you want to provision an S3 endpoint to the VPC | string | `false` | no |
| enable_vpn_gateway | Should be true if you want to create a new VPN Gateway resource and attach it to the VPC | string | `false` | no |
| external_nat_ip_ids | List of EIP IDs to be assigned to the NAT Gateways (used in combination with reuse_nat_ips) | list | `<list>` | no |
| igw_tags | Additional tags for the internet gateway | string | `<map>` | no |
| instance_tenancy | A tenancy option for instances launched into the VPC | string | `default` | no |
| intra_route_table_tags | Additional tags for the intra route tables | string | `<map>` | no |
| intra_subnet_tags | Additional tags for the intra subnets | string | `<map>` | no |
| intra_subnets | A list of intra subnets | list | `<list>` | no |
| manage_default_vpc | Should be true to adopt and manage Default VPC | string | `false` | no |
| map_public_ip_on_launch | Should be false if you do not want to auto-assign public IP on launch | string | `true` | no |
| name | Name to be used on all the resources as identifier | string | `` | no |
| nat_gateway_tags | Additional tags for the nat gateways | string | `<map>` | no |
| nat_eip_tags | Additional tags for the NAT EIP | string | `<map>` | no |
| nat_gateway_tags | Additional tags for the NAT gateways | string | `<map>` | no |
| one_nat_gateway_per_az | Should be true if you want only one NAT Gateway per availability zone. Requires `var.azs` to be set, and the number of `public_subnets` created to be greater than or equal to the number of availability zones specified in `var.azs`. | string | `false` | no |
| private_route_table_tags | Additional tags for the private route tables | string | `<map>` | no |
| private_subnet_tags | Additional tags for the private subnets | string | `<map>` | no |
Expand All @@ -208,13 +211,15 @@ Terraform version 0.10.3 or newer is required for this module to work.
| public_route_table_tags | Additional tags for the public route tables | string | `<map>` | no |
| public_subnet_tags | Additional tags for the public subnets | string | `<map>` | no |
| public_subnets | A list of public subnets inside the VPC | string | `<list>` | no |
| redshift_subnet_group_tags | Additional tags for the redshift subnet group | string | `<map>` | no |
| redshift_subnet_tags | Additional tags for the redshift subnets | string | `<map>` | no |
| redshift_subnets | A list of redshift subnets | list | `<list>` | no |
| reuse_nat_ips | Should be true if you don't want EIPs to be created for your NAT Gateways and will instead pass them in via the 'external_nat_ip_ids' variable | string | `false` | no |
| single_nat_gateway | Should be true if you want to provision a single shared NAT Gateway across all of your private networks | string | `false` | no |
| tags | A map of tags to add to all resources | string | `<map>` | no |
| vpc_tags | Additional tags for the VPC | string | `<map>` | no |
| vpn_gateway_id | ID of VPN Gateway to attach to the VPC | string | `` | no |
| vpn_gateway_tags | Additional tags for the VPN gateway | string | `<map>` | no |

## Outputs

Expand Down
8 changes: 8 additions & 0 deletions examples/simple-vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ module "vpc" {
enable_nat_gateway = true
single_nat_gateway = true

public_subnet_tags = {
Name = "overriden-name-public"
}

tags = {
Owner = "user"
Environment = "dev"
}

vpc_tags = {
Name = "vpc-name"
}
}
38 changes: 19 additions & 19 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ resource "aws_vpc" "this" {
enable_dns_hostnames = "${var.enable_dns_hostnames}"
enable_dns_support = "${var.enable_dns_support}"

tags = "${merge(var.tags, var.vpc_tags, map("Name", format("%s", var.name)))}"
tags = "${merge(map("Name", format("%s", var.name)), var.vpc_tags, var.tags)}"
}

###################
Expand All @@ -33,7 +33,7 @@ resource "aws_vpc_dhcp_options" "this" {
netbios_name_servers = ["${var.dhcp_options_netbios_name_servers}"]
netbios_node_type = "${var.dhcp_options_netbios_node_type}"

tags = "${merge(var.tags, var.dhcp_options_tags, map("Name", format("%s", var.name)))}"
tags = "${merge(map("Name", format("%s", var.name)), var.dhcp_options_tags, var.tags)}"
}

###############################
Expand All @@ -54,7 +54,7 @@ resource "aws_internet_gateway" "this" {

vpc_id = "${aws_vpc.this.id}"

tags = "${merge(var.tags, map("Name", format("%s", var.name)))}"
tags = "${merge(map("Name", format("%s", var.name)), var.igw_tags, var.tags)}"
}

################
Expand All @@ -65,7 +65,7 @@ resource "aws_route_table" "public" {

vpc_id = "${aws_vpc.this.id}"

tags = "${merge(var.tags, var.public_route_table_tags, map("Name", format("%s-public", var.name)))}"
tags = "${merge(map("Name", format("%s-public", var.name)), var.public_route_table_tags, var.tags)}"
}

resource "aws_route" "public_internet_gateway" {
Expand All @@ -89,7 +89,7 @@ resource "aws_route_table" "private" {

vpc_id = "${aws_vpc.this.id}"

tags = "${merge(var.tags, var.private_route_table_tags, map("Name", (var.single_nat_gateway ? "${var.name}-private" : format("%s-private-%s", var.name, element(var.azs, count.index)))))}"
tags = "${merge(map("Name", (var.single_nat_gateway ? "${var.name}-private" : format("%s-private-%s", var.name, element(var.azs, count.index)))), var.private_route_table_tags, var.tags)}"

lifecycle {
# When attaching VPN gateways it is common to define aws_vpn_gateway_route_propagation
Expand All @@ -106,7 +106,7 @@ resource "aws_route_table" "intra" {

vpc_id = "${aws_vpc.this.id}"

tags = "${merge(var.tags, var.intra_route_table_tags, map("Name", "${var.name}-intra"))}"
tags = "${merge(map("Name", "${var.name}-intra"), var.intra_route_table_tags, var.tags)}"
}

################
Expand All @@ -120,7 +120,7 @@ resource "aws_subnet" "public" {
availability_zone = "${element(var.azs, count.index)}"
map_public_ip_on_launch = "${var.map_public_ip_on_launch}"

tags = "${merge(var.tags, var.public_subnet_tags, map("Name", format("%s-public-%s", var.name, element(var.azs, count.index))))}"
tags = "${merge(map("Name", format("%s-public-%s", var.name, element(var.azs, count.index))), var.public_subnet_tags, var.tags)}"
}

#################
Expand All @@ -133,7 +133,7 @@ resource "aws_subnet" "private" {
cidr_block = "${var.private_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"

tags = "${merge(var.tags, var.private_subnet_tags, map("Name", format("%s-private-%s", var.name, element(var.azs, count.index))))}"
tags = "${merge(map("Name", format("%s-private-%s", var.name, element(var.azs, count.index))), var.private_subnet_tags, var.tags)}"
}

##################
Expand All @@ -146,7 +146,7 @@ resource "aws_subnet" "database" {
cidr_block = "${var.database_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"

tags = "${merge(var.tags, var.database_subnet_tags, map("Name", format("%s-db-%s", var.name, element(var.azs, count.index))))}"
tags = "${merge(map("Name", format("%s-db-%s", var.name, element(var.azs, count.index))), var.database_subnet_tags, var.tags)}"
}

resource "aws_db_subnet_group" "database" {
Expand All @@ -156,7 +156,7 @@ resource "aws_db_subnet_group" "database" {
description = "Database subnet group for ${var.name}"
subnet_ids = ["${aws_subnet.database.*.id}"]

tags = "${merge(var.tags, map("Name", format("%s", var.name)))}"
tags = "${merge(map("Name", format("%s", var.name)), var.database_subnet_group_tags, var.tags)}"
}

##################
Expand All @@ -169,7 +169,7 @@ resource "aws_subnet" "redshift" {
cidr_block = "${var.redshift_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"

tags = "${merge(var.tags, var.redshift_subnet_tags, map("Name", format("%s-redshift-%s", var.name, element(var.azs, count.index))))}"
tags = "${merge(map("Name", format("%s-redshift-%s", var.name, element(var.azs, count.index))), var.redshift_subnet_tags, var.tags)}"
}

resource "aws_redshift_subnet_group" "redshift" {
Expand All @@ -179,7 +179,7 @@ resource "aws_redshift_subnet_group" "redshift" {
description = "Redshift subnet group for ${var.name}"
subnet_ids = ["${aws_subnet.redshift.*.id}"]

tags = "${merge(var.tags, map("Name", format("%s", var.name)))}"
tags = "${merge(map("Name", format("%s", var.name)), var.redshift_subnet_group_tags, var.tags)}"
}

#####################
Expand All @@ -192,7 +192,7 @@ resource "aws_subnet" "elasticache" {
cidr_block = "${var.elasticache_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"

tags = "${merge(var.tags, var.elasticache_subnet_tags, map("Name", format("%s-elasticache-%s", var.name, element(var.azs, count.index))))}"
tags = "${merge(map("Name", format("%s-elasticache-%s", var.name, element(var.azs, count.index))), var.elasticache_subnet_tags, var.tags)}"
}

resource "aws_elasticache_subnet_group" "elasticache" {
Expand All @@ -204,7 +204,7 @@ resource "aws_elasticache_subnet_group" "elasticache" {
}

#####################################################
# intra subnets - private subnet with no NAT gateway
# intra subnets - private subnet without NAT gateway
#####################################################
resource "aws_subnet" "intra" {
count = "${var.create_vpc && length(var.intra_subnets) > 0 ? length(var.intra_subnets) : 0}"
Expand All @@ -213,7 +213,7 @@ resource "aws_subnet" "intra" {
cidr_block = "${var.intra_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"

tags = "${merge(var.tags, var.intra_subnet_tags, map("Name", format("%s-intra-%s", var.name, element(var.azs, count.index))))}"
tags = "${merge(map("Name", format("%s-intra-%s", var.name, element(var.azs, count.index))), var.intra_subnet_tags, var.tags)}"
}

##############
Expand All @@ -236,7 +236,7 @@ resource "aws_eip" "nat" {

vpc = true

tags = "${merge(var.tags, map("Name", format("%s-%s", var.name, element(var.azs, (var.single_nat_gateway ? 0 : count.index)))))}"
tags = "${merge(map("Name", format("%s-%s", var.name, element(var.azs, (var.single_nat_gateway ? 0 : count.index)))), var.nat_eip_tags, var.tags)}"
}

resource "aws_nat_gateway" "this" {
Expand All @@ -245,7 +245,7 @@ resource "aws_nat_gateway" "this" {
allocation_id = "${element(local.nat_gateway_ips, (var.single_nat_gateway ? 0 : count.index))}"
subnet_id = "${element(aws_subnet.public.*.id, (var.single_nat_gateway ? 0 : count.index))}"

tags = "${merge(var.tags, var.nat_gateway_tags, map("Name", format("%s-%s", var.name, element(var.azs, (var.single_nat_gateway ? 0 : count.index)))))}"
tags = "${merge(map("Name", format("%s-%s", var.name, element(var.azs, (var.single_nat_gateway ? 0 : count.index)))), var.nat_gateway_tags, var.tags)}"

depends_on = ["aws_internet_gateway.this"]
}
Expand Down Expand Up @@ -389,7 +389,7 @@ resource "aws_vpn_gateway" "this" {

vpc_id = "${aws_vpc.this.id}"

tags = "${merge(var.tags, map("Name", format("%s", var.name)))}"
tags = "${merge(map("Name", format("%s", var.name)), var.vpn_gateway_tags, var.tags)}"
}

resource "aws_vpn_gateway_attachment" "this" {
Expand Down Expand Up @@ -423,5 +423,5 @@ resource "aws_default_vpc" "this" {
enable_dns_hostnames = "${var.default_vpc_enable_dns_hostnames}"
enable_classiclink = "${var.default_vpc_enable_classiclink}"

tags = "${merge(var.tags, var.default_vpc_tags, map("Name", format("%s", var.default_vpc_name)))}"
tags = "${merge(map("Name", format("%s", var.default_vpc_name)), var.default_vpc_tags, var.tags)}"
}
27 changes: 26 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ variable "vpc_tags" {
default = {}
}

variable "igw_tags" {
description = "Additional tags for the internet gateway"
default = {}
}

variable "public_subnet_tags" {
description = "Additional tags for the public subnets"
default = {}
Expand Down Expand Up @@ -178,11 +183,21 @@ variable "database_subnet_tags" {
default = {}
}

variable "database_subnet_group_tags" {
description = "Additional tags for the database subnet group"
default = {}
}

variable "redshift_subnet_tags" {
description = "Additional tags for the redshift subnets"
default = {}
}

variable "redshift_subnet_group_tags" {
description = "Additional tags for the redshift subnet group"
default = {}
}

variable "elasticache_subnet_tags" {
description = "Additional tags for the elasticache subnets"
default = {}
Expand All @@ -199,7 +214,17 @@ variable "dhcp_options_tags" {
}

variable "nat_gateway_tags" {
description = "Additional tags for the nat gateways"
description = "Additional tags for the NAT gateways"
default = {}
}

variable "nat_eip_tags" {
description = "Additional tags for the NAT EIP"
default = {}
}

variable "vpn_gateway_tags" {
description = "Additional tags for the VPN gateway"
default = {}
}

Expand Down