Add files for instance-cluster environment

Commit files to show use of 2 separate Terraform modules, including the "instance-cluster" module to spin up a cluster of instances

Signed-off-by: Scott Lowe <scott.lowe@scottlowe.org>
This commit is contained in:
Scott Lowe 2017-09-24 22:04:11 -06:00
parent 4736fc3a5f
commit e260eb828e
No known key found for this signature in database
GPG key ID: 949F43F6E6C11780
11 changed files with 230 additions and 0 deletions

View file

@ -0,0 +1,16 @@
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,33 @@
module "vpc20" {
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"
}
module "etcd" {
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"
}
module "vpc50" {
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"
}

View file

@ -0,0 +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}"]
tags {
Name = "${var.name}-${count.index}"
Role = "${var.role}"
}
}

View file

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

View file

@ -0,0 +1,44 @@
variable "name" {
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"
}
variable "type" {
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"
}
variable "ssh_key" {
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"
}
variable "subnet_list" {
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"
}
variable "role" {
type = "string"
description = "Value to assign to the Role tag on instances"
}

View file

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

View file

@ -0,0 +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}"
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}"
}
# Create a new Internet gateway
resource "aws_internet_gateway" "gateway" {
vpc_id = "${aws_vpc.vpc.id}"
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}"
}
# 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}"
tags {
Name = "${var.name}_subnet_${count.index}"
tool = "terraform"
}
}

View file

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

View file

@ -0,0 +1,24 @@
variable "name" {
type = "string"
description = "Name prefix to use for networking resources"
}
variable "vpc_cidr_block" {
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"
}
variable "vpc_dns_support" {
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"
}

View file

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

View file

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