Autoscaling groups to use launch_template instead of launch_configuration

Hi,

I am migrating my Autoscaling groups to use launch_template instead of launch_configuration. The plan was successful but getting the below error

ERROR:
Error: creating Auto Scaling Group (sandbox): ValidationError: You must use a valid fully-formed launch template. The parameter groupName cannot be used with the parameter subnet

│ status code: 400, request id: a34faaa6-9297-4b69-81d4-3c83d57c5097

│ with module.app-stack.module.stack-asg.aws_autoscaling_group.asg,

│ on .terraform/modules/app-stack.stack-asg/main.tf line 70, in resource “aws_autoscaling_group” “asg”:

│ 70: resource “aws_autoscaling_group” “asg” {

Below is my code snippet.

New Code
resource “aws_launch_template” “lt” {
name_prefix = “${var.name}-lt”
image_id = var.image_id
instance_type = var.instance_type
ebs_optimized = var.ebs_optimized
vpc_security_group_ids = var.security_groups
user_data = var.user_data
key_name = var.key_name

iam_instance_profile {
name = var.iam_instance_profile
}
block_device_mappings {
device_name = “/dev/xvda” # Root device
ebs {
volume_type = “gp2”
volume_size = var.root_volume_size
delete_on_termination = true
encrypted = true
}
}
block_device_mappings {
device_name = “/dev/xvdb”
ebs {
volume_type = “gp2”
volume_size = var.extra_volume_size
delete_on_termination = true
encrypted = true
}
}
metadata_options {
http_tokens = “required”
}
}

resource “aws_autoscaling_group” “asg” {
depends_on = [aws_launch_template.lt]
name = “${var.name}-asg”
min_size = var.asg_min
max_size = var.asg_max
desired_capacity = var.asg_desired
default_cooldown = 10
launch_template {
id = aws_launch_template.lt.id
}
vpc_zone_identifier = var.vpc_zone_identifier
target_group_arns = [var.target_group_arn]
load_balancers = [var.load_balancer]
suspended_processes = var.asg_suspended_processes

dynamic “tag” {
for_each = { for idx, val in var.tags : idx => val }
content {
key = tag.value.key
value = tag.value.value
propagate_at_launch = true
}
}

lifecycle {
create_before_destroy = true
}
}

Old Code:
#resource “aws_launch_configuration” “lc” {

name_prefix = “${var.name}-lc”

image_id = var.image_id

instance_type = var.instance_type

ebs_optimized = var.ebs_optimized

iam_instance_profile = var.iam_instance_profile

security_groups = var.security_groups

user_data = var.user_data

key_name = var.key_name

metadata_options {

http_tokens = “required”

}

lifecycle {

create_before_destroy = true

}

root_block_device {

volume_type = “gp2”

volume_size = var.root_volume_size

encrypted = true

}

ebs_block_device {

device_name = “/dev/xvdb”

volume_type = “gp2”

volume_size = var.extra_volume_size

delete_on_termination = true

encrypted = true

}

#}

#resource “aws_autoscaling_group” “asg” {

name = “${var.name}-asg”

min_size = var.asg_min

max_size = var.asg_max

desired_capacity = var.asg_desired

default_cooldown = 10

launch_configuration = aws_launch_configuration.lc.name

vpc_zone_identifier = var.vpc_zone_identifier

target_group_arns = [var.target_group_arn]

load_balancers = [var.load_balancer]

suspended_processes = var.asg_suspended_processes

dynamic “tag” {

for_each = { for idx, val in var.tags : idx => val }

content {

key = tag.value.key

value = tag.value.value

propagate_at_launch = true

}

}

lifecycle {

create_before_destroy = true

}

#}