Skip to content

Commit 8529a70

Browse files
authored
Allow tags override for all resources (fix for terraform-aws-modules#138) (terraform-aws-modules#145)
* Allow tags override for all resources (fix for terraform-aws-modules#138) * Added tags for all resources which were missing them
1 parent 646b282 commit 8529a70

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ Terraform version 0.10.3 or newer is required for this module to work.
167167
| 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 |
168168
| create_database_subnet_group | Controls if database subnet group should be created | string | `true` | no |
169169
| create_vpc | Controls if VPC should be created (it affects almost all resources) | string | `true` | no |
170+
| database_subnet_group_tags | Additional tags for the database subnet group | string | `<map>` | no |
170171
| database_subnet_tags | Additional tags for the database subnets | string | `<map>` | no |
171172
| database_subnets | A list of database subnets | list | `<list>` | no |
172173
| default_route_table_tags | Additional tags for the default route table | string | `<map>` | no |
@@ -191,14 +192,16 @@ Terraform version 0.10.3 or newer is required for this module to work.
191192
| enable_s3_endpoint | Should be true if you want to provision an S3 endpoint to the VPC | string | `false` | no |
192193
| 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 |
193194
| 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 |
195+
| igw_tags | Additional tags for the internet gateway | string | `<map>` | no |
194196
| instance_tenancy | A tenancy option for instances launched into the VPC | string | `default` | no |
195197
| intra_route_table_tags | Additional tags for the intra route tables | string | `<map>` | no |
196198
| intra_subnet_tags | Additional tags for the intra subnets | string | `<map>` | no |
197199
| intra_subnets | A list of intra subnets | list | `<list>` | no |
198200
| manage_default_vpc | Should be true to adopt and manage Default VPC | string | `false` | no |
199201
| map_public_ip_on_launch | Should be false if you do not want to auto-assign public IP on launch | string | `true` | no |
200202
| name | Name to be used on all the resources as identifier | string | `` | no |
201-
| nat_gateway_tags | Additional tags for the nat gateways | string | `<map>` | no |
203+
| nat_eip_tags | Additional tags for the NAT EIP | string | `<map>` | no |
204+
| nat_gateway_tags | Additional tags for the NAT gateways | string | `<map>` | no |
202205
| 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 |
203206
| private_route_table_tags | Additional tags for the private route tables | string | `<map>` | no |
204207
| private_subnet_tags | Additional tags for the private subnets | string | `<map>` | no |
@@ -208,13 +211,15 @@ Terraform version 0.10.3 or newer is required for this module to work.
208211
| public_route_table_tags | Additional tags for the public route tables | string | `<map>` | no |
209212
| public_subnet_tags | Additional tags for the public subnets | string | `<map>` | no |
210213
| public_subnets | A list of public subnets inside the VPC | string | `<list>` | no |
214+
| redshift_subnet_group_tags | Additional tags for the redshift subnet group | string | `<map>` | no |
211215
| redshift_subnet_tags | Additional tags for the redshift subnets | string | `<map>` | no |
212216
| redshift_subnets | A list of redshift subnets | list | `<list>` | no |
213217
| 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 |
214218
| 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 |
215219
| tags | A map of tags to add to all resources | string | `<map>` | no |
216220
| vpc_tags | Additional tags for the VPC | string | `<map>` | no |
217221
| vpn_gateway_id | ID of VPN Gateway to attach to the VPC | string | `` | no |
222+
| vpn_gateway_tags | Additional tags for the VPN gateway | string | `<map>` | no |
218223

219224
## Outputs
220225

examples/simple-vpc/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ module "vpc" {
1616
enable_nat_gateway = true
1717
single_nat_gateway = true
1818

19+
public_subnet_tags = {
20+
Name = "overriden-name-public"
21+
}
22+
1923
tags = {
2024
Owner = "user"
2125
Environment = "dev"
2226
}
27+
28+
vpc_tags = {
29+
Name = "vpc-name"
30+
}
2331
}

main.tf

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ resource "aws_vpc" "this" {
1818
enable_dns_hostnames = "${var.enable_dns_hostnames}"
1919
enable_dns_support = "${var.enable_dns_support}"
2020

21-
tags = "${merge(var.tags, var.vpc_tags, map("Name", format("%s", var.name)))}"
21+
tags = "${merge(map("Name", format("%s", var.name)), var.vpc_tags, var.tags)}"
2222
}
2323

2424
###################
@@ -33,7 +33,7 @@ resource "aws_vpc_dhcp_options" "this" {
3333
netbios_name_servers = ["${var.dhcp_options_netbios_name_servers}"]
3434
netbios_node_type = "${var.dhcp_options_netbios_node_type}"
3535

36-
tags = "${merge(var.tags, var.dhcp_options_tags, map("Name", format("%s", var.name)))}"
36+
tags = "${merge(map("Name", format("%s", var.name)), var.dhcp_options_tags, var.tags)}"
3737
}
3838

3939
###############################
@@ -54,7 +54,7 @@ resource "aws_internet_gateway" "this" {
5454

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

57-
tags = "${merge(var.tags, map("Name", format("%s", var.name)))}"
57+
tags = "${merge(map("Name", format("%s", var.name)), var.igw_tags, var.tags)}"
5858
}
5959

6060
################
@@ -65,7 +65,7 @@ resource "aws_route_table" "public" {
6565

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

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

7171
resource "aws_route" "public_internet_gateway" {
@@ -89,7 +89,7 @@ resource "aws_route_table" "private" {
8989

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

92-
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)))))}"
92+
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)}"
9393

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

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

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

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

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

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

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

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

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

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

