Skip to content

Instantly share code, notes, and snippets.

@cabrinha
Created March 20, 2018 21:00
Show Gist options
  • Save cabrinha/b3a931e25d79f4cfdbd3f98cea31fc29 to your computer and use it in GitHub Desktop.
Save cabrinha/b3a931e25d79f4cfdbd3f98cea31fc29 to your computer and use it in GitHub Desktop.
# Create a VPC
resource "aws_vpc" "jenkins" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
tags {
Name = "jenkins"
}
}
resource "aws_internet_gateway" "jenkins" {
vpc_id = "${aws_vpc.jenkins.id}"
}
resource "aws_security_group" "nat" {
name = "vpc_nat"
egress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["73.162.14.117/32"]
}
vpc_id = "${aws_vpc.jenkins.id}"
tags {
Name = "nat-sg"
}
}
resource "aws_instance" "nat" {
ami = "ami-030f4133" # this is a special ami preconfigured to do NAT
availability_zone = "us-west-2b"
instance_type = "m1.small"
vpc_security_group_ids = ["${aws_security_group.nat.id}"]
subnet_id = "${aws_subnet.us-west-2b-public.id}"
associate_public_ip_address = true
source_dest_check = false
tags {
Name = "VPC NAT"
}
}
resource "aws_eip" "nat" {
instance = "${aws_instance.nat.id}"
vpc = true
}
/*
Public Subnet
*/
resource "aws_subnet" "us-west-2b-public" {
vpc_id = "${aws_vpc.jenkins.id}"
cidr_block = "${var.public_subnet_cidr}"
availability_zone = "us-west-2b"
tags {
Name = "Public Subnet"
}
}
resource "aws_route_table" "us-west-2b-public" {
vpc_id = "${aws_vpc.jenkins.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.jenkins.id}"
}
tags {
Name = "Public Subnet"
}
}
resource "aws_route_table_association" "us-west-2b-public" {
subnet_id = "${aws_subnet.us-west-2b-public.id}"
route_table_id = "${aws_route_table.us-west-2b-public.id}"
}
/*
Private Subnet
*/
resource "aws_subnet" "us-west-2b-private" {
vpc_id = "${aws_vpc.jenkins.id}"
cidr_block = "${var.private_subnet_cidr}"
availability_zone = "us-west-2b"
tags {
Name = "Private Subnet"
}
}
resource "aws_route_table" "us-west-2b-private" {
vpc_id = "${aws_vpc.jenkins.id}"
route {
cidr_block = "0.0.0.0/0"
instance_id = "${aws_instance.nat.id}"
}
tags {
Name = "Private Subnet"
}
}
resource "aws_route_table_association" "us-west-2b-private" {
subnet_id = "${aws_subnet.us-west-2b-private.id}"
route_table_id = "${aws_route_table.us-west-2b-private.id}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment