I want to lauch EC2 instances with an autoscaling group.
I have created a security group wih an inbound rule that comes from the ALB security group and its associated with my custom VPC.
resource "aws_security_group" "asg_sg" {
name = "alb-inbound"
description = "Security group for autoscaling instances"
vpc_id = aws_vpc.vpc_prod.id
}
resource "aws_security_group_rule" "inbound_alb" {
type = "ingress"
from_port = 3000
to_port = 3000
protocol = "tcp"
security_group_id = "${aws_security_group.asg_sg.id}"
source_security_group_id = "${aws_security_group.alb_sg.id}"
}
I want to deploy some instances with an autoscaling group as a test for a project
My launch_template configuration looks like this.
resource "aws_launch_template" "as_lt_node_app" {
name_prefix = "as-lt-"
image_id = data.aws_ami.amzlinux2.id
instance_type = "t2.micro"
vpc_security_group_ids = ["${aws_security_group.asg_sg.id}"]
user_data = base64encode(file("${path.module}/install.sh"))
}
My autoscaling group configuration is the following.
resource "aws_autoscaling_group" "asg_node_app" {
# launch_configuration = "${aws_launch_configuration.as_lc_node_app.name}"
availability_zones = [var.azs[0],var.azs[1]]
min_size = 1
max_size = 4
target_group_arns = ["${aws_alb_target_group.node_tg.arn}"]
health_check_type = "ELB"
desired_capacity = 2
launch_template {
id = aws_launch_template.as_lt_node_app.id
version = "$Latest"
}
}
Now when i run terraform apply i get this error.
│ Error: Error creating Auto Scaling Group: ValidationError: You must use a valid fully-
formed launch template. Security group sg-01510848bd1c3a79d and subnet subnet-c5d624b8
belong to different networks.
│ status code: 400, request id: 8318256b-4c03-4be5-8be0-51473bc1e0b7
│
│ with aws_autoscaling_group.asg_node_app,
│ on c6-02_autoscaling.tf line 22, in resource "aws_autoscaling_group" "asg_node_app":
│ 22: resource "aws_autoscaling_group" "asg_node_app" {
It appears that the EC2 instances created with this template are deployed in the default VPC subnet. Is there a way to specify the VPC i am working with in the launch_template?
I tried this autoscaling group with launch_configuration but i got a similar error.
I want to try the network_interfaces parameter inside the laun_template, but in this deployment i create a list of subnets
resource "aws_subnet" "public_subnet" {
count = length(var.public_cidr)
availability_zone = var.azs[count.index]
vpc_id = aws_vpc.vpc_prod.id
cidr_block = var.public_cidr[count.index]
tags = local.common_tags
}
Can i work with more than one subnet in this case?