Skip to content
This repository was archived by the owner on Jul 20, 2024. It is now read-only.

Commit 2efab55

Browse files
authored
Add ability to add tags (#12)
* feat: attempt to include tags * feat: update readme
1 parent 5698aa1 commit 2efab55

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ This is an open source software. Feel free to open issues and pull requests.
165165
| private\_subnets\_cidr\_blocks | List of CIDR blocks of the private subnets. The NAT instance accepts connections from this subnets | string | n/a | yes |
166166
| public\_subnet | ID of the public subnet to place the NAT instance | string | n/a | yes |
167167
| vpc\_id | ID of the VPC | string | n/a | yes |
168+
| tags | Tags to be applied to resources | map(string) | {} | no |
168169

169170
## Outputs
170171

main.tf

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ resource "aws_security_group" "this" {
22
name_prefix = var.name
33
vpc_id = var.vpc_id
44
description = "Security group for NAT instance ${var.name}"
5-
tags = {
6-
Name = "nat-instance-${var.name}"
7-
}
5+
tags = local.common_tags
86
}
97

108
resource "aws_security_group_rule" "egress" {
@@ -30,17 +28,13 @@ resource "aws_network_interface" "this" {
3028
subnet_id = var.public_subnet
3129
source_dest_check = false
3230
description = "ENI for NAT instance ${var.name}"
33-
tags = {
34-
Name = "nat-instance-${var.name}"
35-
}
31+
tags = local.common_tags
3632
}
3733

3834
resource "aws_eip" "this" {
3935
count = var.enabled ? 1 : 0
4036
network_interface = aws_network_interface.this.id
41-
tags = {
42-
Name = "nat-instance-${var.name}"
43-
}
37+
tags = local.common_tags
4438
}
4539

4640
resource "aws_route" "this" {
@@ -130,11 +124,7 @@ resource "aws_autoscaling_group" "this" {
130124
}
131125
}
132126

133-
tag {
134-
key = "Name"
135-
value = "nat-instance-${var.name}"
136-
propagate_at_launch = true
137-
}
127+
tags = local.asg_tags
138128

139129
lifecycle {
140130
create_before_destroy = true

variables.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,32 @@ variable "key_name" {
4747
description = "Name of the key pair for the NAT instance. You can set this to assign the key pair to the NAT instance"
4848
default = ""
4949
}
50+
51+
variable "tags" {
52+
description = "Tags applied to resources created with this module"
53+
default = {}
54+
}
55+
56+
locals {
57+
// Generate common tags by merging variables and default Name
58+
common_tags = merge(
59+
var.tags, {
60+
Name = "nat-instance-${var.name}"
61+
})
62+
63+
// Generate asg tags by merging variables in object format
64+
asg_tags = concat([
65+
for key, value in var.tags : {
66+
key = key
67+
value = value
68+
propagate_at_launch = true
69+
}
70+
], [
71+
{
72+
key = "Name"
73+
value = "nat-instance-${var.name}"
74+
propagate_at_launch = true
75+
}
76+
]
77+
)
78+
}

0 commit comments

Comments
 (0)