seperate east/west

This commit is contained in:
Greg Hendrickson
2023-11-17 09:46:38 -08:00
parent e2e0983404
commit 48ed0ca1d6
28 changed files with 396 additions and 243 deletions

View File

@@ -4,13 +4,17 @@ provider "aws" {
}
# Define modules
module "vpc_us" {
source = "./modules/vpc"
module "vpc-east" {
source = "./modules/vpc/vpc-east"
region = "us-east-1"
us_east_subnet_1_id = module.subnets_us_east.us_east_subnet_1_id
us_east_subnet_2_id = module.subnets_us_east.us_east_subnet_2_id
us_east_subnet_3_id = module.subnets_us_east.us_east_subnet_3_id
}
module "vpc-west" {
source = "./modules/vpc/vpc-west"
region = "us-west-2"
us_west_subnet_1_id = module.subnets_us_west.us_west_subnet_1_id
us_west_subnet_2_id = module.subnets_us_west.us_west_subnet_2_id
us_west_subnet_3_id = module.subnets_us_west.us_west_subnet_3_id
@@ -18,28 +22,36 @@ module "vpc_us" {
module "subnets_us_west" {
source = "./modules/vpc/subnets/us-west"
vpc_id = module.vpc_us.vpc_id
vpc_id_west_2 = module.vpc-west.vpc_id_west_2
}
module "subnets_us_east" {
source = "./modules/vpc/subnets/us-east"
vpc_id = module.vpc_us.vpc_id
vpc_id_east_1 = module.vpc-east.vpc_id_east_1
}
module "ec2" {
source = "./modules/ec2"
source = "./modules/ec2/ec2-east"
us_east_subnet_1_id = module.subnets_us_east.us_east_subnet_1_id
us_east_subnet_2_id = module.subnets_us_east.us_east_subnet_2_id
us_east_subnet_3_id = module.subnets_us_east.us_east_subnet_3_id
vpc_id_east_1 = module.vpc-east.vpc_id_east_1
}
module "ec2-west" {
source = "./modules/ec2/ec2-west"
us_west_subnet_1_id = module.subnets_us_west.us_west_subnet_1_id
us_west_subnet_2_id = module.subnets_us_west.us_west_subnet_2_id
us_west_subnet_3_id = module.subnets_us_west.us_west_subnet_3_id
vpc_id = module.vpc_us.vpc_id
vpc_id_west_2 = module.vpc-west.vpc_id_west_2
}
module "elb" {
source = "./modules/elb"
}

View File

@@ -0,0 +1,71 @@
provider "aws" {
region = "us-east-1"
}
# Create a security group for the EC2 instance
resource "aws_security_group" "instance-east" {
name_prefix = "instance-east-"
vpc_id = var.vpc_id_east_1
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "instance-security-group-east"
}
}
# Define the Launch Configuration
resource "aws_launch_configuration" "lc-east" {
name_prefix = "lc-east-"
image_id = "ami-0237a465e7f465b10"
instance_type = "t3.small"
security_groups = [
aws_security_group.instance-east.id
]
user_data = file("${path.module}/user-data.sh")
root_block_device {
volume_size = 300
}
}
# Define the Auto Scaling Group
resource "aws_autoscaling_group" "asg-east" {
name_prefix = "asg-east-"
launch_configuration = aws_launch_configuration.lc-east.id
depends_on = [
aws_launch_configuration.lc-east,
]
vpc_zone_identifier = [
var.us_east_subnet_1_id,
var.us_east_subnet_2_id,
var.us_east_subnet_3_id,
]
min_size = var.min_size
max_size = var.max_size
desired_capacity = var.min_size
health_check_grace_period = 300
health_check_type = "EC2"
termination_policies = ["OldestInstance"]
tag {
key = "Name"
value = "asg-instance-east"
propagate_at_launch = true
}
}
data "aws_instances" "asg_instances-east" {
instance_tags = {
"aws:autoscaling:groupName" = aws_autoscaling_group.asg-east.name
}
}

View File

@@ -28,22 +28,10 @@ variable "us_east_subnet_3_id" {
type = string
}
variable "us_west_subnet_1_id" {
description = "The ID of the first US West subnet"
type = string
}
variable "us_west_subnet_2_id" {
description = "The ID of the second US West subnet"
type = string
}
variable "us_west_subnet_3_id" {
description = "The ID of the third US West subnet"
type = string
}
variable "vpc_id" {
variable "vpc_id_east_1" {
description = "The ID of the VPC"
type = string
}
}

View File

@@ -0,0 +1,72 @@
provider "aws" {
region = "us-west-2"
}
# Create a security group for the EC2 instance
resource "aws_security_group" "instance-west" {
name_prefix = "instance-west-"
vpc_id = var.vpc_id_west_2
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "instance-security-group-west"
}
}
# Define the Launch Configuration
resource "aws_launch_configuration" "lc-west" {
name_prefix = "lc-west"
image_id = "ami-03bf1eb153d14803f"
instance_type = "t3.small"
security_groups = [
aws_security_group.instance-west.id
]
user_data = file("${path.module}/user-data.sh")
root_block_device {
volume_size = 300
}
}
# Define the Auto Scaling Group
resource "aws_autoscaling_group" "asg-west" {
name_prefix = "asg-west-"
launch_configuration = aws_launch_configuration.lc-west.id
depends_on = [
aws_launch_configuration.lc-west,
]
vpc_zone_identifier = [
var.us_west_subnet_1_id,
var.us_west_subnet_2_id,
var.us_west_subnet_3_id
]
min_size = var.min_size
max_size = var.max_size
desired_capacity = var.min_size
health_check_grace_period = 300
health_check_type = "EC2"
termination_policies = ["OldestInstance"]
tag {
key = "Name"
value = "asg-instance-west"
propagate_at_launch = true
}
}
data "aws_instances" "asg_instances-west" {
instance_tags = {
"aws:autoscaling:groupName" = aws_autoscaling_group.asg-west.name
}
}

View File

@@ -0,0 +1,4 @@
# output "public_ips" {
# description = "Public IP addresses of the instances in the Auto Scaling group"
# value = [for i in data.aws_instances.asg_instances.ids : aws_instance[i].public_ip]
# }

View File

@@ -0,0 +1,2 @@
min_size = 1
max_size = 10

View File

@@ -0,0 +1,29 @@
#!/bin/bash
# Install necessary packages
sudo apt-get update
sudo apt-get install -y git amazon-efs-utils vim-nox neofetch htop tmux curl wget
# Create new user with sudo privileges
sudo useradd -m -s /bin/bash greg
sudo usermod -aG sudo greg
# Add authorized keys for your public key
sudo mkdir -p /home/greg/.ssh
sudo touch /home/greg/.ssh/authorized_keys
sudo chmod 700 /home/greg/.ssh
sudo chmod 600 /home/greg/.ssh/authorized_keys
sudo chown -R greg:greg /home/greg/.ssh
# Add your public key to authorized_keys
sudo echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCdOF80z0piQEnYzNCu2OGvOJdm7+3wfDuiC+Jzi8VbSC5VW4iJAQXOuDNGLzyqNi6uMjI77xpEL6Xzn29uJiQti6Y/LxhOZwNNIQiGUpFco1wkBYeBFbtgHQxsMLwumrxQGEj2fyCiSrACAPyy/l1fP4mlN7abBGD5aozBrYKxXPS/kfwO5nsWmw27RgTzfHJzie2dUU3ew/kd7td3wEdWrRXq8wNbu+yvAyiog54huUUWmYZwY3QVwXr6R1wsVudawM6BEl45QFq+hdB4t83azHG94XLy2NCAncohdU7zP40nsbvIDyh+4wIKeU90z6TLrXfHUYuBT6/ky7qOFm/Ym1QG4zCDz3jin8Qoa31PGaObzj/zoMJXgOXKcp16W0j9SZAenvnSfuWUEfBR1yBRR0T5Wg5v1vi7KGBTATaz8el802uliL+yZbGtMbNpAPGR5nK5C4yorf8yVYvIgo/LJaWCDND2O1e2mdut1WyRmvIwMnq7PFZT8zAsgGXfhDM= greg@ligma
" >> /home/greg/.ssh/authorized_keys
# Set hostname
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
AVAILABILITY_ZONE=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
HOSTNAME="$INSTANCE_ID-$AVAILABILITY_ZONE"
sudo hostnamectl set-hostname $HOSTNAME
# Run cloud-init.sh script
# sudo sh /path/to/cloud-init.sh

View File

@@ -0,0 +1,34 @@
# Define variables
variable "min_size" {
type = number
description = "Minimum number of instances in the Auto Scaling Group"
default = 1
}
variable "max_size" {
type = number
description = "Maximum number of instances in the Auto Scaling Group"
default = 1
}
variable "us_west_subnet_1_id" {
description = "The ID of the first US West subnet"
type = string
}
variable "us_west_subnet_2_id" {
description = "The ID of the second US West subnet"
type = string
}
variable "us_west_subnet_3_id" {
description = "The ID of the third US West subnet"
type = string
}
variable "vpc_id_west_2" {
description = "The ID of the VPC"
type = string
}

View File

@@ -1,107 +0,0 @@
# Define the VPC and subnets data sources
data "aws_vpc" "vpc" {
id = var.vpc_id
}
data "aws_subnet" "subnet1" {
id = var.us_east_subnet_1_id
}
data "aws_subnet" "subnet2" {
id = var.us_east_subnet_2_id
}
data "aws_subnet" "subnet3" {
id = var.us_east_subnet_3_id
}
data "aws_subnet" "subnet4" {
id = var.us_west_subnet_1_id
}
data "aws_subnet" "subnet5" {
id = var.us_west_subnet_2_id
}
data "aws_subnet" "subnet6" {
id = var.us_west_subnet_3_id
}
# Create a security group for the EC2 instance
resource "aws_security_group" "instance" {
name_prefix = "instance-"
vpc_id = var.vpc_id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "instance-security-group"
}
}
# Define the Launch Configuration
resource "aws_launch_configuration" "lc" {
name_prefix = "lc-"
image_id = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI
instance_type = "t3.small"
security_groups = [
aws_security_group.instance.id
]
user_data = file("${path.module}/user-data.sh")
root_block_device {
volume_size = 20
}
}
# Define the Auto Scaling Group
resource "aws_autoscaling_group" "asg" {
name_prefix = "asg-"
launch_configuration = aws_launch_configuration.lc.id
depends_on = [
var.vpc_id,
aws_launch_configuration.lc,
data.aws_subnet.subnet1,
data.aws_subnet.subnet2,
data.aws_subnet.subnet3,
data.aws_subnet.subnet4,
data.aws_subnet.subnet5,
data.aws_subnet.subnet6
]
vpc_zone_identifier = [
data.aws_subnet.subnet1.id,
data.aws_subnet.subnet2.id,
data.aws_subnet.subnet3.id,
data.aws_subnet.subnet4.id,
data.aws_subnet.subnet5.id,
data.aws_subnet.subnet6.id
]
min_size = var.min_size
max_size = var.max_size
desired_capacity = var.min_size
health_check_grace_period = 300
health_check_type = "EC2"
termination_policies = ["OldestInstance"]
tag {
key = "Name"
value = "asg-instance"
propagate_at_launch = true
}
}
data "aws_instances" "asg_instances" {
instance_tags = {
"aws:autoscaling:groupName" = aws_autoscaling_group.asg.name
}
}

View File

@@ -1,3 +0,0 @@
output "vpc_id" {
value = aws_vpc.vpc_us.id
}

View File

@@ -13,4 +13,4 @@ output "us_east_subnet_2_id" {
output "us_east_subnet_3_id" {
description = "The ID of the third US East subnet"
value = aws_subnet.us_east_subnet_3.id
}
}

View File

@@ -1,36 +1,30 @@
provider "aws" {
region = var.region
region = "us-east-1"
}
resource "aws_subnet" "us_east_subnet_1" {
vpc_id = var.vpc_id
vpc_id = var.vpc_id_east_1
cidr_block = var.us_east_subnet_1_cidr_block
availability_zone = var.us_east_subnet_1_az
tags = {
Name = "${var.region}_${var.us_east_subnet_1_az}_subnet"
}
depends_on = [var.vpc_id]
}
resource "aws_subnet" "us_east_subnet_2" {
vpc_id = var.vpc_id
vpc_id = var.vpc_id_east_1
cidr_block = var.us_east_subnet_2_cidr_block
availability_zone = var.us_east_subnet_2_az
tags = {
Name = "${var.region}_${var.us_east_subnet_2_az}_subnet"
}
depends_on = [var.vpc_id]
}
resource "aws_subnet" "us_east_subnet_3" {
vpc_id = var.vpc_id
vpc_id = var.vpc_id_east_1
cidr_block = var.us_east_subnet_3_cidr_block
availability_zone = var.us_east_subnet_3_az
tags = {
Name = "${var.region}_${var.us_east_subnet_3_az}_subnet"
}
depends_on = [var.vpc_id]
}

