|
| 1 | +/*==== |
| 2 | +The VPC |
| 3 | +======*/ |
| 4 | + |
| 5 | +resource "aws_vpc" "vpc" { |
| 6 | + cidr_block = "${var.vpc_cidr}" |
| 7 | + enable_dns_hostnames = true |
| 8 | + enable_dns_support = true |
| 9 | + |
| 10 | + tags = { |
| 11 | + Name = "${var.environment}-vpc" |
| 12 | + Environment = "${var.environment}" |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +/*==== |
| 17 | +Subnets |
| 18 | +======*/ |
| 19 | +/* Internet gateway for the public subnet */ |
| 20 | +resource "aws_internet_gateway" "ig" { |
| 21 | + vpc_id = "${aws_vpc.vpc.id}" |
| 22 | + |
| 23 | + tags = { |
| 24 | + Name = "${var.environment}-igw" |
| 25 | + Environment = "${var.environment}" |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +/* Elastic IP for NAT */ |
| 31 | +resource "aws_eip" "nat_eip" { |
| 32 | + vpc = true |
| 33 | + depends_on = [aws_internet_gateway.ig] |
| 34 | +} |
| 35 | + |
| 36 | +/* NAT */ |
| 37 | +resource "aws_nat_gateway" "nat" { |
| 38 | + allocation_id = "${aws_eip.nat_eip.id}" |
| 39 | + subnet_id = "${element(aws_subnet.public_subnet.*.id, 0)}" |
| 40 | + depends_on = [aws_internet_gateway.ig] |
| 41 | + |
| 42 | + tags = { |
| 43 | + Name = "nat" |
| 44 | + Environment = "${var.environment}" |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/* Public subnet */ |
| 49 | +resource "aws_subnet" "public_subnet" { |
| 50 | + vpc_id = "${aws_vpc.vpc.id}" |
| 51 | + count = "${length(var.public_subnets_cidr)}" |
| 52 | + cidr_block = "${element(var.public_subnets_cidr, count.index)}" |
| 53 | + availability_zone = "${element(var.availability_zones, count.index)}" |
| 54 | + map_public_ip_on_launch = true |
| 55 | + |
| 56 | + tags = { |
| 57 | + Name = "${var.environment}-${element(var.availability_zones, count.index)}-public-subnet" |
| 58 | + Environment = "${var.environment}" |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/* Private subnet */ |
| 63 | +resource "aws_subnet" "private_subnet" { |
| 64 | + vpc_id = "${aws_vpc.vpc.id}" |
| 65 | + count = "${length(var.private_subnets_cidr)}" |
| 66 | + cidr_block = "${element(var.private_subnets_cidr, count.index)}" |
| 67 | + availability_zone = "${element(var.availability_zones, count.index)}" |
| 68 | + map_public_ip_on_launch = false |
| 69 | + |
| 70 | + tags = { |
| 71 | + Name = "${var.environment}-${element(var.availability_zones, count.index)}-private-subnet" |
| 72 | + Environment = "${var.environment}" |
| 73 | + "kubernetes.io/role/elb" = "1" |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/* Routing table for private subnet */ |
| 78 | +resource "aws_route_table" "private" { |
| 79 | + vpc_id = "${aws_vpc.vpc.id}" |
| 80 | + |
| 81 | + tags = { |
| 82 | + Name = "${var.environment}-private-route-table" |
| 83 | + Environment = "${var.environment}" |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/* Routing table for public subnet */ |
| 88 | +resource "aws_route_table" "public" { |
| 89 | + vpc_id = "${aws_vpc.vpc.id}" |
| 90 | + |
| 91 | + tags = { |
| 92 | + Name = "${var.environment}-public-route-table" |
| 93 | + Environment = "${var.environment}" |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +resource "aws_route" "public_internet_gateway" { |
| 98 | + route_table_id = "${aws_route_table.public.id}" |
| 99 | + destination_cidr_block = "0.0.0.0/0" |
| 100 | + gateway_id = "${aws_internet_gateway.ig.id}" |
| 101 | +} |
| 102 | + |
| 103 | +resource "aws_route" "private_nat_gateway" { |
| 104 | + route_table_id = "${aws_route_table.private.id}" |
| 105 | + destination_cidr_block = "0.0.0.0/0" |
| 106 | + nat_gateway_id = "${aws_nat_gateway.nat.id}" |
| 107 | +} |
| 108 | + |
| 109 | +/* Route table associations */ |
| 110 | +resource "aws_route_table_association" "public" { |
| 111 | + count = "${length(var.public_subnets_cidr)}" |
| 112 | + subnet_id = "${element(aws_subnet.public_subnet.*.id, count.index)}" |
| 113 | + route_table_id = "${aws_route_table.public.id}" |
| 114 | +} |
| 115 | + |
| 116 | +resource "aws_route_table_association" "private" { |
| 117 | + count = "${length(var.private_subnets_cidr)}" |
| 118 | + subnet_id = "${element(aws_subnet.private_subnet.*.id, count.index)}" |
| 119 | + route_table_id = "${aws_route_table.private.id}" |
| 120 | +} |
| 121 | + |
| 122 | +/*==== |
| 123 | +VPC's Default Security Group |
| 124 | +======*/ |
| 125 | +resource "aws_security_group" "default" { |
| 126 | + name = "${var.environment}-default-sg" |
| 127 | + description = "Default security group to allow inbound/outbound from the VPC" |
| 128 | + vpc_id = "${aws_vpc.vpc.id}" |
| 129 | + depends_on = [aws_vpc.vpc] |
| 130 | + |
| 131 | + ingress { |
| 132 | + from_port = "0" |
| 133 | + to_port = "0" |
| 134 | + protocol = "-1" |
| 135 | + self = true |
| 136 | + } |
| 137 | + |
| 138 | + egress { |
| 139 | + from_port = "0" |
| 140 | + to_port = "0" |
| 141 | + protocol = "-1" |
| 142 | + self = "true" |
| 143 | + } |
| 144 | + |
| 145 | + tags = { |
| 146 | + Environment = "${var.environment}" |
| 147 | + } |
| 148 | +} |
| 149 | + |
0 commit comments