How can i specify the subnet ID of my ec2 instances created with terraform aws_launch_template?

Viewed 382

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?

2 Answers

You specify the VPC on the aws_autoscaling_group using the vpc_zone_identifier attribute. This takes a list with subnet IDs from a VPC, in which you would want to launch instances. By knowing the subnets, the ASG will automatically know the VPC.

Also, you need to remove the availability_zones attribute. From the docs:

availability_zones: A list of one or more availability zones for the group. Used for EC2-Classic, attaching a network interface via id from a launch template and default subnets when not specified with vpc_zone_identifier argument. Conflicts with vpc_zone_identifier.

Nobody should be using EC2 classic nowadays. Subnets are bound to availability zones, so this information will be known from the vpc_zone_identifier.

Modify my code with the changes suggested by the documentation, thanks to Ervin's answer

My ASG looks like this now

resource "aws_autoscaling_group" "asg_node_app" {
  # launch_configuration  = "${aws_launch_configuration.as_lc_node_app.name}"
  depends_on            = [aws_alb_target_group.node_tg]
  vpc_zone_identifier   = ["${aws_subnet.public_subnet.0.id}","${aws_subnet.public_subnet.1.id}"]
  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"
  }
}

The resources were created wih no problem whatsoever.

Related