Merge pull request #101 from lowescott/fmt-cleanup

Clean up and update formatting for Terraform configurations
This commit is contained in:
Scott S. Lowe 2017-10-18 11:53:24 -06:00 committed by GitHub
commit aa337214e9
48 changed files with 664 additions and 616 deletions

View file

@ -1,25 +1,29 @@
data "aws_ami" "atomic_ami" {
most_recent = true
owners = ["410186602215"]
filter {
name = "name"
values = ["CentOS Atomic Host 7*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
most_recent = true
owners = ["410186602215"]
filter {
name = "name"
values = ["CentOS Atomic Host 7*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
data "aws_ami" "centos_ami" {
most_recent = true
owners = ["410186602215"]
filter {
name = "name"
values = ["CentOS Linux 7*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
most_recent = true
owners = ["410186602215"]
filter {
name = "name"
values = ["CentOS Linux 7*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}

View file

@ -1,32 +1,34 @@
# Launch a CentOS 7 instance to serve as bastion host
resource "aws_instance" "bastion" {
ami = "${data.aws_ami.centos_ami.id}"
instance_type = "${var.flavor}"
key_name = "${var.keypair}"
vpc_security_group_ids = ["${aws_security_group.bastion_sg.id}"]
subnet_id = "${aws_subnet.bastion_net.id}"
depends_on = ["aws_internet_gateway.bastion_gw"]
tags {
Name = "bastion"
tool = "terraform"
demo = "bastion-aws"
area = "instances"
}
ami = "${data.aws_ami.centos_ami.id}"
instance_type = "${var.flavor}"
key_name = "${var.keypair}"
vpc_security_group_ids = ["${aws_security_group.bastion_sg.id}"]
subnet_id = "${aws_subnet.bastion_net.id}"
depends_on = ["aws_internet_gateway.bastion_gw"]
tags {
Name = "bastion"
tool = "terraform"
demo = "bastion-aws"
area = "instances"
}
}
# Launch a CentOS Atomic Host instance to serve as a private host
resource "aws_instance" "private" {
ami = "${data.aws_ami.atomic_ami.id}"
instance_type = "${var.flavor}"
key_name = "${var.keypair}"
vpc_security_group_ids = ["${aws_security_group.private_sg.id}"]
subnet_id = "${aws_subnet.private_net.id}"
depends_on = ["aws_internet_gateway.bastion_gw"]
associate_public_ip_address = false
tags {
Name = "private"
tool = "terraform"
demo = "bastion-aws"
area = "instances"
}
ami = "${data.aws_ami.atomic_ami.id}"
instance_type = "${var.flavor}"
key_name = "${var.keypair}"
vpc_security_group_ids = ["${aws_security_group.private_sg.id}"]
subnet_id = "${aws_subnet.private_net.id}"
depends_on = ["aws_internet_gateway.bastion_gw"]
associate_public_ip_address = false
tags {
Name = "private"
tool = "terraform"
demo = "bastion-aws"
area = "instances"
}
}

View file

@ -1,65 +1,71 @@
# Create a new VPC
resource "aws_vpc" "bastion_vpc" {
cidr_block = "10.2.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags {
tool = "terraform"
demo = "bastion-aws"
area = "networking"
}
cidr_block = "10.2.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags {
tool = "terraform"
demo = "bastion-aws"
area = "networking"
}
}
# Create a public subnet in the new VPC
resource "aws_subnet" "bastion_net" {
vpc_id = "${aws_vpc.bastion_vpc.id}"
cidr_block = "10.2.1.0/24"
map_public_ip_on_launch = true
tags {
tool = "terraform"
demo = "bastion-aws"
area = "networking"
}
vpc_id = "${aws_vpc.bastion_vpc.id}"
cidr_block = "10.2.1.0/24"
map_public_ip_on_launch = true
tags {
tool = "terraform"
demo = "bastion-aws"
area = "networking"
}
}
# Create a private subnet in the new VPC
resource "aws_subnet" "private_net" {
vpc_id = "${aws_vpc.bastion_vpc.id}"
cidr_block = "10.2.2.0/24"
map_public_ip_on_launch = false
tags {
tool = "terraform"
demo = "bastion-aws"
area = "networking"
}
vpc_id = "${aws_vpc.bastion_vpc.id}"
cidr_block = "10.2.2.0/24"
map_public_ip_on_launch = false
tags {
tool = "terraform"
demo = "bastion-aws"
area = "networking"
}
}
# Create a new Internet gateway
resource "aws_internet_gateway" "bastion_gw" {
vpc_id = "${aws_vpc.bastion_vpc.id}"
tags {
tool = "terraform"
demo = "bastion-aws"
area = "networking"
}
vpc_id = "${aws_vpc.bastion_vpc.id}"
tags {
tool = "terraform"
demo = "bastion-aws"
area = "networking"
}
}
# Create a route table for the new VPC
resource "aws_route_table" "bastion_routes" {
vpc_id = "${aws_vpc.bastion_vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.bastion_gw.id}"
}
tags {
tool = "terraform"
demo = "bastion-aws"
area = "networking"
}
vpc_id = "${aws_vpc.bastion_vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.bastion_gw.id}"
}
tags {
tool = "terraform"
demo = "bastion-aws"
area = "networking"
}
}
# Associate route table with subnet in VPC
resource "aws_route_table_association" "bastion_rt_assoc" {
subnet_id = "${aws_subnet.bastion_net.id}"
route_table_id = "${aws_route_table.bastion_routes.id}"
subnet_id = "${aws_subnet.bastion_net.id}"
route_table_id = "${aws_route_table.bastion_routes.id}"
}

View file

@ -1,11 +1,11 @@
output "bastion_pub_ip" {
value = ["${aws_instance.bastion.public_ip}"]
value = ["${aws_instance.bastion.public_ip}"]
}
output "bastion_priv_ip" {
value = ["${aws_instance.bastion.private_ip}"]
value = ["${aws_instance.bastion.private_ip}"]
}
output "remote_priv_ip" {
value = ["${aws_instance.private.private_ip}"]
value = ["${aws_instance.private.private_ip}"]
}

View file

@ -1,3 +1,3 @@
provider "aws" {
region = "${var.user_region}"
region = "${var.user_region}"
}

View file

@ -1,47 +1,53 @@
# Create a security group to allow SSH traffic to private host(s) only from bastion
resource "aws_security_group" "private_sg" {
vpc_id = "${aws_vpc.bastion_vpc.id}"
name = "private-sg"
description = "Security group for web traffic to private hosts"
ingress {
from_port = "22"
to_port = "22"
protocol = "tcp"
cidr_blocks = ["10.2.1.0/24"]
}
egress {
from_port = "0"
to_port = "0"
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags {
tool = "terraform"
demo = "bastion-aws"
area = "security"
}
vpc_id = "${aws_vpc.bastion_vpc.id}"
name = "private-sg"
description = "Security group for web traffic to private hosts"
ingress {
from_port = "22"
to_port = "22"
protocol = "tcp"
cidr_blocks = ["10.2.1.0/24"]
}
egress {
from_port = "0"
to_port = "0"
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags {
tool = "terraform"
demo = "bastion-aws"
area = "security"
}
}
# Create a security group to allow inbound SSH (for bastion only)
resource "aws_security_group" "bastion_sg" {
vpc_id = "${aws_vpc.bastion_vpc.id}"
name = "bastion-sg"
description = "Security group for SSH bastion host"
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"
demo = "bastion-aws"
area = "security"
}
vpc_id = "${aws_vpc.bastion_vpc.id}"
name = "bastion-sg"
description = "Security group for SSH bastion host"
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"
demo = "bastion-aws"
area = "security"
}
}

View file

@ -1,14 +1,14 @@
variable "user_region" {
type = "string"
description = "AWS region in which all resources will be created"
type = "string"
description = "AWS region in which all resources will be created"
}
variable "keypair" {
type = "string"
description = "AWS SSH keypair to use to connect to instances"
type = "string"
description = "AWS SSH keypair to use to connect to instances"
}
variable "flavor" {
type = "string"
description = "AWS type to use when creating instances"
type = "string"
description = "AWS type to use when creating instances"
}

View file

@ -1,16 +1,19 @@
data "aws_ami" "node_ami" {
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["*ubuntu-xenial-16.04*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["*ubuntu-xenial-16.04*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
}

View file

@ -1,23 +1,23 @@
module "vpc20" {
source = "./modules/vpc"
source = "./modules/vpc"
name = "etcd"
vpc_cidr_block = "10.20.0.0/16"
vpc_dns_hostnames = "true"
vpc_dns_support = "true"
subnet_map_pub_ip = "true"
name = "etcd"
vpc_cidr_block = "10.20.0.0/16"
vpc_dns_hostnames = "true"
vpc_dns_support = "true"
subnet_map_pub_ip = "true"
}
module "etcd" {
source = "./modules/ic-asg"
source = "./modules/ic-asg"
name = "etcd"
ami = "${data.aws_ami.node_ami.id}"
type = "${var.node_type}"
assign_pub_ip = true
ssh_key = "${var.key_pair}"
min_size = 3
max_size = 5
subnet_list = ["${module.vpc20.subnet_id}"]
sec_group_list = ["${module.vpc20.default_sg_id}"]
name = "etcd"
ami = "${data.aws_ami.node_ami.id}"
type = "${var.node_type}"
assign_pub_ip = true
ssh_key = "${var.key_pair}"
min_size = 3
max_size = 5
subnet_list = ["${module.vpc20.subnet_id}"]
sec_group_list = ["${module.vpc20.default_sg_id}"]
}

View file

@ -1,27 +1,27 @@
# Create a launch configuration for the instance cluster
resource "aws_launch_configuration" "lc" {
name = "${var.name}-lc"
associate_public_ip_address = "${var.assign_pub_ip}"
image_id = "${var.ami}"
instance_type = "${var.type}"
key_name = "${var.ssh_key}"
security_groups = ["${var.sec_group_list}"]
name = "${var.name}-lc"
associate_public_ip_address = "${var.assign_pub_ip}"
image_id = "${var.ami}"
instance_type = "${var.type}"
key_name = "${var.ssh_key}"
security_groups = ["${var.sec_group_list}"]
lifecycle {
create_before_destroy = true
}
lifecycle {
create_before_destroy = true
}
}
# Create an Auto Scaling group for the instance cluster
resource "aws_autoscaling_group" "asg" {
name = "${var.name}-asg"
min_size = "${var.min_size}"
max_size = "${var.max_size}"
desired_capacity = "${var.min_size}"
vpc_zone_identifier = ["${var.subnet_list}"]
launch_configuration = "${aws_launch_configuration.lc.name}"
name = "${var.name}-asg"
min_size = "${var.min_size}"
max_size = "${var.max_size}"
desired_capacity = "${var.min_size}"
vpc_zone_identifier = ["${var.subnet_list}"]
launch_configuration = "${aws_launch_configuration.lc.name}"
lifecycle {
create_before_destroy = true
}
lifecycle {
create_before_destroy = true
}
}

View file

@ -1,44 +1,44 @@
variable "name" {
type = "string"
description = "Name to use for instances in this instance cluster"
type = "string"
description = "Name to use for instances in this instance cluster"
}
variable "ami" {
type = "string"
description = "AMI to use for instances in this instance cluster"
type = "string"
description = "AMI to use for instances in this instance cluster"
}
variable "type" {
type = "string"
description = "Type to use for instances in this instance cluster"
type = "string"
description = "Type to use for instances in this instance cluster"
}
variable "ssh_key" {
type = "string"
description = "SSH key to inject into instances in this instance cluster"
type = "string"
description = "SSH key to inject into instances in this instance cluster"
}
variable "assign_pub_ip" {
type = "string"
description = "True/False to assign a public IP address"
type = "string"
description = "True/False to assign a public IP address"
}
variable "sec_group_list" {
type = "list"
description = "List of security group IDs for instances to use"
type = "list"
description = "List of security group IDs for instances to use"
}
variable "min_size" {
type = "string"
description = "Minimum number of instances in this instance cluster"
type = "string"
description = "Minimum number of instances in this instance cluster"
}
variable "max_size" {
type = "string"
description = "Maximum number of instances in this instance cluster"
type = "string"
description = "Maximum number of instances in this instance cluster"
}
variable "subnet_list" {
type = "list"
description = "List of subnets where instances should be launched"
type = "list"
description = "List of subnets where instances should be launched"
}

View file

@ -1,3 +1,3 @@
data "aws_availability_zones" "az_list" {
state = "available"
state = "available"
}

View file

@ -1,52 +1,52 @@
# Create a new VPC
resource "aws_vpc" "vpc" {
cidr_block = "${var.vpc_cidr_block}"
enable_dns_hostnames = "${var.vpc_dns_hostnames}"
enable_dns_support = "${var.vpc_dns_support}"
cidr_block = "${var.vpc_cidr_block}"
enable_dns_hostnames = "${var.vpc_dns_hostnames}"
enable_dns_support = "${var.vpc_dns_support}"
tags {
Name = "${var.name}_vpc"
tool = "terraform"
}
tags {
Name = "${var.name}_vpc"
tool = "terraform"
}
}
# Modify default VPC security group to allow inbound SSH
resource "aws_security_group_rule" "allow_ssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_vpc.vpc.default_security_group_id}"
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_vpc.vpc.default_security_group_id}"
}
# Create a new Internet gateway
resource "aws_internet_gateway" "gateway" {
vpc_id = "${aws_vpc.vpc.id}"
vpc_id = "${aws_vpc.vpc.id}"
tags {
Name = "${var.name}_igw"
tool = "terraform"
}
tags {
Name = "${var.name}_igw"
tool = "terraform"
}
}
# Add default route to VPC's main route table
resource "aws_route" "default_route" {
route_table_id = "${aws_vpc.vpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gateway.id}"
route_table_id = "${aws_vpc.vpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gateway.id}"
}
# Create a public subnet in the new VPC
resource "aws_subnet" "subnet" {
count = "${length(data.aws_availability_zones.az_list.names)}"
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${cidrsubnet(var.vpc_cidr_block, 4, count.index)}"
availability_zone = "${element(data.aws_availability_zones.az_list.names, count.index)}"
map_public_ip_on_launch = "${var.subnet_map_pub_ip}"
count = "${length(data.aws_availability_zones.az_list.names)}"
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${cidrsubnet(var.vpc_cidr_block, 4, count.index)}"
availability_zone = "${element(data.aws_availability_zones.az_list.names, count.index)}"
map_public_ip_on_launch = "${var.subnet_map_pub_ip}"
tags {
Name = "${var.name}_subnet_${count.index}"
tool = "terraform"
}
tags {
Name = "${var.name}_subnet_${count.index}"
tool = "terraform"
}
}

View file

@ -1,15 +1,15 @@
output "vpc_id" {
value = "${aws_vpc.vpc.id}"
value = "${aws_vpc.vpc.id}"
}
output "subnet_id" {
value = "${aws_subnet.subnet.*.id}"
value = "${aws_subnet.subnet.*.id}"
}
output "subnet_az" {
value = "${aws_subnet.subnet.*.availability_zone}"
value = "${aws_subnet.subnet.*.availability_zone}"
}
output "default_sg_id" {
value = "${aws_vpc.vpc.default_security_group_id}"
value = "${aws_vpc.vpc.default_security_group_id}"
}

View file

@ -1,24 +1,24 @@
variable "name" {
type = "string"
description = "Name prefix to use for networking resources"
type = "string"
description = "Name prefix to use for networking resources"
}
variable "vpc_cidr_block" {
type = "string"
description = "CIDR block to use for new VPC"
type = "string"
description = "CIDR block to use for new VPC"
}
variable "vpc_dns_hostnames" {
type = "string"
description = "True/False to enable DNS hostnames in new VPC"
type = "string"
description = "True/False to enable DNS hostnames in new VPC"
}
variable "vpc_dns_support" {
type = "string"
description = "True/False to enable DNS support in new VPC"
type = "string"
description = "True/False to enable DNS support in new VPC"
}
variable "subnet_map_pub_ip" {
type = "string"
description = "True/False to map public IP addresses on launch"
type = "string"
description = "True/False to map public IP addresses on launch"
}

View file

@ -1,3 +1,3 @@
provider "aws" {
region = "${var.user_region}"
region = "${var.user_region}"
}

View file

@ -1,15 +1,15 @@
variable "user_region" {
type = "string"
description = "AWS region to use for all resources"
type = "string"
description = "AWS region to use for all resources"
}
variable "node_type" {
type = "string"
description = "Type of instance to use"
default = "t2.micro"
type = "string"
description = "Type of instance to use"
default = "t2.micro"
}
variable "key_pair" {
type = "string"
description = "SSH keypair to use for accessing instances"
type = "string"
description = "SSH keypair to use for accessing instances"
}

View file

@ -1,16 +1,19 @@
data "aws_ami" "node_ami" {
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["*ubuntu-xenial-16.04*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["*ubuntu-xenial-16.04*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
}

View file

@ -1,33 +1,33 @@
module "vpc20" {
source = "./modules/vpc"
source = "./modules/vpc"
name = "etcd"
vpc_cidr_block = "10.20.0.0/16"
vpc_dns_hostnames = "true"
vpc_dns_support = "true"
subnet_map_pub_ip = "true"
name = "etcd"
vpc_cidr_block = "10.20.0.0/16"
vpc_dns_hostnames = "true"
vpc_dns_support = "true"
subnet_map_pub_ip = "true"
}
module "etcd" {
source = "./modules/instance-cluster"
source = "./modules/instance-cluster"
name = "etcd"
ami = "${data.aws_ami.node_ami.id}"
type = "${var.node_type}"
assign_pub_ip = true
ssh_key = "${var.key_pair}"
cluster_size = 3
subnet_list = ["${module.vpc20.subnet_id}"]
sec_group_list = ["${module.vpc20.default_sg_id}"]
role = "etcd"
name = "etcd"
ami = "${data.aws_ami.node_ami.id}"
type = "${var.node_type}"
assign_pub_ip = true
ssh_key = "${var.key_pair}"
cluster_size = 3
subnet_list = ["${module.vpc20.subnet_id}"]
sec_group_list = ["${module.vpc20.default_sg_id}"]
role = "etcd"
}
module "vpc50" {
source = "./modules/vpc"
source = "./modules/vpc"
name = "k8s"
vpc_cidr_block = "10.50.0.0/16"
vpc_dns_hostnames = "true"
vpc_dns_support = "true"
subnet_map_pub_ip = "true"
name = "k8s"
vpc_cidr_block = "10.50.0.0/16"
vpc_dns_hostnames = "true"
vpc_dns_support = "true"
subnet_map_pub_ip = "true"
}

View file

@ -1,15 +1,15 @@
# Launch a cluster of instances
resource "aws_instance" "instance" {
count = "${var.cluster_size}"
ami = "${var.ami}"
instance_type = "${var.type}"
subnet_id = "${element(var.subnet_list, count.index)}"
key_name = "${var.ssh_key}"
associate_public_ip_address = "${var.assign_pub_ip}"
vpc_security_group_ids = ["${var.sec_group_list}"]
count = "${var.cluster_size}"
ami = "${var.ami}"
instance_type = "${var.type}"
subnet_id = "${element(var.subnet_list, count.index)}"
key_name = "${var.ssh_key}"
associate_public_ip_address = "${var.assign_pub_ip}"
vpc_security_group_ids = ["${var.sec_group_list}"]
tags {
Name = "${var.name}-${count.index}"
Role = "${var.role}"
}
tags {
Name = "${var.name}-${count.index}"
Role = "${var.role}"
}
}

View file

@ -1,11 +1,11 @@
output "cluster_instance_public_addresses" {
value = "${aws_instance.instance.*.public_ip}"
value = "${aws_instance.instance.*.public_ip}"
}
output "cluster_instance_private_addresses" {
value = "${aws_instance.instance.*.private_ip}"
value = "${aws_instance.instance.*.private_ip}"
}
output "cluster_instance_ids" {
value = "${aws_instance.instance.*.id}"
value = "${aws_instance.instance.*.id}"
}

View file

@ -1,44 +1,44 @@
variable "name" {
type = "string"
description = "Name to use for instances in this instance cluster"
type = "string"
description = "Name to use for instances in this instance cluster"
}
variable "ami" {
type = "string"
description = "AMI to use for instances in this instance cluster"
type = "string"
description = "AMI to use for instances in this instance cluster"
}
variable "type" {
type = "string"
description = "Type to use for instances in this instance cluster"
type = "string"
description = "Type to use for instances in this instance cluster"
}
variable "assign_pub_ip" {
type = "string"
description = "True/False to assign a public IP address"
type = "string"
description = "True/False to assign a public IP address"
}
variable "ssh_key" {
type = "string"
description = "SSH key to inject into instances in this instance cluster"
type = "string"
description = "SSH key to inject into instances in this instance cluster"
}
variable "cluster_size" {
type = "string"
description = "Number of instances in this instance cluster"
type = "string"
description = "Number of instances in this instance cluster"
}
variable "subnet_list" {
type = "list"
description = "List of subnet IDs where to launch instances"
type = "list"
description = "List of subnet IDs where to launch instances"
}
variable "sec_group_list" {
type = "list"
description = "List of security group IDs for instances to use"
type = "list"
description = "List of security group IDs for instances to use"
}
variable "role" {
type = "string"
description = "Value to assign to the Role tag on instances"
type = "string"
description = "Value to assign to the Role tag on instances"
}

View file

@ -1,3 +1,3 @@
data "aws_availability_zones" "az_list" {
state = "available"
state = "available"
}

View file

@ -1,52 +1,52 @@
# Create a new VPC
resource "aws_vpc" "vpc" {
cidr_block = "${var.vpc_cidr_block}"
enable_dns_hostnames = "${var.vpc_dns_hostnames}"
enable_dns_support = "${var.vpc_dns_support}"
cidr_block = "${var.vpc_cidr_block}"
enable_dns_hostnames = "${var.vpc_dns_hostnames}"
enable_dns_support = "${var.vpc_dns_support}"
tags {
Name = "${var.name}_vpc"
tool = "terraform"
}
tags {
Name = "${var.name}_vpc"
tool = "terraform"
}
}
# Modify default VPC security group to allow inbound SSH
resource "aws_security_group_rule" "allow_ssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_vpc.vpc.default_security_group_id}"
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_vpc.vpc.default_security_group_id}"
}
# Create a new Internet gateway
resource "aws_internet_gateway" "gateway" {
vpc_id = "${aws_vpc.vpc.id}"
vpc_id = "${aws_vpc.vpc.id}"
tags {
Name = "${var.name}_igw"
tool = "terraform"
}
tags {
Name = "${var.name}_igw"
tool = "terraform"
}
}
# Add default route to VPC's main route table
resource "aws_route" "default_route" {
route_table_id = "${aws_vpc.vpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gateway.id}"
route_table_id = "${aws_vpc.vpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gateway.id}"
}
# Create a public subnet in the new VPC
resource "aws_subnet" "subnet" {
count = "${length(data.aws_availability_zones.az_list.names)}"
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${cidrsubnet(var.vpc_cidr_block, 4, count.index)}"
availability_zone = "${element(data.aws_availability_zones.az_list.names, count.index)}"
map_public_ip_on_launch = "${var.subnet_map_pub_ip}"
count = "${length(data.aws_availability_zones.az_list.names)}"
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${cidrsubnet(var.vpc_cidr_block, 4, count.index)}"
availability_zone = "${element(data.aws_availability_zones.az_list.names, count.index)}"
map_public_ip_on_launch = "${var.subnet_map_pub_ip}"
tags {
Name = "${var.name}_subnet_${count.index}"
tool = "terraform"
}
tags {
Name = "${var.name}_subnet_${count.index}"
tool = "terraform"
}
}

View file

@ -1,15 +1,15 @@
output "vpc_id" {
value = "${aws_vpc.vpc.id}"
value = "${aws_vpc.vpc.id}"
}
output "subnet_id" {
value = "${aws_subnet.subnet.*.id}"
value = "${aws_subnet.subnet.*.id}"
}
output "subnet_az" {
value = "${aws_subnet.subnet.*.availability_zone}"
value = "${aws_subnet.subnet.*.availability_zone}"
}
output "default_sg_id" {
value = "${aws_vpc.vpc.default_security_group_id}"
value = "${aws_vpc.vpc.default_security_group_id}"
}

View file

@ -1,24 +1,24 @@
variable "name" {
type = "string"
description = "Name prefix to use for networking resources"
type = "string"
description = "Name prefix to use for networking resources"
}
variable "vpc_cidr_block" {
type = "string"
description = "CIDR block to use for new VPC"
type = "string"
description = "CIDR block to use for new VPC"
}
variable "vpc_dns_hostnames" {
type = "string"
description = "True/False to enable DNS hostnames in new VPC"
type = "string"
description = "True/False to enable DNS hostnames in new VPC"
}
variable "vpc_dns_support" {
type = "string"
description = "True/False to enable DNS support in new VPC"
type = "string"
description = "True/False to enable DNS support in new VPC"
}
variable "subnet_map_pub_ip" {
type = "string"
description = "True/False to map public IP addresses on launch"
type = "string"
description = "True/False to map public IP addresses on launch"
}

View file

@ -1,3 +1,3 @@
provider "aws" {
region = "${var.user_region}"
region = "${var.user_region}"
}

View file

@ -1,14 +1,14 @@
variable "user_region" {
type = "string"
description = "AWS region to use for all resources"
type = "string"
description = "AWS region to use for all resources"
}
variable "node_type" {
type = "string"
description = "Type of instance to use"
type = "string"
description = "Type of instance to use"
}
variable "key_pair" {
type = "string"
description = "SSH keypair to use for accessing instances"
type = "string"
description = "SSH keypair to use for accessing instances"
}

View file

@ -1,18 +1,21 @@
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*"]
}
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

@ -1,80 +1,89 @@
# 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"
}
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"
}
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"
}
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"
}
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"
}
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}"
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"
}
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

