Add AWS ASG module and update VPC subnets

This commit is contained in:
gregory hendrickson
2023-11-19 06:29:02 -08:00
42 changed files with 972 additions and 237 deletions

66
aws/aws_asg/main.tf Normal file
View File

@@ -0,0 +1,66 @@
terraform {
backend "s3" {
bucket = "my-tf-bucket-ghndrx"
key = "terraform.tfstate"
region = "us-west-2"
}
}
# Define provider
provider "aws" {
region = var.aws_region
}
# Define modules
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
}
module "subnets_us_west" {
source = "./modules/vpc/subnets/us-west"
vpc_id_west_2 = module.vpc-west.vpc_id_west_2
}
module "subnets_us_east" {
source = "./modules/vpc/subnets/us-east"
vpc_id_east_1 = module.vpc-east.vpc_id_east_1
}
module "ec2-east" {
source = "./modules/ec2/ec2-east"
min_size = "4"
max_size = "10"
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"
min_size = "4"
max_size = "10"
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_west_2 = module.vpc-west.vpc_id_west_2
}

View File

@@ -0,0 +1,88 @@
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
# HTTP access
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# HTTPS access
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# SSH access
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "instance-security-group-east"
}
}
# Define the Launch Template
resource "aws_launch_template" "lt-east" {
name_prefix = "lt-east-"
image_id = "ami-0237a465e7f465b10"
instance_type = "t3.small"
user_data = base64encode(file("${path.module}/user-data.sh"))
block_device_mappings {
device_name = "/dev/xvda"
ebs {
volume_size = 300
}
}
network_interfaces {
associate_public_ip_address = true
security_groups = [aws_security_group.instance-east.id]
}
}
# Define the Auto Scaling Group
resource "aws_autoscaling_group" "asg-east" {
name_prefix = "asg-east-"
launch_template {
id = aws_launch_template.lt-east.id
version = "$Latest"
}
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
}
depends_on = [
aws_security_group.instance-east,
aws_launch_template.lt-east
]
}
data "aws_instances" "asg_instances-east" {
instance_tags = {
"aws:autoscaling:groupName" = aws_autoscaling_group.asg-east.name
}
}

View File

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

View File

@@ -25,4 +25,4 @@ HOSTNAME="$INSTANCE_ID-$AVAILABILITY_ZONE"
sudo hostnamectl set-hostname $HOSTNAME
# Run cloud-init.sh script
sudo sh /path/to/cloud-init.sh
# sudo sh /path/to/cloud-init.sh

View File

@@ -0,0 +1,35 @@
# Define variables
variable "min_size" {
type = number
description = "Minimum number of instances in the Auto Scaling Group"
}
variable "max_size" {
type = number
description = "Maximum number of instances in the Auto Scaling Group"
}
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 "vpc_id_east_1" {
description = "The ID of the VPC"
type = string
}

View File

@@ -0,0 +1,90 @@
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
# HTTP access
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# HTTPS access
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# SSH access
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "instance-security-group-west"
}
}
# Define the Launch Template
resource "aws_launch_template" "lt-west" {
name_prefix = "lt-west"
image_id = "ami-03bf1eb153d14803f"
instance_type = "t3.small"
user_data = base64encode(file("${path.module}/user-data.sh"))
block_device_mappings {
device_name = "/dev/xvda"
ebs {
volume_size = 300
}
}
network_interfaces {
associate_public_ip_address = true
security_groups = [aws_security_group.instance-west.id]
}
}
# Define the Auto Scaling Group
resource "aws_autoscaling_group" "asg-west" {
name_prefix = "asg-west-"
launch_template {
id = aws_launch_template.lt-west.id
version = "$Latest"
}
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
}
depends_on = [
aws_security_group.instance-west,
aws_launch_template.lt-west
]
}
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 = "3"
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,32 @@
# Define variables
variable "min_size" {
type = number
description = "Minimum number of instances in the Auto Scaling Group"
}
variable "max_size" {
type = number
description = "Maximum number of instances in the Auto Scaling Group"
}
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

