mirror of
https://codeberg.org/scottslowe/learning-tools.git
synced 2026-03-11 09:04:37 +00:00
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:
parent
a961d689c8
commit
525db796e6
5 changed files with 117 additions and 0 deletions
15
terraform/aws/vpc-all-azs/compute.tf
Normal file
15
terraform/aws/vpc-all-azs/compute.tf
Normal 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"
|
||||
}
|
||||
}
|
||||
20
terraform/aws/vpc-all-azs/data.tf
Normal file
20
terraform/aws/vpc-all-azs/data.tf
Normal 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"]
|
||||
}
|
||||
}
|
||||
55
terraform/aws/vpc-all-azs/networking.tf
Normal file
55
terraform/aws/vpc-all-azs/networking.tf
Normal 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"
|
||||
}
|
||||
}
|
||||
3
terraform/aws/vpc-all-azs/provider.tf
Normal file
3
terraform/aws/vpc-all-azs/provider.tf
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
provider "aws" {
|
||||
region = "${var.user_region}"
|
||||
}
|
||||
24
terraform/aws/vpc-all-azs/variables.tf
Normal file
24
terraform/aws/vpc-all-azs/variables.tf
Normal 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"
|
||||
}
|
||||
Loading…
Reference in a new issue