Commit 8c5f1320 authored by Rob Greenwood's avatar Rob Greenwood

Initial commit

parents
Pipeline #58 passed with stage
in 5 seconds
image: alpine:latest
variables:
TERRAFORM_URL: "https://releases.hashicorp.com/terraform/0.7.1/terraform_0.7.1_linux_amd64.zip"
before_script:
- apk update && apk add ca-certificates && update-ca-certificates && apk add openssl
- wget -O /tmp/terraform.zip $TERRAFORM_URL
- unzip /tmp/terraform.zip -d /usr/local/bin
test:
script: terraform validate
variable "name" {
description = "Name of the VPC"
}
variable "cidr" {
description = "The CIDR block for the VPC"
}
variable "availability_zones" {
description = "List of availability zones"
type = "list"
}
variable "public_subnets" {
description = "List of public subnets to availability zones"
type = "list"
}
variable "private_subnets" {
description = "List of private subnets to availability zones"
type = "list"
}
variable "enable_dns_hostnames" {
description = "When set to true, instances in the VPC get DNS hostname"
default = true
}
variable "enable_dns_support" {
description = "When set to true, the Amazon DNS server is enabled within the VPC"
default = true
}
resource "aws_vpc" "mod" {
cidr_block = "${var.cidr}"
enable_dns_hostnames = "${var.enable_dns_hostnames}"
enable_dns_support = "${var.enable_dns_support}"
tags {
Name = "${var.name}"
}
}
resource "aws_subnet" "public" {
lifecycle { create_before_destroy = true }
vpc_id = "${aws_vpc.mod.id}"
cidr_block = "${element(var.public_subnets, count.index)}"
availability_zone = "${element(var.availability_zones, count.index)}"
count = "${length(var.public_subnets)}"
map_public_ip_on_launch = true
tags {
Name = "${var.name}.public.${element(var.availability_zones, count.index)}"
}
}
resource "aws_subnet" "private" {
lifecycle { create_before_destroy = true }
vpc_id = "${aws_vpc.mod.id}"
cidr_block = "${element(var.private_subnets, count.index)}"
availability_zone = "${element(var.availability_zones, count.index)}"
count = "${length(var.private_subnets)}"
tags {
Name = "${var.name}.private.${element(var.availability_zones, count.index)}"
}
}
resource "aws_internet_gateway" "mod" {
vpc_id = "${aws_vpc.mod.id}"
tags {
Name = "${var.name}"
}
}
resource "aws_eip" "nat" {
vpc = true
count = "${length(var.public_subnets)}"
}
resource "aws_nat_gateway" "nat" {
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
allocation_id = "${element(aws_eip.nat.*.id, count.index)}"
count = "${length(var.public_subnets)}"
depends_on = ["aws_internet_gateway.mod"]
}
resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.mod.id}"
tags {
Name = "${var.name}.public"
}
}
resource "aws_route_table" "private" {
vpc_id = "${aws_vpc.mod.id}"
count = "${length(var.private_subnets)}"
tags {
Name = "${var.name}.private.${element(var.availability_zones, count.index)}"
}
}
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" "nat_gateway" {
route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = "${element(aws_nat_gateway.nat.*.id, count.index)}"
count = "${length(var.public_subnets)}"
depends_on = ["aws_route_table.private"]
}
resource "aws_route_table_association" "public" {
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
route_table_id = "${aws_route_table.public.id}"
count = "${length(var.public_subnets)}"
}
resource "aws_route_table_association" "private" {
subnet_id = "${element(aws_subnet.private.*.id, count.index)}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
count = "${length(var.private_subnets)}"
}
output "private_subnets" {
value = ["${aws_subnet.private.*.id}"]
}
output "public_subnets" {
value = ["${aws_subnet.public.*.id}"]
}
output "vpc_id" {
value = "${aws_vpc.mod.id}"
}
output "public_route_table_id" {
value = "${aws_route_table.public.id}"
}
output "private_route_table_ids" {
value = ["${aws_route_table.private.*.id}"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment