mirror of
https://github.com/ghndrx/terraform.git
synced 2026-02-10 14:55:06 +00:00
Add AWS ASG module and update VPC subnets
This commit is contained in:
88
aws/aws_asg/modules/ec2/ec2-east/ec2-east.tf
Normal file
88
aws/aws_asg/modules/ec2/ec2-east/ec2-east.tf
Normal 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
|
||||
}
|
||||
}
|
||||
0
aws/aws_asg/modules/ec2/ec2-east/outputs.tf
Normal file
0
aws/aws_asg/modules/ec2/ec2-east/outputs.tf
Normal file
2
aws/aws_asg/modules/ec2/ec2-east/terraform.tfvars
Normal file
2
aws/aws_asg/modules/ec2/ec2-east/terraform.tfvars
Normal file
@@ -0,0 +1,2 @@
|
||||
min_size = "3"
|
||||
max_size = "10"
|
||||
28
aws/aws_asg/modules/ec2/ec2-east/user-data.sh
Normal file
28
aws/aws_asg/modules/ec2/ec2-east/user-data.sh
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/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 "" >> /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
|
||||
35
aws/aws_asg/modules/ec2/ec2-east/variables.tf
Normal file
35
aws/aws_asg/modules/ec2/ec2-east/variables.tf
Normal 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
|
||||
}
|
||||
|
||||
|
||||
|
||||
90
aws/aws_asg/modules/ec2/ec2-west/ec2-west..tf
Normal file
90
aws/aws_asg/modules/ec2/ec2-west/ec2-west..tf
Normal 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
|
||||
}
|
||||
}
|
||||
4
aws/aws_asg/modules/ec2/ec2-west/outputs.tf
Normal file
4
aws/aws_asg/modules/ec2/ec2-west/outputs.tf
Normal 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]
|
||||
# }
|
||||
2
aws/aws_asg/modules/ec2/ec2-west/terraform.tfvars
Normal file
2
aws/aws_asg/modules/ec2/ec2-west/terraform.tfvars
Normal file
@@ -0,0 +1,2 @@
|
||||
min_size = "3"
|
||||
max_size = "10"
|
||||
29
aws/aws_asg/modules/ec2/ec2-west/user-data.sh
Normal file
29
aws/aws_asg/modules/ec2/ec2-west/user-data.sh
Normal 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
|
||||
32
aws/aws_asg/modules/ec2/ec2-west/variables.tf
Normal file
32
aws/aws_asg/modules/ec2/ec2-west/variables.tf
Normal 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user