diff --git a/label.tf b/label.tf deleted file mode 100644 index 8407e6a3..00000000 --- a/label.tf +++ /dev/null @@ -1,105 +0,0 @@ -module "label" { - source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0" - attributes = var.attributes - namespace = var.namespace - environment = var.environment - stage = var.stage - delimiter = var.delimiter - name = var.name - tags = var.tags - additional_tag_map = var.additional_tag_map - regex_replace_chars = var.regex_replace_chars - label_order = var.label_order - context = var.context - enabled = var.enabled -} - -variable "additional_tag_map" { - type = map(string) - default = {} - description = "Additional tags for appending to each tag map" -} - -variable "label_order" { - type = list(string) - default = [] - description = "The naming order of the ID output and Name tag" -} - -variable "regex_replace_chars" { - type = string - default = "/[^a-zA-Z0-9-]/" - description = "Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`. By default only hyphens, letters and digits are allowed, all other chars are removed" -} - -variable "tags" { - description = "Additional tags to apply to all resources that use this label module" - type = map(string) - default = {} -} - -variable "namespace" { - type = string - default = "" - description = "Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp'" -} - -variable "stage" { - type = string - default = "" - description = "Stage, e.g. 'prod', 'staging', 'dev', or 'test'" -} - -variable "name" { - type = string - default = "" - description = "Solution name, e.g. 'app' or 'cluster'" -} - -variable "environment" { - type = string - description = "The environment name if not using stage" - default = "" -} - -variable "attributes" { - type = list(string) - description = "Any extra attributes for naming these resources" - default = [] -} - -variable "delimiter" { - type = string - default = "-" - description = "Delimiter to be used between `namespace`, `stage`, `name` and `attributes`" -} - -variable "context" { - type = object({ - namespace = string - environment = string - stage = string - name = string - enabled = bool - delimiter = string - attributes = list(string) - label_order = list(string) - tags = map(string) - additional_tag_map = map(string) - regex_replace_chars = string - }) - default = { - namespace = "" - environment = "" - stage = "" - name = "" - enabled = true - delimiter = "" - attributes = [] - label_order = [] - tags = {} - additional_tag_map = {} - regex_replace_chars = "" - } - description = "Default context to use for passing state between label invocations" -} diff --git a/main.tf b/main.tf index ff0226f4..59158907 100644 --- a/main.tf +++ b/main.tf @@ -14,10 +14,26 @@ locals { } data "aws_eip" "nat_ips" { - count = length(var.existing_nat_ips) + count = var.enabled ? length(var.existing_nat_ips) : 0 public_ip = element(var.existing_nat_ips, count.index) } locals { use_existing_eips = length(var.existing_nat_ips) > 0 } + +module "label" { + source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0" + attributes = var.attributes + namespace = var.namespace + environment = var.environment + stage = var.stage + delimiter = var.delimiter + name = var.name + tags = var.tags + additional_tag_map = var.additional_tag_map + regex_replace_chars = var.regex_replace_chars + label_order = var.label_order + context = var.context + enabled = var.enabled +} diff --git a/nat-gateway.tf b/nat-gateway.tf index f4311330..b0b948af 100644 --- a/nat-gateway.tf +++ b/nat-gateway.tf @@ -1,5 +1,6 @@ module "nat_label" { source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0" + enabled = var.enabled context = module.label.context attributes = distinct(compact(concat(module.label.attributes, ["nat"]))) } @@ -12,7 +13,7 @@ locals { } resource "aws_eip" "default" { - count = local.nat_gateway_eip_count + count = var.enabled ? local.nat_gateway_eip_count : 0 vpc = true tags = merge( @@ -37,7 +38,7 @@ resource "aws_eip" "default" { } resource "aws_nat_gateway" "default" { - count = local.nat_gateways_count + count = var.enabled ? local.nat_gateways_count : 0 allocation_id = element(local.gateway_eip_allocations, count.index) subnet_id = element(aws_subnet.public.*.id, count.index) @@ -63,7 +64,7 @@ resource "aws_nat_gateway" "default" { } resource "aws_route" "default" { - count = local.nat_gateways_count + count = var.enabled ? local.nat_gateways_count : 0 route_table_id = element(aws_route_table.private.*.id, count.index) nat_gateway_id = element(aws_nat_gateway.default.*.id, count.index) destination_cidr_block = "0.0.0.0/0" diff --git a/nat-instance.tf b/nat-instance.tf index bf6ac359..7dd0c693 100644 --- a/nat-instance.tf +++ b/nat-instance.tf @@ -1,4 +1,5 @@ module "nat_instance_label" { + enabled = var.enabled source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0" context = module.label.context attributes = distinct(compact(concat(module.label.attributes, ["nat", "instance"]))) @@ -6,14 +7,14 @@ module "nat_instance_label" { locals { cidr_block = var.cidr_block != "" ? var.cidr_block : join("", data.aws_vpc.default.*.cidr_block) - nat_instance_enabled = var.enabled && var.nat_instance_enabled ? 1 : 0 + nat_instance_enabled = var.nat_instance_enabled ? 1 : 0 nat_instance_count = var.nat_instance_enabled && ! local.use_existing_eips ? length(var.availability_zones) : 0 nat_instance_eip_count = local.use_existing_eips ? 0 : local.nat_instance_count instance_eip_allocations = local.use_existing_eips ? data.aws_eip.nat_ips.*.id : aws_eip.nat_instance.*.id } resource "aws_security_group" "nat_instance" { - count = local.nat_instance_enabled + count = var.enabled ? local.nat_instance_enabled : 0 name = module.nat_instance_label.id description = "Security Group for NAT Instance" vpc_id = var.vpc_id @@ -21,7 +22,7 @@ resource "aws_security_group" "nat_instance" { } resource "aws_security_group_rule" "nat_instance_egress" { - count = local.nat_instance_enabled + count = var.enabled ? local.nat_instance_enabled : 0 description = "Allow all egress traffic" from_port = 0 to_port = 0 @@ -32,7 +33,7 @@ resource "aws_security_group_rule" "nat_instance_egress" { } resource "aws_security_group_rule" "nat_instance_ingress" { - count = local.nat_instance_enabled + count = var.enabled ? local.nat_instance_enabled : 0 description = "Allow ingress traffic from the VPC CIDR block" from_port = 0 to_port = 0 @@ -44,7 +45,7 @@ resource "aws_security_group_rule" "nat_instance_ingress" { // aws --region us-west-2 ec2 describe-images --owners amazon --filters Name="name",Values="amzn-ami-vpc-nat*" Name="virtualization-type",Values="hvm" data "aws_ami" "nat_instance" { - count = local.nat_instance_enabled + count = var.enabled ? local.nat_instance_enabled : 0 most_recent = true filter { @@ -64,7 +65,7 @@ data "aws_ami" "nat_instance" { // https://docs.aws.amazon.com/vpc/latest/userguide/VPC_NAT_Instance.html // https://dzone.com/articles/nat-instance-vs-nat-gateway resource "aws_instance" "nat_instance" { - count = local.nat_instance_count + count = var.enabled ? local.nat_instance_count : 0 ami = join("", data.aws_ami.nat_instance.*.id) instance_type = var.nat_instance_type subnet_id = element(aws_subnet.public.*.id, count.index) @@ -98,7 +99,7 @@ resource "aws_instance" "nat_instance" { } resource "aws_eip" "nat_instance" { - count = local.nat_instance_eip_count + count = var.enabled ? local.nat_instance_eip_count : 0 vpc = true tags = merge( module.nat_instance_label.tags, @@ -122,13 +123,13 @@ resource "aws_eip" "nat_instance" { } resource "aws_eip_association" "nat_instance" { - count = local.nat_instance_count + count = var.enabled ? local.nat_instance_count : 0 instance_id = element(aws_instance.nat_instance.*.id, count.index) allocation_id = element(local.instance_eip_allocations, count.index) } resource "aws_route" "nat_instance" { - count = local.nat_instance_count + count = var.enabled ? local.nat_instance_count : 0 route_table_id = element(aws_route_table.private.*.id, count.index) instance_id = element(aws_instance.nat_instance.*.id, count.index) destination_cidr_block = "0.0.0.0/0" diff --git a/private.tf b/private.tf index 28b87580..edca5497 100644 --- a/private.tf +++ b/private.tf @@ -1,5 +1,6 @@ module "private_label" { source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0" + enabled = var.enabled context = module.label.context attributes = compact(concat(module.label.attributes, ["private"])) @@ -11,12 +12,12 @@ module "private_label" { } locals { - private_subnet_count = var.enabled && var.max_subnet_count == 0 ? length(flatten(data.aws_availability_zones.available.*.names)) : var.max_subnet_count - private_network_acl_enabled = var.enabled && signum(length(var.private_network_acl_id)) == 0 ? 1 : 0 + private_subnet_count = var.max_subnet_count == 0 ? length(flatten(data.aws_availability_zones.available.*.names)) : var.max_subnet_count + private_network_acl_enabled = signum(length(var.private_network_acl_id)) == 0 ? 1 : 0 } resource "aws_subnet" "private" { - count = local.availability_zones_count + count = var.enabled ? local.availability_zones_count : 0 vpc_id = join("", data.aws_vpc.default.*.id) availability_zone = element(var.availability_zones, count.index) @@ -49,7 +50,7 @@ resource "aws_subnet" "private" { } resource "aws_route_table" "private" { - count = local.availability_zones_count + count = var.enabled ? local.availability_zones_count : 0 vpc_id = join("", data.aws_vpc.default.*.id) tags = merge( @@ -70,13 +71,13 @@ resource "aws_route_table" "private" { } resource "aws_route_table_association" "private" { - count = local.availability_zones_count + count = var.enabled ? local.availability_zones_count : 0 subnet_id = element(aws_subnet.private.*.id, count.index) route_table_id = element(aws_route_table.private.*.id, count.index) } resource "aws_network_acl" "private" { - count = local.private_network_acl_enabled + count = var.enabled ? local.private_network_acl_enabled : 0 vpc_id = var.vpc_id subnet_ids = aws_subnet.private.*.id diff --git a/public.tf b/public.tf index d381f8a2..3c6bc812 100644 --- a/public.tf +++ b/public.tf @@ -1,5 +1,6 @@ module "public_label" { source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0" + enabled = var.enabled context = module.label.context attributes = compact(concat(module.label.attributes, ["public"])) diff --git a/variables.tf b/variables.tf index b1a45843..d5678d2b 100644 --- a/variables.tf +++ b/variables.tf @@ -100,3 +100,94 @@ variable "public_subnets_additional_tags" { default = {} description = "Additional tags to be added to public subnets" } + +variable "additional_tag_map" { + type = map(string) + default = {} + description = "Additional tags for appending to each tag map" +} + +variable "label_order" { + type = list(string) + default = [] + description = "The naming order of the ID output and Name tag" +} + +variable "regex_replace_chars" { + type = string + default = "/[^a-zA-Z0-9-]/" + description = "Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`. By default only hyphens, letters and digits are allowed, all other chars are removed" +} + +variable "tags" { + description = "Additional tags to apply to all resources that use this label module" + type = map(string) + default = {} +} + +variable "namespace" { + type = string + default = "" + description = "Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp'" +} + +variable "stage" { + type = string + default = "" + description = "Stage, e.g. 'prod', 'staging', 'dev', or 'test'" +} + +variable "name" { + type = string + default = "" + description = "Solution name, e.g. 'app' or 'cluster'" +} + +variable "environment" { + type = string + description = "The environment name if not using stage" + default = "" +} + +variable "attributes" { + type = list(string) + description = "Any extra attributes for naming these resources" + default = [] +} + +variable "delimiter" { + type = string + default = "-" + description = "Delimiter to be used between `namespace`, `stage`, `name` and `attributes`" +} + +variable "context" { + type = object({ + namespace = string + environment = string + stage = string + name = string + enabled = bool + delimiter = string + attributes = list(string) + label_order = list(string) + tags = map(string) + additional_tag_map = map(string) + regex_replace_chars = string + }) + default = { + namespace = "" + environment = "" + stage = "" + name = "" + enabled = true + delimiter = "" + attributes = [] + label_order = [] + tags = {} + additional_tag_map = {} + regex_replace_chars = "" + } + description = "Default context to use for passing state between label invocations" +} +