@@ -0,0 +1,16 @@
# In your vpc/subnets/us-east module
output "us_east_subnet_1_id" {
description = "The ID of the first US East subnet"
value = aws_subnet.us_east_subnet_1.id
}
output "us_east_subnet_2_id" {
description = "The ID of the second US East subnet"
value = aws_subnet.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,30 +1,32 @@
provider "aws" {
region = "us-east-1"
}
resource "aws_subnet" "us_east_subnet_1" {
vpc_id = aws_vpc.my_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
map_public_ip_on_launch = true
tags = {
Name = "${var.region}_${var.us_east_subnet_1_az}_subnet"
}
}
resource "aws_subnet" "us_east_subnet_2" {
vpc_id = aws_vpc.my_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
map_public_ip_on_launch = true
tags = {
Name = "${var.region}_${var.us_east_subnet_2_az}_subnet"
}
}
resource "aws_subnet" "us_east_subnet_3" {
vpc_id = aws_vpc.my_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
map_public_ip_on_launch = true
tags = {
Name = "${var.region}_${var.us_east_subnet_3_az}_subnet"
}

View File

@@ -16,13 +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_east_1" {
description = "The ID of the VPC"
type = string
}

View File

@@ -0,0 +1,16 @@
# In your vpc/subnets/us-east module
output "us_west_subnet_1_id" {
description = "The ID of the first US East subnet"
value = aws_subnet.us_west_subnet_1.id
}
output "us_west_subnet_2_id" {
description = "The ID of the second US East subnet"
value = aws_subnet.us_west_subnet_2.id
}
output "us_west_subnet_3_id" {
description = "The ID of the third US East subnet"
value = aws_subnet.us_west_subnet_3.id
}

View File

@@ -1,27 +1,36 @@
provider "aws" {
region = "us-west-2"
}
resource "aws_subnet" "us_west_subnet_1" {
vpc_id = aws_vpc.my_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"
}
map_public_ip_on_launch = true
depends_on = [var.vpc_id_west_2]
}
resource "aws_subnet" "us_west_subnet_2" {
vpc_id = aws_vpc.my_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"
}
map_public_ip_on_launch = true
depends_on = [var.vpc_id_west_2]
}
resource "aws_subnet" "us_west_subnet_3" {
vpc_id = aws_vpc.my_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"
}
map_public_ip_on_launch = true
depends_on = [var.vpc_id_west_2]
}

View File

@@ -7,4 +7,3 @@ us_west_subnet_2_az = "us-west-2b"
us_west_subnet_3_cidr_block = "10.0.3.0/24"
us_west_subnet_3_az = "us-west-2c"

View File

@@ -26,3 +26,9 @@ variable "us_west_subnet_3_az" {
variable "region" {
default = "us-west-2"
}
variable "vpc_id_west_2" {
description = "The ID of the VPC"
type = string
}

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

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

View File

@@ -0,0 +1,23 @@
variable "vpc_cidr_block" {
default = "10.0.0.0/16"
}
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 "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

@@ -0,0 +1,3 @@
aws_secret_key = ""
aws_access_key = ""
aws_region = "us-west-2"

View File

@@ -1,96 +0,0 @@
# Define the VPC and subnets data sources
data "aws_vpc" "vpc" {
id = data.aws_subnet.subnet1.vpc_id
}
module "vpc_subnets" {
source = "../vpc/subnets"
}
data "aws_subnet" "subnet1" {
id = module.vpc_subnets.subnet_ids[0]
}
data "aws_subnet" "subnet2" {
id = module.vpc_subnets.subnet_ids[1]
}
data "aws_subnet" "subnet3" {
id = module.vpc_subnets.subnet_ids[2]
}
data "aws_subnet" "subnet4" {
id = module.vpc_subnets.subnet_ids[3]
}
data "aws_subnet" "subnet5" {
id = module.vpc_subnets.subnet_ids[4]
}
data "aws_subnet" "subnet6" {
id = module.vpc_subnets.subnet_ids[5]
}
# Create a security group for the EC2 instance
resource "aws_security_group" "instance" {
name_prefix = "instance-"
vpc_id = data.aws_vpc.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
vpc_zone_identifier = [
data.aws_subnet.subnet1.id,
data.aws_subnet.subnet2.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
}
}
# Output the instance public IP address
output "public_ip" {
value = aws_autoscaling_group.asg.instances[0].public_ip
}

View File

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

View File

@@ -1,13 +0,0 @@
# 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
}

