From 7a620b3a7a549597d290efc3e303287a5e23a136 Mon Sep 17 00:00:00 2001 From: Greg Hendrickson Date: Fri, 17 Nov 2023 11:06:04 -0800 Subject: [PATCH] launch configuration to launch template --- .gitignore | 1 + .../ec2/ec2-east/{ec2.tf => ec2-east.tf} | 49 +++++++++++++------ .../modules/ec2/ec2-east/outputs.tf | 4 -- .../modules/ec2/ec2-east/terraform.tfvars | 2 +- .../ec2/ec2-west/{ec2.tf => ec2-west..tf} | 46 +++++++++++------ .../modules/ec2/ec2-west/terraform.tfvars | 2 +- .../vpc/subnets/us-east/subnets-east.tf | 3 ++ .../vpc/subnets/us-west/subnets-west.tf | 3 ++ 8 files changed, 74 insertions(+), 36 deletions(-) rename aws/aws_simple/modules/ec2/ec2-east/{ec2.tf => ec2-east.tf} (62%) rename aws/aws_simple/modules/ec2/ec2-west/{ec2.tf => ec2-west..tf} (64%) diff --git a/.gitignore b/.gitignore index 2462613..7f01bc4 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/aws/aws_simple/modules/ec2/ec2-east/ec2.tf b/aws/aws_simple/modules/ec2/ec2-east/ec2-east.tf similarity index 62% rename from aws/aws_simple/modules/ec2/ec2-east/ec2.tf rename to aws/aws_simple/modules/ec2/ec2-east/ec2-east.tf index cdbaf94..b68f61d 100644 --- a/aws/aws_simple/modules/ec2/ec2-east/ec2.tf +++ b/aws/aws_simple/modules/ec2/ec2-east/ec2-east.tf @@ -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" { diff --git a/aws/aws_simple/modules/ec2/ec2-east/outputs.tf b/aws/aws_simple/modules/ec2/ec2-east/outputs.tf index d5f2320..e69de29 100644 --- a/aws/aws_simple/modules/ec2/ec2-east/outputs.tf +++ b/aws/aws_simple/modules/ec2/ec2-east/outputs.tf @@ -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] -# } \ No newline at end of file diff --git a/aws/aws_simple/modules/ec2/ec2-east/terraform.tfvars b/aws/aws_simple/modules/ec2/ec2-east/terraform.tfvars index e591122..62c817a 100644 --- a/aws/aws_simple/modules/ec2/ec2-east/terraform.tfvars +++ b/aws/aws_simple/modules/ec2/ec2-east/terraform.tfvars @@ -1,2 +1,2 @@ -min_size = 1 +min_size = 3 max_size = 10 \ 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-west..tf similarity index 64% rename from aws/aws_simple/modules/ec2/ec2-west/ec2.tf rename to aws/aws_simple/modules/ec2/ec2-west/ec2-west..tf index 69277bf..9cdc72f 100644 --- a/aws/aws_simple/modules/ec2/ec2-west/ec2.tf +++ b/aws/aws_simple/modules/ec2/ec2-west/ec2-west..tf @@ -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" { diff --git a/aws/aws_simple/modules/ec2/ec2-west/terraform.tfvars b/aws/aws_simple/modules/ec2/ec2-west/terraform.tfvars index e591122..62c817a 100644 --- a/aws/aws_simple/modules/ec2/ec2-west/terraform.tfvars +++ b/aws/aws_simple/modules/ec2/ec2-west/terraform.tfvars @@ -1,2 +1,2 @@ -min_size = 1 +min_size = 3 max_size = 10 \ 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 f846e52..b15c20a 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 @@ -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" } 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 2f3f099..4e6d645 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 @@ -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] }