Add Terraform configurations for creating a new VPC

Add Terraform configuration files for launching an instance in a new VPC on a new subnet, with a new Internet gateway, new route table, and new route table association (all required when creating a new VPC).

Signed-off-by: Scott S. Lowe <scott.lowe@scottlowe.org>
This commit is contained in:
Scott S. Lowe 2016-11-09 01:38:43 -07:00
parent 4bd2a658de
commit b12b263e77
4 changed files with 112 additions and 0 deletions

View file

@ -0,0 +1,18 @@
data "aws_ami" "coreos_stable" {
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "name"
values = ["*1185.3.0*"]
}
}

View file

@ -0,0 +1,80 @@
# Create a new VPC
resource "aws_vpc" "coreos_vpc" {
cidr_block = "10.1.0.0/16"
enable_dns_hostnames = "true"
enable_dns_support = "true"
tags {
tool = "terraform"
}
}
# Create a subnet in the new VPC
resource "aws_subnet" "coreos_subnet" {
vpc_id = "${aws_vpc.coreos_vpc.id}"
cidr_block = "10.1.1.0/24"
map_public_ip_on_launch = "true"
tags {
tool = "terraform"
}
}
# Create a new security group
resource "aws_security_group" "allow_inbound_ssh" {
vpc_id = "${aws_vpc.coreos_vpc.id}"
name = "allow-inbound-ssh"
description = "Allows inbound SSH"
ingress {
from_port = "22"
to_port = "22"
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = "0"
to_port = "0"
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags {
tool = "terraform"
}
}
# Create a new Internet gateway
resource "aws_internet_gateway" "coreos_gw" {
vpc_id = "${aws_vpc.coreos_vpc.id}"
tags {
tool = "terraform"
}
}
# Create a route table for the new VPC
resource "aws_route_table" "coreos_vpc_rt" {
vpc_id = "${aws_vpc.coreos_vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.coreos_gw.id}"
}
tags {
tool = "terraform"
}
}
# Associate route table with subnet in VPC
resource "aws_route_table_association" "coreos_vpc_rta" {
subnet_id = "${aws_subnet.coreos_subnet.id}"
route_table_id = "${aws_route_table.coreos_vpc_rt.id}"
}
# Launch a new CoreOS instance in the new subnet and VPC
resource "aws_instance" "coreos01" {
ami = "${data.aws_ami.coreos_stable.id}"
instance_type = "${var.flavor}"
key_name = "${var.keypair}"
vpc_security_group_ids = ["${aws_security_group.allow_inbound_ssh.id}"]
subnet_id = "${aws_subnet.coreos_subnet.id}"
depends_on = ["aws_internet_gateway.coreos_gw"]
tags {
tool = "terraform"
}
}

View file

@ -0,0 +1,3 @@
provider "aws" {
region = "us-west-2"
}

View file

@ -0,0 +1,11 @@
variable "keypair" {
type = "string"
description = "AWS SSH keypair to use to connect to instances"
default = "aws_rsa"
}
variable "flavor" {
type = "string"
description = "AWS type to use when creating instances"
default = "t2.micro"
}