159-
tags = "${merge(var.tags, map("Name", format("%s", var.name)))}"
159+
tags = "${merge(map("Name", format("%s", var.name)), var.database_subnet_group_tags, var.tags)}"
160160
}
161161

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

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

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

182-
tags = "${merge(var.tags, map("Name", format("%s", var.name)))}"
182+
tags = "${merge(map("Name", format("%s", var.name)), var.redshift_subnet_group_tags, var.tags)}"
183183
}
184184

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

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

198198
resource "aws_elasticache_subnet_group" "elasticache" {
@@ -204,7 +204,7 @@ resource "aws_elasticache_subnet_group" "elasticache" {
204204
}
205205

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

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

219219
##############
@@ -236,7 +236,7 @@ resource "aws_eip" "nat" {
236236

237237
vpc = true
238238

239-
tags = "${merge(var.tags, map("Name", format("%s-%s", var.name, element(var.azs, (var.single_nat_gateway ? 0 : count.index)))))}"
239+
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)}"
240240
}
241241

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

248-
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)))))}"
248+
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)}"
249249

250250
depends_on = ["aws_internet_gateway.this"]
251251
}
@@ -389,7 +389,7 @@ resource "aws_vpn_gateway" "this" {
389389

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

392-
tags = "${merge(var.tags, map("Name", format("%s", var.name)))}"
392+
tags = "${merge(map("Name", format("%s", var.name)), var.vpn_gateway_tags, var.tags)}"
393393
}
394394

395395
resource "aws_vpn_gateway_attachment" "this" {
@@ -423,5 +423,5 @@ resource "aws_default_vpc" "this" {
423423
enable_dns_hostnames = "${var.default_vpc_enable_dns_hostnames}"
424424
enable_classiclink = "${var.default_vpc_enable_classiclink}"
425425

426-
tags = "${merge(var.tags, var.default_vpc_tags, map("Name", format("%s", var.default_vpc_name)))}"
426+
tags = "${merge(map("Name", format("%s", var.default_vpc_name)), var.default_vpc_tags, var.tags)}"
427427
}

variables.tf

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ variable "vpc_tags" {
143143
default = {}
144144
}
145145

146+
variable "igw_tags" {
147+
description = "Additional tags for the internet gateway"
148+
default = {}
149+
}
150+
146151
variable "public_subnet_tags" {
147152
description = "Additional tags for the public subnets"
148153
default = {}
@@ -178,11 +183,21 @@ variable "database_subnet_tags" {
178183
default = {}
179184
}
180185

186+
variable "database_subnet_group_tags" {
187+
description = "Additional tags for the database subnet group"
188+
default = {}
189+
}
190+
181191
variable "redshift_subnet_tags" {
182192
description = "Additional tags for the redshift subnets"
183193
default = {}
184194
}
185195

196+
variable "redshift_subnet_group_tags" {
197+
description = "Additional tags for the redshift subnet group"
198+
default = {}
199+
}
200+
186201
variable "elasticache_subnet_tags" {
187202
description = "Additional tags for the elasticache subnets"
188203
default = {}
@@ -199,7 +214,17 @@ variable "dhcp_options_tags" {
199214
}
200215

201216
variable "nat_gateway_tags" {
202-
description = "Additional tags for the nat gateways"
217+
description = "Additional tags for the NAT gateways"
218+
default = {}
219+
}
220+
221+
variable "nat_eip_tags" {
222+
description = "Additional tags for the NAT EIP"
223+
default = {}
224+
}
225+
226+
variable "vpn_gateway_tags" {
227+
description = "Additional tags for the VPN gateway"
203228
default = {}
204229
}
205230

0 commit comments

Comments
 (0)