@ -1,3 +1,3 @@
output "instance_public_ip" {
value = "${aws_instance.coreos01.public_ip}"
value = "${aws_instance.coreos01.public_ip}"
}

View file

@ -1,3 +1,3 @@
provider "aws" {
region = "${var.user_region}"
region = "${var.user_region}"
}

View file

@ -1,14 +1,14 @@
variable "user_region" {
type = "string"
description = "AWS region in which all resources will be created"
type = "string"
description = "AWS region in which all resources will be created"
}
variable "keypair" {
type = "string"
description = "AWS SSH keypair to use to connect to instances"
type = "string"
description = "AWS SSH keypair to use to connect to instances"
}
variable "flavor" {
type = "string"
description = "AWS type to use when creating instances"
type = "string"
description = "AWS type to use when creating instances"
}

View file

@ -1,34 +1,38 @@
data "aws_ami" "ubuntu-1404-ami" {
most_recent = true
owners = ["099720109477"]
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "name"
values = ["*ubuntu-trusty-14.04*"]
}
most_recent = true
owners = ["099720109477"]
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "name"
values = ["*ubuntu-trusty-14.04*"]
}
}
data "aws_vpc" "default-vpc" {
filter {
name = "isDefault"
values = ["true"]
}
filter {
name = "isDefault"
values = ["true"]
}
}
data "aws_subnet" "default-subnet" {
filter {
name = "cidrBlock"
values = ["172.31.16.0/20"]
}
filter {
name = "cidrBlock"
values = ["172.31.16.0/20"]
}
}

View file

@ -1,11 +1,12 @@
# Create a new Ubuntu 14.04 instance using values from vars.tf and data.tf
resource "aws_instance" "test-01" {
ami = "${data.aws_ami.ubuntu-1404-ami.id}"
instance_type = "${var.flavor}"
key_name = "${var.keypair}"
vpc_security_group_ids = ["${var.sec-group}"]
subnet_id = "${data.aws_subnet.default-subnet.id}"
tags {
Name = "terraform"
}
ami = "${data.aws_ami.ubuntu-1404-ami.id}"
instance_type = "${var.flavor}"
key_name = "${var.keypair}"
vpc_security_group_ids = ["${var.sec-group}"]
subnet_id = "${data.aws_subnet.default-subnet.id}"
tags {
Name = "terraform"
}
}

View file

@ -1,3 +1,3 @@
output "instance_ip_address" {
value = "${aws_instance.test-01.public_ip}"
value = "${aws_instance.test-01.public_ip}"
}

View file

@ -1,3 +1,3 @@
provider "aws" {
region = "${var.user_region}"
region = "${var.user_region}"
}

View file

@ -1,19 +1,19 @@
variable "user_region" {
type = "string"
description = "AWS region in which to create all resources"
type = "string"
description = "AWS region in which to create all resources"
}
variable "keypair" {
type = "string"
description = "AWS SSH keypair to use to connect to instances"
type = "string"
description = "AWS SSH keypair to use to connect to instances"
}
variable "flavor" {
type = "string"
description = "AWS type to use when creating instances"
type = "string"
description = "AWS type to use when creating instances"
}
variable "sec-group" {
type = "string"
description = "AWS security group to apply to instances"
type = "string"
description = "AWS security group to apply to instances"
}

