launch configuration to launch template

This commit is contained in:
Greg Hendrickson
2023-11-17 11:06:04 -08:00
parent 48ed0ca1d6
commit 7a620b3a7a
8 changed files with 74 additions and 36 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ aws/aws_simple/.terraform/modules/modules.json
aws/aws_simple/terraform.tfstate.backup
aws/aws_simple/terraform.tfstate
aws/aws_simple/.terraform/providers/registry.terraform.io/hashicorp/aws/5.25.0/linux_amd64/terraform-provider-aws_v5.25.0_x5
aws/aws_simple/.terraform.tfstate.lock.info

View File

@@ -1,55 +1,67 @@
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 Configuration
resource "aws_launch_configuration" "lc-east" {
name_prefix = "lc-east-"
# Define the Launch Template
resource "aws_launch_template" "lt-east" {
name_prefix = "lt-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
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_configuration = aws_launch_configuration.lc-east.id
depends_on = [
aws_launch_configuration.lc-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,
var.us_east_subnet_3_id
]
min_size = var.min_size
max_size = var.max_size
@@ -62,6 +74,11 @@ resource "aws_autoscaling_group" "asg-east" {
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" {

View File

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

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

View File

@@ -6,7 +6,7 @@ provider "aws" {
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
@@ -14,6 +14,7 @@ resource "aws_security_group" "instance-west" {
cidr_blocks = ["0.0.0.0/0"]
}
# HTTPS access
ingress {
from_port = 443
to_port = 443
@@ -21,32 +22,45 @@ resource "aws_security_group" "instance-west" {
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 Configuration
resource "aws_launch_configuration" "lc-west" {
name_prefix = "lc-west"
# Define the Launch Template
resource "aws_launch_template" "lt-west" {
name_prefix = "lt-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
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_configuration = aws_launch_configuration.lc-west.id
depends_on = [
aws_launch_configuration.lc-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,
@@ -63,6 +77,10 @@ resource "aws_autoscaling_group" "asg-west" {
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" {

View File

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

View File

@@ -6,6 +6,7 @@ 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"
}
@@ -15,6 +16,7 @@ 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"
}
@@ -24,6 +26,7 @@ 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

@@ -9,6 +9,7 @@ resource "aws_subnet" "us_west_subnet_1" {
tags = {
Name = "${var.region}_${var.us_west_subnet_1_az}_subnet"
}
map_public_ip_on_launch = true
depends_on = [var.vpc_id_west_2]
}
@@ -19,6 +20,7 @@ resource "aws_subnet" "us_west_subnet_2" {
tags = {
Name = "${var.region}_${var.us_west_subnet_2_az}_subnet"
}
map_public_ip_on_launch = true
depends_on = [var.vpc_id_west_2]
}
@@ -29,5 +31,6 @@ resource "aws_subnet" "us_west_subnet_3" {
tags = {
Name = "${var.region}_${var.us_west_subnet_3_az}_subnet"
}
map_public_ip_on_launch = true
depends_on = [var.vpc_id_west_2]
}