cleanup folders and files

This commit is contained in:
Greg Hendrickson
2023-11-17 17:08:59 -08:00
parent a27a3737fc
commit 8a23203128
35 changed files with 117 additions and 1225 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

@@ -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,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

@@ -0,0 +1,33 @@
provider "aws" {
region = "us-east-1"
}
resource "aws_subnet" "us_east_subnet_1" {
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 = 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 = 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

@@ -0,0 +1,10 @@
us_east_subnet_1_az = "us-east-1a"
us_east_subnet_1_cidr_block = "10.0.4.0/24"
us_east_subnet_2_az = "us-east-1b"
us_east_subnet_2_cidr_block = "10.0.5.0/24"
us_east_subnet_3_az = "us-east-1c"
us_east_subnet_3_cidr_block = "10.0.6.0/24"

View File

@@ -0,0 +1,34 @@
variable "us_east_subnet_1_az" {
default = "us-east-1a"
}
variable "us_east_subnet_2_az" {
default = "us-east-1b"
}
variable "us_east_subnet_3_az" {
default = "us-east-1c"
}
variable "region" {
default = "us-east-1"
}
variable "us_east_subnet_1_cidr_block" {
default = "10.1.4.0/24"
}
variable "us_east_subnet_2_cidr_block" {
default = "10.1.5.0/24"
}
variable "us_east_subnet_3_cidr_block" {
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

@@ -0,0 +1,36 @@
provider "aws" {
region = "us-west-2"
}
resource "aws_subnet" "us_west_subnet_1" {
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 = 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 = 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

@@ -0,0 +1,9 @@
region = "us-west-2"
us_west_subnet_1_cidr_block = "10.0.1.0/24"
us_west_subnet_1_az = "us-west-2a"
us_west_subnet_2_cidr_block = "10.0.2.0/24"
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

@@ -0,0 +1,34 @@
variable "us_west_subnet_1_cidr_block" {
default = "10.0.1.0/24"
}
variable "us_west_subnet_2_cidr_block" {
default = "10.0.2.0/24"
}
variable "us_west_subnet_3_cidr_block" {
default = "10.0.3.0/24"
}
variable "us_west_subnet_1_az" {
default = "us-west-2a"
}
variable "us_west_subnet_2_az" {
default = "us-west-2b"
}
variable "us_west_subnet_3_az" {
default = "us-west-2c"
}
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

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

15
aws/aws_asg/readme.md Normal file
View File

@@ -0,0 +1,15 @@
my-terraform-project/
├── main.tf
└── modules/
├── EFS/
│ └── main.tf
├── ELB/
│ └── main.tf
├── EC2/
│ └── main.tf
├── S3/
│ └── main.tf
└── VPC/
└── main.tf

View File

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

16
aws/aws_asg/variables.tf Normal file
View File

@@ -0,0 +1,16 @@
# Define variables
variable "aws_region" {
type = string
default = "us-west-2"
}
variable "aws_access_key" {
type = string
default = ""
}
variable "aws_secret_key" {
type = string
default = ""
}