View file

@ -1,23 +1,23 @@
module "vpc20" {
source = "./modules/vpc"
source = "./modules/vpc"
name = "test-vpc-1"
vpc_cidr_block = "10.20.0.0/16"
vpc_dns_hostnames = "true"
vpc_dns_support = "true"
name = "test-vpc-1"
vpc_cidr_block = "10.20.0.0/16"
vpc_dns_hostnames = "true"
vpc_dns_support = "true"
subnet_cidr_block = "10.20.1.0/24"
subnet_map_pub_ip = "true"
subnet_cidr_block = "10.20.1.0/24"
subnet_map_pub_ip = "true"
}
module "vpc50" {
source = "./modules/vpc"
source = "./modules/vpc"
name = "test-vpc-2"
vpc_cidr_block = "10.50.0.0/16"
vpc_dns_hostnames = "true"
vpc_dns_support = "true"
name = "test-vpc-2"
vpc_cidr_block = "10.50.0.0/16"
vpc_dns_hostnames = "true"
vpc_dns_support = "true"
subnet_cidr_block = "10.50.1.0/24"
subnet_map_pub_ip = "true"
subnet_cidr_block = "10.50.1.0/24"
subnet_map_pub_ip = "true"
}

View file

@ -1,36 +1,36 @@
# Create a new VPC
resource "aws_vpc" "vpc" {
cidr_block = "${var.vpc_cidr_block}"
enable_dns_hostnames = "${var.vpc_dns_hostnames}"
enable_dns_support = "${var.vpc_dns_support}"
cidr_block = "${var.vpc_cidr_block}"
enable_dns_hostnames = "${var.vpc_dns_hostnames}"
enable_dns_support = "${var.vpc_dns_support}"
}
# Create a public subnet in the new VPC
resource "aws_subnet" "subnet" {
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${var.subnet_cidr_block}"
map_public_ip_on_launch = "${var.subnet_map_pub_ip}"
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${var.subnet_cidr_block}"
map_public_ip_on_launch = "${var.subnet_map_pub_ip}"
}
# Create a new Internet gateway
resource "aws_internet_gateway" "gateway" {
vpc_id = "${aws_vpc.vpc.id}"
vpc_id = "${aws_vpc.vpc.id}"
}
# Create a route table for the new VPC
resource "aws_route_table" "routes" {
vpc_id = "${aws_vpc.vpc.id}"
vpc_id = "${aws_vpc.vpc.id}"
}
# Create a route in new route table
resource "aws_route" "default_route" {
route_table_id = "${aws_route_table.routes.id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gateway.id}"
route_table_id = "${aws_route_table.routes.id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gateway.id}"
}
# Associate route table with subnet in VPC
resource "aws_route_table_association" "rte_tbl_assoc" {
subnet_id = "${aws_subnet.subnet.id}"
route_table_id = "${aws_route_table.routes.id}"
subnet_id = "${aws_subnet.subnet.id}"
route_table_id = "${aws_route_table.routes.id}"
}

