diff --git a/terraform/aws/vpc-all-azs/compute.tf b/terraform/aws/vpc-all-azs/compute.tf new file mode 100644 index 0000000..16c87d3 --- /dev/null +++ b/terraform/aws/vpc-all-azs/compute.tf @@ -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" + } +} diff --git a/terraform/aws/vpc-all-azs/data.tf b/terraform/aws/vpc-all-azs/data.tf new file mode 100644 index 0000000..17ee05d --- /dev/null +++ b/terraform/aws/vpc-all-azs/data.tf @@ -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"] + } +} diff --git a/terraform/aws/vpc-all-azs/networking.tf b/terraform/aws/vpc-all-azs/networking.tf new file mode 100644 index 0000000..64bd3d3 --- /dev/null +++ b/terraform/aws/vpc-all-azs/networking.tf @@ -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" + } +} diff --git a/terraform/aws/vpc-all-azs/provider.tf b/terraform/aws/vpc-all-azs/provider.tf new file mode 100644 index 0000000..d7c130e --- /dev/null +++ b/terraform/aws/vpc-all-azs/provider.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = "${var.user_region}" +} diff --git a/terraform/aws/vpc-all-azs/variables.tf b/terraform/aws/vpc-all-azs/variables.tf new file mode 100644 index 0000000..b084636 --- /dev/null +++ b/terraform/aws/vpc-all-azs/variables.tf @@ -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" +}