Commit multi-AZ Terraform configurations

Commit a set of Terraform configurations that support multi-AZ deployments

Signed-off-by: Scott Lowe <scott.lowe@scottlowe.org>
This commit is contained in:
Scott Lowe 2017-09-21 23:28:51 -06:00
parent a961d689c8
commit 525db796e6
No known key found for this signature in database
GPG key ID: 949F43F6E6C11780
5 changed files with 117 additions and 0 deletions

View file

@ -0,0 +1,15 @@
# 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"
}
}

View file

@ -0,0 +1,20 @@
data "aws_availability_zones" "avail_zones" {
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"]
}
}

View file

@ -0,0 +1,55 @@
# 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"
}
}
# 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}"
}
# 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"
}
}
# 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}"
}
# 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"
}
}

View file

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

View file

@ -0,0 +1,24 @@
variable "user_region" {
type = "string"
description = "AWS region to use for new resources"
}
variable "assigned_cidr" {
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)"
}
variable "key_pair" {
type = "string"
description = "SSH keypair to inject into launched instances"
}
variable "num_nodes" {
type = "string"
description = "Number of nodes to launch"
}