|
| 1 | +module "nat_instance_label" { |
| 2 | + source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3" |
| 3 | + namespace = "${var.namespace}" |
| 4 | + stage = "${var.stage}" |
| 5 | + name = "${var.name}" |
| 6 | + delimiter = "${var.delimiter}" |
| 7 | + attributes = "${compact(concat(var.attributes,list("nat", "instance")))}" |
| 8 | + tags = "${var.tags}" |
| 9 | +} |
| 10 | + |
| 11 | +locals { |
| 12 | + nat_instance_enabled = "${var.nat_instance_enabled == "true" ? true : false}" |
| 13 | + nat_instance_count = "${local.nat_instance_enabled ? length(var.availability_zones) : 0}" |
| 14 | + cidr_block = "${var.cidr_block != "" ? var.cidr_block : data.aws_vpc.default.cidr_block}" |
| 15 | +} |
| 16 | + |
| 17 | +resource "aws_security_group" "nat_instance" { |
| 18 | + count = "${local.nat_instance_enabled ? 1 : 0}" |
| 19 | + name = "${module.nat_instance_label.id}" |
| 20 | + description = "Security Group for NAT Instance" |
| 21 | + vpc_id = "${var.vpc_id}" |
| 22 | + tags = "${module.nat_instance_label.tags}" |
| 23 | +} |
| 24 | + |
| 25 | +resource "aws_security_group_rule" "nat_instance_egress" { |
| 26 | + count = "${local.nat_instance_enabled ? 1 : 0}" |
| 27 | + description = "Allow all egress traffic" |
| 28 | + from_port = 0 |
| 29 | + to_port = 0 |
| 30 | + protocol = "-1" |
| 31 | + cidr_blocks = ["0.0.0.0/0"] |
| 32 | + security_group_id = "${join("", aws_security_group.nat_instance.*.id)}" |
| 33 | + type = "egress" |
| 34 | +} |
| 35 | + |
| 36 | +resource "aws_security_group_rule" "nat_instance_ingress" { |
| 37 | + count = "${local.nat_instance_enabled ? 1 : 0}" |
| 38 | + description = "Allow ingress traffic from the VPC CIDR block" |
| 39 | + from_port = 0 |
| 40 | + to_port = 0 |
| 41 | + protocol = "-1" |
| 42 | + cidr_blocks = ["${local.cidr_block}"] |
| 43 | + security_group_id = "${join("", aws_security_group.nat_instance.*.id)}" |
| 44 | + type = "ingress" |
| 45 | +} |
| 46 | + |
| 47 | +// aws --region us-west-2 ec2 describe-images --owners amazon --filters Name="name",Values="amzn-ami-vpc-nat*" Name="virtualization-type",Values="hvm" |
| 48 | +data "aws_ami" "nat_instance" { |
| 49 | + count = "${local.nat_instance_enabled ? 1 : 0}" |
| 50 | + most_recent = true |
| 51 | + |
| 52 | + filter { |
| 53 | + name = "name" |
| 54 | + values = ["amzn-ami-vpc-nat*"] |
| 55 | + } |
| 56 | + |
| 57 | + filter { |
| 58 | + name = "virtualization-type" |
| 59 | + values = ["hvm"] |
| 60 | + } |
| 61 | + |
| 62 | + owners = ["amazon"] |
| 63 | +} |
| 64 | + |
| 65 | +// https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-comparison.html |
| 66 | +// https://docs.aws.amazon.com/vpc/latest/userguide/VPC_NAT_Instance.html |
| 67 | +// https://dzone.com/articles/nat-instance-vs-nat-gateway |
| 68 | +resource "aws_instance" "nat_instance" { |
| 69 | + count = "${local.nat_instance_count}" |
| 70 | + ami = "${join("", data.aws_ami.nat_instance.*.id)}" |
| 71 | + instance_type = "${var.nat_instance_type}" |
| 72 | + subnet_id = "${element(aws_subnet.public.*.id, count.index)}" |
| 73 | + vpc_security_group_ids = ["${aws_security_group.nat_instance.id}"] |
| 74 | + tags = "${merge(module.nat_instance_label.tags, map("Name",format("%s%s%s", module.nat_label.id, var.delimiter, replace(element(var.availability_zones, count.index),"-",var.delimiter))))}" |
| 75 | + |
| 76 | + # Required by NAT |
| 77 | + # https://docs.aws.amazon.com/vpc/latest/userguide/VPC_NAT_Instance.html#EIP_Disable_SrcDestCheck |
| 78 | + source_dest_check = false |
| 79 | + |
| 80 | + associate_public_ip_address = true |
| 81 | + |
| 82 | + lifecycle { |
| 83 | + create_before_destroy = true |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +resource "aws_route" "nat_instance" { |
| 88 | + count = "${local.nat_instance_count}" |
| 89 | + route_table_id = "${element(aws_route_table.private.*.id, count.index)}" |
| 90 | + instance_id = "${element(aws_instance.nat_instance.*.id, count.index)}" |
| 91 | + destination_cidr_block = "0.0.0.0/0" |
| 92 | + depends_on = ["aws_route_table.private"] |
| 93 | +} |
0 commit comments