View file

@ -1,11 +1,11 @@
output "new_vpc_id" {
value = "${aws_vpc.vpc.id}"
value = "${aws_vpc.vpc.id}"
}
output "new_subnet_id" {
value = "${aws_subnet.subnet.id}"
value = "${aws_subnet.subnet.id}"
}
output "new_subnet_az" {
value = "${aws_subnet.subnet.availability_zone}"
value = "${aws_subnet.subnet.availability_zone}"
}

View file

@ -1,29 +1,29 @@
variable "name" {
type = "string"
description = "Name prefix to use for networking resources"
type = "string"
description = "Name prefix to use for networking resources"
}
variable "vpc_cidr_block" {
type = "string"
description = "CIDR block to use for new VPC"
type = "string"
description = "CIDR block to use for new VPC"
}
variable "vpc_dns_hostnames" {
type = "string"
description = "True/False to enable DNS hostnames in new VPC"
type = "string"
description = "True/False to enable DNS hostnames in new VPC"
}
variable "vpc_dns_support" {
type = "string"
description = "True/False to enable DNS support in new VPC"
type = "string"
description = "True/False to enable DNS support in new VPC"
}
variable "subnet_cidr_block" {
type = "string"
description = "CIDR block (in VPC CIDR block) to use for new subnet"
type = "string"
description = "CIDR block (in VPC CIDR block) to use for new subnet"
}
variable "subnet_map_pub_ip" {
type = "string"
description = "True/False to map public IP addresses on launch"
type = "string"
description = "True/False to map public IP addresses on launch"
}

