Skip to content

Commit

Permalink
fixing enabled flag (#93)
Browse files Browse the repository at this point in the history
* fixing enabled flag

* fixing enabled flag

* Updated README.md

* cleaning empty file

Co-authored-by: actions-bot <[email protected]>
  • Loading branch information
jamengual and actions-bot authored Aug 12, 2020
1 parent ad55d97 commit 537bf7b
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 124 deletions.
105 changes: 0 additions & 105 deletions label.tf

This file was deleted.

18 changes: 17 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 4 additions & 3 deletions nat-gateway.tf
Original file line number Diff line number Diff line change
@@ -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"])))
}
Expand All @@ -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(
Expand All @@ -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)

Expand All @@ -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"
Expand Down
19 changes: 10 additions & 9 deletions nat-instance.tf
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
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"])))
}

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
tags = module.nat_instance_label.tags
}

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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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"
Expand Down
13 changes: 7 additions & 6 deletions private.tf
Original file line number Diff line number Diff line change
@@ -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"]))

Expand All @@ -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)

Expand Down Expand Up @@ -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(
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions public.tf
Original file line number Diff line number Diff line change
@@ -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"]))

Expand Down
Loading

0 comments on commit 537bf7b

Please sign in to comment.