variable "globalTags" { type = "map" default {} } variable "resource_prefix" {} terraform { required_version = ">= 0.9.9" } # VPC resource "aws_vpc" "tf-vpc1" { cidr_block = "10.10.0.0/16" tags = "${merge(var.globalTags, map("Name","KFLAB"))}" } # Get all AZs data "aws_availability_zones" "available" { state = "available" } # 2 az, 1 public subnet in each resource "aws_subnet" "PublicSubnet" { vpc_id = "${aws_vpc.tf-vpc1.id}" cidr_block = "10.10.${count.index + 1}.0/24" availability_zone = "${data.aws_availability_zones.available.names[count.index]}" tags = "${merge(var.globalTags, map("Name","PublicSubnet-${count.index + 1}"))}" count = "${length(data.aws_availability_zones.available.names)}" } # 2 az, 1 private subnet in each resource "aws_subnet" "PrivateSubnet" { vpc_id = "${aws_vpc.tf-vpc1.id}" cidr_block = "10.10.${count.index + 21}.0/24" availability_zone = "${data.aws_availability_zones.available.names[count.index]}" tags = "${merge(var.globalTags, map("Name","PrivateSubnet-${count.index + 1}"))}" count = "${length(data.aws_availability_zones.available.names)}" } # IGW resource "aws_internet_gateway" "igw1" { vpc_id = "${aws_vpc.tf-vpc1.id}" tags = "${merge(var.globalTags, map("Name","IGW1"))}" } # Public RT resource "aws_route_table" "PublicRouteTable" { vpc_id = "${aws_vpc.tf-vpc1.id}" route { cidr_block = "0.0.0.0/0" gateway_id = "${aws_internet_gateway.igw1.id}" } tags = "${merge(var.globalTags, map("Name","PublicRouteTable"))}" } # Associate Public RT resource "aws_route_table_association" "PublicRTAsso" { subnet_id = "${element(aws_subnet.PublicSubnet.*.id, count.index)}" route_table_id = "${aws_route_table.PublicRouteTable.id}" count = "${length(data.aws_availability_zones.available.names)}" } # NAT Gateway resource "aws_eip" "ngw1-eip" { vpc = true tags = "${var.globalTags}" } resource "aws_nat_gateway" "ngw1" { allocation_id = "${aws_eip.ngw1-eip.id}" subnet_id = "${aws_subnet.PublicSubnet.0.id}" tags = "${merge(var.globalTags, map("Name","NGW1"))}" } # Private RT resource "aws_route_table" "PrivateRouteTable" { vpc_id = "${aws_vpc.tf-vpc1.id}" route { cidr_block = "0.0.0.0/0" nat_gateway_id = "${aws_nat_gateway.ngw1.id}" } tags = "${merge(var.globalTags, map("Name","PrivateRouteTable"))}" } # Associate Private RT resource "aws_route_table_association" "PrivateRTAsso" { subnet_id = "${element(aws_subnet.PrivateSubnet.*.id, count.index)}" route_table_id = "${aws_route_table.PrivateRouteTable.id}" count = "${length(data.aws_availability_zones.available.names)}" } output "NGW IP" { value = "${aws_nat_gateway.ngw1.public_ip}" }