Skip to content

Instantly share code, notes, and snippets.

@echo-devnull
Created June 9, 2016 07:01
Show Gist options
  • Save echo-devnull/8efed6f0978667bd21c67cdc8b722f1d to your computer and use it in GitHub Desktop.
Save echo-devnull/8efed6f0978667bd21c67cdc8b722f1d to your computer and use it in GitHub Desktop.
# Create a vpc
resource "aws_vpc" "mod" {
cidr_block = "${var.cidr}.0.0/16"
enable_dns_hostnames = "${var.enable_dns_hostnames}"
enable_dns_support = "${var.enable_dns_support}"
tags { Name = "${var.name}" }
}
resource "aws_internet_gateway" "mod" {
vpc_id = "${aws_vpc.mod.id}"
}
### Route tables ###
resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.mod.id}"
tags { Name = "${var.name}-public" }
}
resource "aws_route" "public_internet_gateway" {
route_table_id = "${aws_route_table.public.id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.mod.id}"
}
resource "aws_route_table" "frontend" {
vpc_id = "${aws_vpc.mod.id}"
tags { Name = "${var.name}-frontend" }
}
resource "aws_route" "fr" {
route_table_id = "${aws_route_table.frontend.id}"
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = "${aws_nat_gateway.nat.id}"
}
resource "aws_route_table" "backend" {
vpc_id = "${aws_vpc.mod.id}"
tags { Name = "${var.name}-backend" }
}
resource "aws_route" "bk" {
route_table_id = "${aws_route_table.backend.id}"
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = "${aws_nat_gateway.nat.id}"
}
### SUBNETS ###
resource "aws_subnet" "public" {
vpc_id = "${aws_vpc.mod.id}"
#cidr_block = "${lookup(var.public_blocks, concat("zone", count.index))}"
cidr_block = "${var.cidr}.10${count.index + 1}.0/24"
availability_zone = "${lookup(var.zones, concat("zone", count.index))}"
map_public_ip_on_launch = true
count = "${var.public_count}"
tags {
Name = "${var.env}-public-${count.index + 1}"
}
}
resource "aws_subnet" "frontend" {
vpc_id = "${aws_vpc.mod.id}"
#cidr_block = "${lookup(var.frontend_blocks, concat("zone", count.index))}"
cidr_block = "${var.cidr}.11${count.index + 1}.0/24"
availability_zone = "${lookup(var.zones, concat("zone", count.index))}"
map_public_ip_on_launch = false
count = "${var.frontend_count}"
tags {
Name = "${var.env}-frontend-${count.index + 1}"
}
}
resource "aws_subnet" "backend" {
vpc_id = "${aws_vpc.mod.id}"
#cidr_block = "${lookup(var.backend_blocks, concat("zone", count.index))}"
cidr_block = "${var.cidr}.12${count.index + 1}.0/24"
availability_zone = "${lookup(var.zones, concat("zone", count.index))}"
map_public_ip_on_launch = false
count = "${var.backend_count}"
tags {
Name = "${var.env}-backend-${count.index + 1}"
}
}
### Route table Associations ###
resource "aws_route_table_association" "public" {
count = "${var.public_count}"
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
route_table_id = "${aws_route_table.public.id}"
}
resource "aws_route_table_association" "frontend" {
count = "${var.frontend_count}"
subnet_id = "${element(aws_subnet.frontend.*.id, count.index)}"
route_table_id = "${aws_route_table.frontend.id}"
}
resource "aws_route_table_association" "backend" {
count = "${var.backend_count}"
subnet_id = "${element(aws_subnet.backend.*.id, count.index)}"
route_table_id = "${aws_route_table.backend.id}"
}
# resource "aws_eip" "nat" {
# vpc = true
# count = "${length(compact(split(",", var.public_subnets)))}"
# }
# resource "aws_nat_gateway" "nat" {
# allocation_id = "${element(aws_eip.nat.*.id, count.index)}"
# subnet_id = "${element(split(",", aws_subnet.public.*.id), count.index)}"
# count = "${length(compact(split(",", var.public_subnets)))}"
# }
resource "aws_eip" "nat" {
vpc = true
# count = "${var.nat_count}"
}
resource "aws_nat_gateway" "nat" {
allocation_id = "${aws_eip.nat.id}"
subnet_id = "${aws_subnet.public.0.id}"
# count = "${var.nat_count}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment