From 48ed0ca1d618a1dbaf5910def40f2388f3eaa17a Mon Sep 17 00:00:00 2001 From: Greg Hendrickson Date: Fri, 17 Nov 2023 09:46:38 -0800 Subject: [PATCH] seperate east/west --- aws/aws_simple/main.tf | 28 +++-- aws/aws_simple/modules/ec2/ec2-east/ec2.tf | 71 ++++++++++++ .../modules/ec2/{ => ec2-east}/outputs.tf | 0 .../ec2/{ => ec2-east}/terraform.tfvars | 0 .../modules/ec2/{ => ec2-east}/user-data.sh | 0 .../modules/ec2/{ => ec2-east}/variables.tf | 22 +--- aws/aws_simple/modules/ec2/ec2-west/ec2.tf | 72 ++++++++++++ .../modules/ec2/ec2-west/outputs.tf | 4 + .../modules/ec2/ec2-west/terraform.tfvars | 2 + .../modules/ec2/ec2-west/user-data.sh | 29 +++++ .../modules/ec2/ec2-west/variables.tf | 34 ++++++ aws/aws_simple/modules/ec2/ec2.tf | 107 ------------------ aws/aws_simple/modules/vpc/outputs.tf | 3 - .../modules/vpc/subnets/us-east/outputs.tf | 2 +- .../vpc/subnets/us-east/subnets-east.tf | 14 +-- .../modules/vpc/subnets/us-east/variables.tf | 11 +- .../vpc/subnets/us-west/subnets-west.tf | 13 ++- .../modules/vpc/subnets/us-west/variables.tf | 3 +- aws/aws_simple/modules/vpc/terraform.tfvars | 8 -- .../modules/vpc/vpc-east/outputs.tf | 8 ++ .../modules/vpc/vpc-east/terraform.tfvars | 2 + .../modules/vpc/vpc-east/variables.tf | 23 ++++ .../modules/vpc/vpc-east/vpc-east.tf | 42 +++++++ .../modules/vpc/vpc-west/outputs.tf | 8 ++ .../modules/vpc/vpc-west/terraform.tfvars | 2 + .../modules/vpc/{ => vpc-west}/variables.tf | 20 +--- .../modules/vpc/vpc-west/vpc-west.tf | 46 ++++++++ aws/aws_simple/modules/vpc/vpc.tf | 65 ----------- 28 files changed, 396 insertions(+), 243 deletions(-) create mode 100644 aws/aws_simple/modules/ec2/ec2-east/ec2.tf rename aws/aws_simple/modules/ec2/{ => ec2-east}/outputs.tf (100%) rename aws/aws_simple/modules/ec2/{ => ec2-east}/terraform.tfvars (100%) rename aws/aws_simple/modules/ec2/{ => ec2-east}/user-data.sh (100%) rename aws/aws_simple/modules/ec2/{ => ec2-east}/variables.tf (66%) create mode 100644 aws/aws_simple/modules/ec2/ec2-west/ec2.tf create mode 100644 aws/aws_simple/modules/ec2/ec2-west/outputs.tf create mode 100644 aws/aws_simple/modules/ec2/ec2-west/terraform.tfvars create mode 100644 aws/aws_simple/modules/ec2/ec2-west/user-data.sh create mode 100644 aws/aws_simple/modules/ec2/ec2-west/variables.tf delete mode 100644 aws/aws_simple/modules/ec2/ec2.tf delete mode 100644 aws/aws_simple/modules/vpc/outputs.tf delete mode 100644 aws/aws_simple/modules/vpc/terraform.tfvars create mode 100644 aws/aws_simple/modules/vpc/vpc-east/outputs.tf create mode 100644 aws/aws_simple/modules/vpc/vpc-east/terraform.tfvars create mode 100644 aws/aws_simple/modules/vpc/vpc-east/variables.tf create mode 100644 aws/aws_simple/modules/vpc/vpc-east/vpc-east.tf create mode 100644 aws/aws_simple/modules/vpc/vpc-west/outputs.tf create mode 100644 aws/aws_simple/modules/vpc/vpc-west/terraform.tfvars rename aws/aws_simple/modules/vpc/{ => vpc-west}/variables.tf (57%) create mode 100644 aws/aws_simple/modules/vpc/vpc-west/vpc-west.tf delete mode 100644 aws/aws_simple/modules/vpc/vpc.tf diff --git a/aws/aws_simple/main.tf b/aws/aws_simple/main.tf index ecd4bc0..99a90d7 100644 --- a/aws/aws_simple/main.tf +++ b/aws/aws_simple/main.tf @@ -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" } diff --git a/aws/aws_simple/modules/ec2/ec2-east/ec2.tf b/aws/aws_simple/modules/ec2/ec2-east/ec2.tf new file mode 100644 index 0000000..cdbaf94 --- /dev/null +++ b/aws/aws_simple/modules/ec2/ec2-east/ec2.tf @@ -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 + } +} diff --git a/aws/aws_simple/modules/ec2/outputs.tf b/aws/aws_simple/modules/ec2/ec2-east/outputs.tf similarity index 100% rename from aws/aws_simple/modules/ec2/outputs.tf rename to aws/aws_simple/modules/ec2/ec2-east/outputs.tf diff --git a/aws/aws_simple/modules/ec2/terraform.tfvars b/aws/aws_simple/modules/ec2/ec2-east/terraform.tfvars similarity index 100% rename from aws/aws_simple/modules/ec2/terraform.tfvars rename to aws/aws_simple/modules/ec2/ec2-east/terraform.tfvars diff --git a/aws/aws_simple/modules/ec2/user-data.sh b/aws/aws_simple/modules/ec2/ec2-east/user-data.sh similarity index 100% rename from aws/aws_simple/modules/ec2/user-data.sh rename to aws/aws_simple/modules/ec2/ec2-east/user-data.sh diff --git a/aws/aws_simple/modules/ec2/variables.tf b/aws/aws_simple/modules/ec2/ec2-east/variables.tf similarity index 66% rename from aws/aws_simple/modules/ec2/variables.tf rename to aws/aws_simple/modules/ec2/ec2-east/variables.tf index 6092172..068b688 100644 --- a/aws/aws_simple/modules/ec2/variables.tf +++ b/aws/aws_simple/modules/ec2/ec2-east/variables.tf @@ -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 -} \ No newline at end of file +} + + + diff --git a/aws/aws_simple/modules/ec2/ec2-west/ec2.tf b/aws/aws_simple/modules/ec2/ec2-west/ec2.tf new file mode 100644 index 0000000..69277bf --- /dev/null +++ b/aws/aws_simple/modules/ec2/ec2-west/ec2.tf @@ -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 + } +} diff --git a/aws/aws_simple/modules/ec2/ec2-west/outputs.tf b/aws/aws_simple/modules/ec2/ec2-west/outputs.tf new file mode 100644 index 0000000..d5f2320 --- /dev/null +++ b/aws/aws_simple/modules/ec2/ec2-west/outputs.tf @@ -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] +# } \ No newline at end of file diff --git a/aws/aws_simple/modules/ec2/ec2-west/terraform.tfvars b/aws/aws_simple/modules/ec2/ec2-west/terraform.tfvars new file mode 100644 index 0000000..e591122 --- /dev/null +++ b/aws/aws_simple/modules/ec2/ec2-west/terraform.tfvars @@ -0,0 +1,2 @@ +min_size = 1 +max_size = 10 \ No newline at end of file diff --git a/aws/aws_simple/modules/ec2/ec2-west/user-data.sh b/aws/aws_simple/modules/ec2/ec2-west/user-data.sh new file mode 100644 index 0000000..bb81c28 --- /dev/null +++ b/aws/aws_simple/modules/ec2/ec2-west/user-data.sh @@ -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 diff --git a/aws/aws_simple/modules/ec2/ec2-west/variables.tf b/aws/aws_simple/modules/ec2/ec2-west/variables.tf new file mode 100644 index 0000000..13e92ed --- /dev/null +++ b/aws/aws_simple/modules/ec2/ec2-west/variables.tf @@ -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 +} + diff --git a/aws/aws_simple/modules/ec2/ec2.tf b/aws/aws_simple/modules/ec2/ec2.tf deleted file mode 100644 index 96743ff..0000000 --- a/aws/aws_simple/modules/ec2/ec2.tf +++ /dev/null @@ -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 - } -} diff --git a/aws/aws_simple/modules/vpc/outputs.tf b/aws/aws_simple/modules/vpc/outputs.tf deleted file mode 100644 index 87ef5aa..0000000 --- a/aws/aws_simple/modules/vpc/outputs.tf +++ /dev/null @@ -1,3 +0,0 @@ -output "vpc_id" { - value = aws_vpc.vpc_us.id -} diff --git a/aws/aws_simple/modules/vpc/subnets/us-east/outputs.tf b/aws/aws_simple/modules/vpc/subnets/us-east/outputs.tf index 71f3ecd..e73df5c 100644 --- a/aws/aws_simple/modules/vpc/subnets/us-east/outputs.tf +++ b/aws/aws_simple/modules/vpc/subnets/us-east/outputs.tf @@ -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 -} \ No newline at end of file +} diff --git a/aws/aws_simple/modules/vpc/subnets/us-east/subnets-east.tf b/aws/aws_simple/modules/vpc/subnets/us-east/subnets-east.tf index dea401b..f846e52 100644 --- a/aws/aws_simple/modules/vpc/subnets/us-east/subnets-east.tf +++ b/aws/aws_simple/modules/vpc/subnets/us-east/subnets-east.tf @@ -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] } \ No newline at end of file diff --git a/aws/aws_simple/modules/vpc/subnets/us-east/variables.tf b/aws/aws_simple/modules/vpc/subnets/us-east/variables.tf index 06b49e1..e8b4108 100644 --- a/aws/aws_simple/modules/vpc/subnets/us-east/variables.tf +++ b/aws/aws_simple/modules/vpc/subnets/us-east/variables.tf @@ -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 -} \ No newline at end of file +} + diff --git a/aws/aws_simple/modules/vpc/subnets/us-west/subnets-west.tf b/aws/aws_simple/modules/vpc/subnets/us-west/subnets-west.tf index fc69355..2f3f099 100644 --- a/aws/aws_simple/modules/vpc/subnets/us-west/subnets-west.tf +++ b/aws/aws_simple/modules/vpc/subnets/us-west/subnets-west.tf @@ -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] } diff --git a/aws/aws_simple/modules/vpc/subnets/us-west/variables.tf b/aws/aws_simple/modules/vpc/subnets/us-west/variables.tf index 2bc6502..0237ab3 100644 --- a/aws/aws_simple/modules/vpc/subnets/us-west/variables.tf +++ b/aws/aws_simple/modules/vpc/subnets/us-west/variables.tf @@ -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 } \ No newline at end of file diff --git a/aws/aws_simple/modules/vpc/terraform.tfvars b/aws/aws_simple/modules/vpc/terraform.tfvars deleted file mode 100644 index 009494e..0000000 --- a/aws/aws_simple/modules/vpc/terraform.tfvars +++ /dev/null @@ -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 = "" \ No newline at end of file diff --git a/aws/aws_simple/modules/vpc/vpc-east/outputs.tf b/aws/aws_simple/modules/vpc/vpc-east/outputs.tf new file mode 100644 index 0000000..8695c74 --- /dev/null +++ b/aws/aws_simple/modules/vpc/vpc-east/outputs.tf @@ -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 +} \ No newline at end of file diff --git a/aws/aws_simple/modules/vpc/vpc-east/terraform.tfvars b/aws/aws_simple/modules/vpc/vpc-east/terraform.tfvars new file mode 100644 index 0000000..a4b92a4 --- /dev/null +++ b/aws/aws_simple/modules/vpc/vpc-east/terraform.tfvars @@ -0,0 +1,2 @@ +vpc_cidr_block = "10.1.0.0/16" +region = "us-east-1" \ No newline at end of file diff --git a/aws/aws_simple/modules/vpc/vpc-east/variables.tf b/aws/aws_simple/modules/vpc/vpc-east/variables.tf new file mode 100644 index 0000000..a112697 --- /dev/null +++ b/aws/aws_simple/modules/vpc/vpc-east/variables.tf @@ -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 +} \ No newline at end of file diff --git a/aws/aws_simple/modules/vpc/vpc-east/vpc-east.tf b/aws/aws_simple/modules/vpc/vpc-east/vpc-east.tf new file mode 100644 index 0000000..2cdeb8b --- /dev/null +++ b/aws/aws_simple/modules/vpc/vpc-east/vpc-east.tf @@ -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 +} diff --git a/aws/aws_simple/modules/vpc/vpc-west/outputs.tf b/aws/aws_simple/modules/vpc/vpc-west/outputs.tf new file mode 100644 index 0000000..62963e1 --- /dev/null +++ b/aws/aws_simple/modules/vpc/vpc-west/outputs.tf @@ -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 +} \ No newline at end of file diff --git a/aws/aws_simple/modules/vpc/vpc-west/terraform.tfvars b/aws/aws_simple/modules/vpc/vpc-west/terraform.tfvars new file mode 100644 index 0000000..517c863 --- /dev/null +++ b/aws/aws_simple/modules/vpc/vpc-west/terraform.tfvars @@ -0,0 +1,2 @@ +vpc_cidr_block = "10.0.0.0/16" +region = "us-west-2" \ No newline at end of file diff --git a/aws/aws_simple/modules/vpc/variables.tf b/aws/aws_simple/modules/vpc/vpc-west/variables.tf similarity index 57% rename from aws/aws_simple/modules/vpc/variables.tf rename to aws/aws_simple/modules/vpc/vpc-west/variables.tf index 248726f..c901f87 100644 --- a/aws/aws_simple/modules/vpc/variables.tf +++ b/aws/aws_simple/modules/vpc/vpc-west/variables.tf @@ -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 } \ No newline at end of file diff --git a/aws/aws_simple/modules/vpc/vpc-west/vpc-west.tf b/aws/aws_simple/modules/vpc/vpc-west/vpc-west.tf new file mode 100644 index 0000000..eed9b31 --- /dev/null +++ b/aws/aws_simple/modules/vpc/vpc-west/vpc-west.tf @@ -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 +} + + + diff --git a/aws/aws_simple/modules/vpc/vpc.tf b/aws/aws_simple/modules/vpc/vpc.tf deleted file mode 100644 index b81b126..0000000 --- a/aws/aws_simple/modules/vpc/vpc.tf +++ /dev/null @@ -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 -} -