View File

@@ -1,40 +0,0 @@
# Define provider
provider "aws" {
region = var.aws_region
access_key = var.aws_access_key
secret_key = var.aws_secret_key
}
# Define modules
module "vpc_us_west" {
source = "./modules/vpc"
}
module "subnet_us_west" {
source = "./modules/subnet"
vpc_id = module.vpc_us_west.vpc_id
region = "us-west-2"
}
module "vpc_us_east" {
source = "./modules/vpc"
region = "us-east-1"
}
module "subnet_us_east" {
source = "./modules/subnet"
vpc_id = module.vpc_us_east.vpc_id
region = "us-east-1"
}
module "ec2" {
source = "./modules/ec2"
}
module "elb" {
source = "./modules/elb"
}
module "efs" {
source = "./modules/efs"
}

View File

@@ -1,3 +0,0 @@
aws_secret_key = ""
aws_access_key = ""
aws_region = "us-west-2"

View File

@@ -1,4 +0,0 @@
variable "vpc_cidr_block" {
default = "10.0.0.0/16"
}

View File

@@ -1,65 +0,0 @@
#Create aws vpc
resource "aws_vpc" "my_vpc" {
cidr_block = var.vpc_cidr_block
tags = {
Name = "production-vpc"
Environment = "production"
}
}
# Create aws internet gateway
resource "aws_internet_gateway" "my_igw" {
vpc_id = aws_vpc.my_vpc.id
}
# Create route table entries for the west subnets
resource "aws_route_table" "us_west_route_table" {
vpc_id = aws_vpc.my_vpc.id
}
# Create route table entries for the east subnets
resource "aws_route_table" "us_east_route_table" {
vpc_id = aws_vpc.my_vpc.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 = aws_subnet.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 = aws_subnet.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 = aws_subnet.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 = aws_subnet.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 = aws_subnet.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 = aws_subnet.us_east_subnet_3.id
route_table_id = aws_route_table.us_east_route_table.id
}

337
aws/aws_vpc_peering/main.tf Normal file
View File