View file

@ -1,3 +1,3 @@
provider "aws" {
region = "${var.user_region}"
region = "${var.user_region}"
}

View file

@ -1,4 +1,4 @@
variable "user_region" {
type = "string"
description = "AWS region to use for all resources"
type = "string"
description = "AWS region to use for all resources"
}

View file

@ -1,20 +1,23 @@
data "aws_availability_zones" "avail_zones" {
state = "available"
state = "available"
}
data "aws_ami" "node_ami" {
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["*ubuntu-xenial-16.04*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["*ubuntu-xenial-16.04*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
}

View file

@ -1,71 +1,75 @@
# Create a new VPC
resource "aws_vpc" "demo_vpc" {
cidr_block = "${var.assigned_cidr}"
enable_dns_hostnames = "true"
enable_dns_support = "true"
tags {
Name = "multi_az_vpc"
tool = "terraform"
demo = "terraform"
area = "networking"
}
cidr_block = "${var.assigned_cidr}"
enable_dns_hostnames = "true"
enable_dns_support = "true"
tags {
Name = "multi_az_vpc"
tool = "terraform"
demo = "terraform"
area = "networking"
}
}
# Modify default security group to allow inbound SSH
resource "aws_security_group_rule" "allow_ssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_vpc.demo_vpc.default_security_group_id}"
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_vpc.demo_vpc.default_security_group_id}"
}
# Create a new Internet gateway
resource "aws_internet_gateway" "demo_gw" {
vpc_id = "${aws_vpc.demo_vpc.id}"
tags {
Name = "multi_az_gw"
tool = "terraform"
demo = "terraform"
area = "networking"
}
vpc_id = "${aws_vpc.demo_vpc.id}"
tags {
Name = "multi_az_gw"
tool = "terraform"
demo = "terraform"
area = "networking"
}
}
# Add default route to VPC's main route table
resource "aws_route" "default" {
route_table_id = "${aws_vpc.demo_vpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.demo_gw.id}"
route_table_id = "${aws_vpc.demo_vpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.demo_gw.id}"
}
# Create public subnet(s) in the new VPC
resource "aws_subnet" "demo_subnet" {
count = "${length(data.aws_availability_zones.avail_zones.names)}"
vpc_id = "${aws_vpc.demo_vpc.id}"
cidr_block = "${cidrsubnet(var.assigned_cidr, 8, count.index)}"
availability_zone = "${element(data.aws_availability_zones.avail_zones.names, count.index)}"
map_public_ip_on_launch = "true"
tags {
Name = "demo_subnet_${count.index}"
tool = "terraform"
demo = "terraform"
area = "networking"
}
count = "${length(data.aws_availability_zones.avail_zones.names)}"
vpc_id = "${aws_vpc.demo_vpc.id}"
cidr_block = "${cidrsubnet(var.assigned_cidr, 8, count.index)}"
availability_zone = "${element(data.aws_availability_zones.avail_zones.names, count.index)}"
map_public_ip_on_launch = "true"
tags {
Name = "demo_subnet_${count.index}"
tool = "terraform"
demo = "terraform"
area = "networking"
}
}
# Launch instances across subnets/AZs
resource "aws_instance" "node" {
count = "${var.num_nodes}"
ami = "${data.aws_ami.node_ami.id}"
instance_type = "${var.node_type}"
key_name = "${var.key_pair}"
vpc_security_group_ids = ["${aws_vpc.demo_vpc.default_security_group_id}"]
subnet_id = "${element(aws_subnet.demo_subnet.*.id, count.index)}"
tags {
Name = "node-${count.index}"
tool = "terraform"
demo = "terraform"
area = "compute"
}
count = "${var.num_nodes}"
ami = "${data.aws_ami.node_ami.id}"
instance_type = "${var.node_type}"
key_name = "${var.key_pair}"
vpc_security_group_ids = ["${aws_vpc.demo_vpc.default_security_group_id}"]
subnet_id = "${element(aws_subnet.demo_subnet.*.id, count.index)}"
tags {
Name = "node-${count.index}"
tool = "terraform"
demo = "terraform"
area = "compute"
}
}

View file

@ -1,3 +1,3 @@
provider "aws" {
region = "${var.user_region}"
region = "${var.user_region}"
}

View file

@ -1,24 +1,24 @@
variable "user_region" {
type = "string"
description = "AWS region to use for new resources"
type = "string"
description = "AWS region to use for new resources"
}
variable "assigned_cidr" {
type = "string"
description = "CIDR block to use for VPC and subnets"
type = "string"
description = "CIDR block to use for VPC and subnets"
}
variable "node_type" {
type = "string"
description = "Type of instance to launch (such as t2.micro)"
type = "string"
description = "Type of instance to launch (such as t2.micro)"
}
variable "key_pair" {
type = "string"
description = "SSH keypair to inject into launched instances"
type = "string"
description = "SSH keypair to inject into launched instances"
}
variable "num_nodes" {
type = "string"
description = "Number of nodes to launch"
type = "string"
description = "Number of nodes to launch"
}