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

Add support for resource-specific tags #29

Merged
merged 3 commits into from
Sep 24, 2024
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
24 changes: 18 additions & 6 deletions availability-zone/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ locals {
resource "aws_route_table" "public" {
vpc_id = var.vpc.id

tags = merge({ Name = "public - ${local.availability_zone}" }, var.default_tags)
tags = merge({
Name = "public - ${local.availability_zone}"
}, var.default_tags, var.public_route_table_tags)
}

resource "aws_route" "internet_gateway" {
Expand All @@ -25,7 +27,9 @@ resource "aws_subnet" "public" {
availability_zone = local.availability_zone
map_public_ip_on_launch = true

tags = merge({ Name = "public - ${local.availability_zone}" }, var.default_tags)
tags = merge({
Name = "public - ${local.availability_zone}"
}, var.default_tags, var.public_subnet_tags)
}

resource "aws_route_table_association" "public" {
Expand All @@ -38,22 +42,28 @@ resource "aws_route_table_association" "public" {
resource "aws_eip" "this" {
domain = "vpc"

tags = merge({ Name = local.availability_zone }, var.default_tags)
tags = merge({
Name = local.availability_zone
}, var.default_tags, var.nat_gateway_eip_tags)
}

resource "aws_nat_gateway" "this" {
subnet_id = aws_subnet.public.id
allocation_id = aws_eip.this.id

tags = merge({ Name = local.availability_zone }, var.default_tags)
tags = merge({
Name = local.availability_zone
}, var.default_tags, var.nat_gateway_tags)
}

# Private Subnet

resource "aws_route_table" "private" {
vpc_id = var.vpc.id

tags = merge({ Name = "private - ${local.availability_zone}" }, var.default_tags)
tags = merge({
Name = "private - ${local.availability_zone}"
}, var.default_tags, var.private_route_table_tags)
}

resource "aws_route" "nat_gateway" {
Expand All @@ -69,7 +79,9 @@ resource "aws_subnet" "private" {
availability_zone = local.availability_zone
map_public_ip_on_launch = false

tags = merge({ Name = "private - ${local.availability_zone}" }, var.default_tags)
tags = merge({
Name = "private - ${local.availability_zone}"
}, var.default_tags, var.private_subnet_tags)
}

resource "aws_route_table_association" "private" {
Expand Down
54 changes: 54 additions & 0 deletions availability-zone/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,60 @@ Internet Gateway which belongs to `var.vpc`.
EOS
}

variable "nat_gateway_eip_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the EIP of the NAT Gateway created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "nat_gateway_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the NAT Gateway created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "private_route_table_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the private route table created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "private_subnet_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the private subnet created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "public_route_table_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the public route table created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "public_subnet_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the public subnet created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "subnet_bits" {
type = number

Expand Down
29 changes: 23 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ resource "aws_vpc" "this" {
enable_dns_support = true
enable_dns_hostnames = true

tags = merge({ Name = var.name }, var.default_tags)
tags = merge({
Name = var.name
}, var.default_tags, var.vpc_tags)
}

# Internet Gateway

resource "aws_internet_gateway" "this" {
vpc_id = aws_vpc.this.id

tags = merge({ Name = var.name }, var.default_tags)
tags = merge({
Name = var.name
}, var.default_tags, var.internet_gateway_tags)
}

# Pairs of Public-Private Subnets per Availability Zone
Expand All @@ -32,6 +36,13 @@ module "availability_zone" {
subnet_bits = var.subnet_bits
subnet_index = count.index

nat_gateway_eip_tags = var.nat_gateway_eip_tags
nat_gateway_tags = var.nat_gateway_tags
private_route_table_tags = var.private_route_table_tags
private_subnet_tags = var.private_subnet_tags
public_route_table_tags = var.public_route_table_tags
public_subnet_tags = var.public_subnet_tags

default_tags = var.default_tags
}

Expand All @@ -45,7 +56,7 @@ resource "aws_db_subnet_group" "this" {

subnet_ids = module.availability_zone[*].private_subnet.id

tags = var.default_tags
tags = merge(var.default_tags, var.db_subnet_group_tags)
}

resource "aws_elasticache_subnet_group" "this" {
Expand All @@ -69,7 +80,9 @@ resource "aws_vpc_endpoint" "gateway" {

route_table_ids = module.availability_zone[*].private_route_table.id

tags = merge({ Name = each.key }, var.default_tags)
tags = merge({
Name = each.key
}, var.default_tags, var.gateway_vpc_endpoint_tags)
}

# VPC Endpoints: type `Interface`
Expand All @@ -86,7 +99,9 @@ resource "aws_vpc_endpoint" "interface" {
subnet_ids = module.availability_zone[*].private_subnet.id
security_group_ids = [aws_security_group.vpc-endpoints-interface[0].id]

tags = merge({ Name = each.key }, var.default_tags)
tags = merge({
Name = each.key
}, var.default_tags, var.interface_vpc_endpoint_tags)

depends_on = [
aws_security_group_rule.vpc-endpoints-interface-ingress,
Expand All @@ -102,7 +117,9 @@ resource "aws_security_group" "vpc-endpoints-interface" {
name = "vpc-endpoints-interface"
description = "VPC Endpoints Interface"

tags = merge({ Name = "VPC Endpoints Interface" }, var.default_tags)
tags = merge({
Name = "VPC Endpoints Interface"
}, var.default_tags, var.interface_vpc_endpoint_security_group_tags)

lifecycle {
create_before_destroy = true
Expand Down
108 changes: 108 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
variable "db_subnet_group_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the DB subnet group created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "cidr_block" {
type = string

Expand Down Expand Up @@ -33,6 +42,42 @@ Map of tags assigned to all AWS resources created by this module.
EOS
}

variable "gateway_vpc_endpoint_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the VPC Endpoints with type `Gateway` created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "interface_vpc_endpoint_security_group_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the security groups of the VPC Endpoints with type `Interface` created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "interface_vpc_endpoint_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the VPC Endpoints with type `Interface` created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "internet_gateway_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the Internet Gateway created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "name" {
type = string

Expand All @@ -41,6 +86,60 @@ Name to be used in all `Name` tags shown in the AWS Console.
EOS
}

variable "nat_gateway_eip_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the EIPs of the NAT Gateways created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "nat_gateway_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the NAT Gateways created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "private_route_table_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the private route tables created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "private_subnet_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the private subnets created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "public_route_table_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the public route tables created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "public_subnet_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the public subnets created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}

variable "size" {
type = number

Expand Down Expand Up @@ -78,3 +177,12 @@ Every VPC endpoint belongs to a service name like `com.amazonaws.REGION.IDENTIFI
The lists of this variable (grouped by VPC endpoint type) are expecting just the `IDENTIFIER` of the service name.
EOS
}

variable "vpc_tags" {
type = map(string)
default = {}

description = <<EOS
Map of tags assigned to the VPC created by this module. Tags in this map will override tags in `var.default_tags`.
EOS
}