@@ -0,0 +1,337 @@
terraform {
backend "s3" {
bucket = "my-tf-bucket-ghndrx"
key = "aws_vpc_peering/terraform.tfstate"
region = "us-west-2"
}
}
# Define AWS provider with aliases for us-west-1 and us-east-1 regions
provider "aws" {
alias = "us-west-1"
region = "us-west-1"
}
provider "aws" {
alias = "us-east-1"
region = "us-east-1"
}
# Create VPCs in us-west-1 and us-east-1 regions
resource "aws_vpc" "us-west-1" {
cidr_block = "10.0.0.0/16"
provider = aws.us-west-1
}
resource "aws_vpc" "us-east-1" {
cidr_block = "10.1.0.0/16"
provider = aws.us-east-1
}
# Create subnets in us-west-1 VPC
resource "aws_subnet" "us-west-1-subnet-1" {
vpc_id = aws_vpc.us-west-1.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-1c"
provider = aws.us-west-1
}
resource "aws_subnet" "us-west-1-subnet-2" {
vpc_id = aws_vpc.us-west-1.id
cidr_block = "10.0.2.0/24"
availability_zone = "us-west-1b"
provider = aws.us-west-1
}
# Create subnets in us-east-1 VPC
resource "aws_subnet" "us-east-1-subnet-1" {
vpc_id = aws_vpc.us-east-1.id
cidr_block = "10.1.1.0/24"
availability_zone = "us-east-1c"
provider = aws.us-east-1
}
resource "aws_subnet" "us-east-1-subnet-2" {
vpc_id = aws_vpc.us-east-1.id
cidr_block = "10.1.2.0/24"
availability_zone = "us-east-1b"
provider = aws.us-east-1
}
# Create VPC peering connection between us-west-1 and us-east-1 VPCs
resource "aws_vpc_peering_connection" "peering_connection" {
vpc_id = aws_vpc.us-west-1.id
peer_vpc_id = aws_vpc.us-east-1.id
peer_region = "us-east-1"
auto_accept = false
provider = aws.us-west-1
}
# Create VPC peering connection accepter in us-east-1 region
resource "aws_vpc_peering_connection_accepter" "peering_accepter" {
vpc_peering_connection_id = aws_vpc_peering_connection.peering_connection.id
auto_accept = true
provider = aws.us-east-1
}
resource "aws_route" "peer-route-us-west-1" {
route_table_id = aws_route_table.us-west-1-route-table.id
destination_cidr_block = aws_vpc.us-east-1.cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.peering_connection.id
provider = aws.us-west-1
}
resource "aws_route" "peer-route-us-east-1" {
route_table_id = aws_route_table.us-east-1-route-table.id
destination_cidr_block = aws_vpc.us-west-1.cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.peering_connection.id
provider = aws.us-east-1
}
# Create route tables for each VPC and associate them with the respective subnets
resource "aws_route_table" "us-west-1-route-table" {
vpc_id = aws_vpc.us-west-1.id
provider = aws.us-west-1
}
resource "aws_route_table_association" "us-west-1-subnet-1-association" {
subnet_id = aws_subnet.us-west-1-subnet-1.id
route_table_id = aws_route_table.us-west-1-route-table.id
provider = aws.us-west-1
}
resource "aws_route_table_association" "us-west-1-subnet-2-association" {
subnet_id = aws_subnet.us-west-1-subnet-2.id
route_table_id = aws_route_table.us-west-1-route-table.id
provider = aws.us-west-1
}
resource "aws_route_table" "us-east-1-route-table" {
vpc_id = aws_vpc.us-east-1.id
provider = aws.us-east-1
}
resource "aws_route_table_association" "us-east-1-subnet-1-association" {
subnet_id = aws_subnet.us-east-1-subnet-1.id
route_table_id = aws_route_table.us-east-1-route-table.id
provider = aws.us-east-1
}
resource "aws_route_table_association" "us-east-1-subnet-2-association" {
subnet_id = aws_subnet.us-east-1-subnet-2.id
route_table_id = aws_route_table.us-east-1-route-table.id
provider = aws.us-east-1
}
# Create internet gateway for us-west-1 VPC
resource "aws_internet_gateway" "us-west-1-igw" {
vpc_id = aws_vpc.us-west-1.id
provider = aws.us-west-1
tags = {
Name = "us-west-1-igw"
}
}
# Create internet gateway for us-east-1 VPC
resource "aws_internet_gateway" "us-east-1-igw" {
vpc_id = aws_vpc.us-east-1.id
provider = aws.us-east-1
tags = {
Name = "us-east-1-igw"
}
}
# Attach internet gateway to the route table of each VPC
resource "aws_route" "us-west-1-igw-route" {
route_table_id = aws_route_table.us-west-1-route-table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.us-west-1-igw.id
provider = aws.us-west-1
}
resource "aws_route" "us-east-1-igw-route" {
route_table_id = aws_route_table.us-east-1-route-table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.us-east-1-igw.id
provider = aws.us-east-1
}
# Create security groups for EC2 instances
resource "aws_security_group" "us-west-1-instance-sg" {
vpc_id = aws_vpc.us-west-1.id
provider = aws.us-west-1
tags = {
Name = "instance-west-security-group"
}
# Allow inbound SSH, HTTP, and HTTPS traffic from any source
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
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"]
}
# Allow outbound traffic to all private subnets in the VPC
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["10.1.0.0/16", "0.0.0.0/0"]
}
ingress {
from_port = -1
to_port = -1
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "us-east-1-instance-sg" {
vpc_id = aws_vpc.us-east-1.id
provider = aws.us-east-1
tags = {
Name = "instance-east-security-group"
}
# Allow inbound SSH, HTTP, and HTTPS traffic from any source
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
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"]
}
# Allow outbound traffic to all private subnets in the VPC
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["10.1.0.0/16", "0.0.0.0/0"]
}
ingress {
from_port = -1
to_port = -1
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Create EC2 instances in each subnet
resource "aws_instance" "us-west-1-instance-1" {
ami = "ami-0f3f6663da6750955" # Ubuntu 20.04 AMI
instance_type = "t3.small" # Add instance type
subnet_id = aws_subnet.us-west-1-subnet-1.id
vpc_security_group_ids = [aws_security_group.us-west-1-instance-sg.id]
associate_public_ip_address = true # Set ephemeral public IP address
user_data = base64encode(file("${path.module}/user-data.sh"))
# Other necessary configurations for the instance
provider = aws.us-west-1
# ...
tags = {
Name = "us-west-1-instance-1"
SecurityGroup = "instance-west-security-group"
}
}
resource "aws_instance" "us-west-1-instance-2" {
ami = "ami-0f3f6663da6750955" # Ubuntu 20.04 AMI
instance_type = "t3.small" # Add instance type
subnet_id = aws_subnet.us-west-1-subnet-2.id
vpc_security_group_ids = [aws_security_group.us-west-1-instance-sg.id]
associate_public_ip_address = true # Set ephemeral public IP address
user_data = base64encode(file("${path.module}/user-data.sh"))
# Other necessary configurations for the instance
provider = aws.us-west-1
# ...
tags = {
Name = "us-west-1-instance-2"
SecurityGroup = "instance-west-security-group"
}
}
resource "aws_instance" "us-east-1-instance-1" {
ami = "ami-04e369782a6d2125e" # Ubuntu 20.04 AMI
instance_type = "t3.small" # Add instance type
subnet_id = aws_subnet.us-east-1-subnet-1.id
vpc_security_group_ids = [aws_security_group.us-east-1-instance-sg.id]
associate_public_ip_address = true # Set ephemeral public IP address
user_data = base64encode(file("${path.module}/user-data.sh"))
# Other necessary configurations for the instance
provider = aws.us-east-1
# ...
tags = {
Name = "us-east-1-instance-1"
SecurityGroup = "instance-east-security-group"
}
}
resource "aws_instance" "us-east-1-instance-2" {
ami = "ami-04e369782a6d2125e" # Ubuntu 20.04 AMI
instance_type = "t3.small" # Add instance type
subnet_id = aws_subnet.us-east-1-subnet-2.id
vpc_security_group_ids = [aws_security_group.us-east-1-instance-sg.id]
associate_public_ip_address = true # Set ephemeral public IP address
user_data = base64encode(file("${path.module}/user-data.sh"))
# Other necessary configurations for the instance
provider = aws.us-east-1
# ...
tags = {
Name = "us-east-1-instance-2"
SecurityGroup = "instance-east-security-group"
}
}
# ...
output "us-west-1-instance-1-public-ip" {
value = aws_instance.us-west-1-instance-1.public_ip
}
output "us-west-1-instance-2-public-ip" {
value = aws_instance.us-west-1-instance-2.public_ip
}
output "us-east-1-instance-1-public-ip" {
value = aws_instance.us-east-1-instance-1.public_ip
}
output "us-east-1-instance-2-public-ip" {
value = aws_instance.us-east-1-instance-2.public_ip
}

View File

@@ -0,0 +1,31 @@
#!/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 nginx
# 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
echo "hello world" >> /var/www/html/index.html
systemctm restart nginx
# Run cloud-init.sh script
# sudo sh /path/to/cloud-init.sh

24
aws/s3/main.tf Normal file
View File

@@ -0,0 +1,24 @@
# Provider configuration
provider "aws" {
region = "us-west-2"
# Add your AWS access and secret keys here
}
resource "aws_s3_bucket" "my-tf-bucket-ghndrx" {
bucket = "my-tf-bucket-ghndrx"
tags = {
Name = "tf-backend"
Environment = "production"
}
}
# Output the bucket name
output "my-tf-bucket-ghndrx" {
value = aws_s3_bucket.my-tf-bucket-ghndrx.bucket
}

View File

@@ -0,0 +1,9 @@
terraform {
backend "s3" {
bucket = "my-tf-bucket-ghndrx"
key = "terraform.tfstate"
region = "us-west-2"
}
}
# Rest of your Terraform configuration goes here...