View File

@@ -16,18 +16,19 @@ variable "region" {
variable "us_east_subnet_1_cidr_block" {
default = "10.0.4.0/24"
default = "10.1.4.0/24"
}
variable "us_east_subnet_2_cidr_block" {
default = "10.0.5.0/24"
default = "10.1.5.0/24"
}
variable "us_east_subnet_3_cidr_block" {
default = "10.0.6.0/24"
default = "10.1.6.0/24"
}
variable "vpc_id" {
variable "vpc_id_east_1" {
description = "The ID of the VPC"
type = string
}
}

View File

@@ -1,26 +1,33 @@
provider "aws" {
region = "us-west-2"
}
resource "aws_subnet" "us_west_subnet_1" {
vpc_id = var.vpc_id
vpc_id = var.vpc_id_west_2
cidr_block = var.us_west_subnet_1_cidr_block
availability_zone = var.us_west_subnet_1_az
tags = {
Name = "${var.region}_${var.us_west_subnet_1_az}_subnet"
}
depends_on = [var.vpc_id_west_2]
}
resource "aws_subnet" "us_west_subnet_2" {
vpc_id = var.vpc_id
vpc_id = var.vpc_id_west_2
cidr_block = var.us_west_subnet_2_cidr_block
availability_zone = var.us_west_subnet_2_az
tags = {
Name = "${var.region}_${var.us_west_subnet_2_az}_subnet"
}
depends_on = [var.vpc_id_west_2]
}
resource "aws_subnet" "us_west_subnet_3" {
vpc_id = var.vpc_id
vpc_id = var.vpc_id_west_2
cidr_block = var.us_west_subnet_3_cidr_block
availability_zone = var.us_west_subnet_3_az
tags = {
Name = "${var.region}_${var.us_west_subnet_3_az}_subnet"
}
depends_on = [var.vpc_id_west_2]
}

View File

@@ -27,7 +27,8 @@ variable "region" {
default = "us-west-2"
}
variable "vpc_id" {
variable "vpc_id_west_2" {
description = "The ID of the VPC"
type = string
}

View File

@@ -1,8 +0,0 @@
vpc_cidr_block = "10.0.0.0/16"
us_east_subnet_1_id = ""
us_east_subnet_2_id = ""
us_east_subnet_3_id = ""
us_west_subnet_1_id = ""
us_west_subnet_2_id = ""
us_west_subnet_3_id = ""

View File

@@ -0,0 +1,8 @@
output "vpc_id_east_1" {
value = aws_vpc.vpc_us_east_1.id
}
output "vpc_cidr_block" {
description = "The CIDR block of the VPC"
value = aws_vpc.vpc_us_east_1.cidr_block
}

View File

@@ -0,0 +1,2 @@
vpc_cidr_block = "10.1.0.0/16"
region = "us-east-1"

View File

@@ -0,0 +1,23 @@
variable "vpc_cidr_block" {
default = "10.1.0.0/16"
}
variable "us_east_subnet_1_id" {
description = "The ID of the first US East subnet"
type = string
}
variable "us_east_subnet_2_id" {
description = "The ID of the second US East subnet"
type = string
}
variable "us_east_subnet_3_id" {
description = "The ID of the third US East subnet"
type = string
}
variable "region" {
description = "The region to deploy to"
type = string
}

View File

@@ -0,0 +1,42 @@
provider "aws" {
region = "us-east-1"
}
resource "aws_vpc" "vpc_us_east_1" {
cidr_block = var.vpc_cidr_block
tags = {
Name = "vpc_us_east_1"
Environment = "production"
}
}
# Create aws internet gateway
resource "aws_internet_gateway" "my_igw_east_1" {
vpc_id = aws_vpc.vpc_us_east_1.id
}
# Create route table entries for the east subnets
resource "aws_route_table" "us_east_route_table" {
vpc_id = aws_vpc.vpc_us_east_1.id
}
resource "aws_route" "us_east_route" {
route_table_id = aws_route_table.us_east_route_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_igw_east_1.id
}
resource "aws_route_table_association" "us_east_subnet_1_association" {
subnet_id = var.us_east_subnet_1_id
route_table_id = aws_route_table.us_east_route_table.id
}
resource "aws_route_table_association" "us_east_subnet_2_association" {
subnet_id = var.us_east_subnet_2_id
route_table_id = aws_route_table.us_east_route_table.id
}
resource "aws_route_table_association" "us_east_subnet_3_association" {
subnet_id = var.us_east_subnet_3_id
route_table_id = aws_route_table.us_east_route_table.id
}

View File

@@ -0,0 +1,8 @@
output "vpc_id_west_2" {
value = aws_vpc.vpc_us_west_2.id
}
output "vpc_cidr_block" {
description = "The CIDR block of the VPC"
value = aws_vpc.vpc_us_west_2.cidr_block
}

View File

@@ -0,0 +1,2 @@
vpc_cidr_block = "10.0.0.0/16"
region = "us-west-2"

View File

@@ -2,21 +2,6 @@ variable "vpc_cidr_block" {
default = "10.0.0.0/16"
}
variable "us_east_subnet_1_id" {
description = "The ID of the first US East subnet"
type = string
}
variable "us_east_subnet_2_id" {
description = "The ID of the second US East subnet"
type = string
}
variable "us_east_subnet_3_id" {
description = "The ID of the third US East subnet"
type = string
}
variable "us_west_subnet_1_id" {
description = "The ID of the first US West subnet"
type = string
@@ -30,4 +15,9 @@ variable "us_west_subnet_2_id" {
variable "us_west_subnet_3_id" {
description = "The ID of the third US West subnet"
type = string
}
variable "region" {
description = "The region to deploy to"
type = string
}

View File

@@ -0,0 +1,46 @@
provider "aws" {
region = var.region
}
#Create aws vpc
resource "aws_vpc" "vpc_us_west_2" {
cidr_block = var.vpc_cidr_block
tags = {
Name = "vpc_us_west_1"
Environment = "production"
}
}
# Create aws internet gateway
resource "aws_internet_gateway" "my_igw_west_2" {
vpc_id = aws_vpc.vpc_us_west_2.id
}
# Create route table entries for the west subnets
resource "aws_route_table" "us_west_route_table" {
vpc_id = aws_vpc.vpc_us_west_2.id
}
resource "aws_route" "us_west_route" {
route_table_id = aws_route_table.us_west_route_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_igw_west_2.id
}
resource "aws_route_table_association" "us_west_subnet_1_association" {
subnet_id = var.us_west_subnet_1_id
route_table_id = aws_route_table.us_west_route_table.id
}
resource "aws_route_table_association" "us_west_subnet_2_association" {
subnet_id = var.us_west_subnet_2_id
route_table_id = aws_route_table.us_west_route_table.id
}
resource "aws_route_table_association" "us_west_subnet_3_association" {
subnet_id = var.us_west_subnet_3_id
route_table_id = aws_route_table.us_west_route_table.id
}

View File

@@ -1,65 +0,0 @@
#Create aws vpc
resource "aws_vpc" "vpc_us" {
cidr_block = var.vpc_cidr_block
tags = {
Name = "vpc_us"
Environment = "production"
}
}
# Create aws internet gateway
resource "aws_internet_gateway" "my_igw" {
vpc_id = aws_vpc.vpc_us.id
}
# Create route table entries for the west subnets
resource "aws_route_table" "us_west_route_table" {
vpc_id = aws_vpc.vpc_us.id
}
# Create route table entries for the east subnets
resource "aws_route_table" "us_east_route_table" {
vpc_id = aws_vpc.vpc_us.id
}
resource "aws_route" "us_west_route" {
route_table_id = aws_route_table.us_west_route_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_igw.id
}
resource "aws_route" "us_east_route" {
route_table_id = aws_route_table.us_east_route_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_igw.id
}
resource "aws_route_table_association" "us_west_subnet_1_association" {
subnet_id = var.us_west_subnet_1_id
route_table_id = aws_route_table.us_west_route_table.id
}
resource "aws_route_table_association" "us_west_subnet_2_association" {
subnet_id = var.us_west_subnet_2_id
route_table_id = aws_route_table.us_west_route_table.id
}
resource "aws_route_table_association" "us_west_subnet_3_association" {
subnet_id = var.us_west_subnet_3_id
route_table_id = aws_route_table.us_west_route_table.id
}
resource "aws_route_table_association" "us_east_subnet_1_association" {
subnet_id = var.us_east_subnet_1_id
route_table_id = aws_route_table.us_east_route_table.id
}
resource "aws_route_table_association" "us_east_subnet_2_association" {
subnet_id = var.us_east_subnet_2_id
route_table_id = aws_route_table.us_east_route_table.id
}
resource "aws_route_table_association" "us_east_subnet_3_association" {
subnet_id = var.us_east_subnet_3_id
route_table_id = aws_route_table.us_east_route_table.id
}