Terraform-provider-aws: Allow the Autoscaling Groups data source to export DesiredCapacity

Created on 11 Dec 2017  路  3Comments  路  Source: hashicorp/terraform-provider-aws

Hi there,

I'd like to be able to create an ASG with the same capacity as the existing one when using the "create_before_destroy" pattern

A first step needed to do that would be to retrieve the DesiredCapacity from the pre-existing ASG, but currently the autoscaling groups data source does not export the DesiredCapacity (nor any other property from http://docs.aws.amazon.com/sdk-for-go/api/service/autoscaling/#Group, beside the name)

Terraform Version

Any

Affected Resource(s)

  • aws_autoscaling_groups

Terraform Configuration Files

This is what I would like to be able to do:

data "terraform_remote_state" "state" {
  backend = "s3"

  "config" {
    "bucket"  = "..."
    "encrypt" = "true"
    "key"     = "..."
    "region"  = "..."
  }
}

# let's retrieve the previous asg from the remote state
data "aws_autoscaling_groups" "someapp" {
  filter {
    name   = "auto-scaling-group"
    values = ["${data.terraform_remote_state.state.asg_name}"]
  }
}

resource "aws_launch_configuration" "someapp" {
  image_id       = "${var.ami}"
  instance_type  = "${var.instance_type}"
  key_name       = "${var.key_name}"
  security_group = ["${var.security_group}"]

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_autoscaling_group" "someapp" {
  name                 = "someapp - ${aws_launch_configuration.someapp.name}"
  launch_configuration = "${aws_launch_configuration.someapp.name}"

  # We can use the desired_capacity of the previous asg that we are replacing below:
  desired_capacity    = "${aws_autoscaling_groups.someapp.desired_capacity}"
  min_size            = "${var.min_size}"
  max_size            = "${var.max_size}"
  availability_zones  = ["${var.azs}"]
  vpc_zone_identifier = ["${var.subnet_ids}"]
  load_balancers      = ["${aws_elb.someapp.id}"]

  lifecycle {
    create_before_destroy = true
  }
}

# Now we need to export the asg_name so it can be retrieved at the next apply :)
output "asg_name" {
  value = "${aws_autoscaling_group.someapp.name}"
}

Expected Behavior

I should be able to use data.aws_autoscaling_groups.someapp.desired_capacity to retrieve the current value of desired_capacity for an existing ASG.

Actual Behavior

It is not supported :)

enhancement servicautoscaling

Most helpful comment

I would also like to have data for an existing aws_autoscaling_group, my use case is that I am manually scaling the group and when I subsequently run terraform plan to change some other aspect, it will reset the desired_capacity rather than use the current desired_capacity. I have to notice the difference and edit the desired_capacity manually, which is error prone.

All 3 comments

I would also like to have data for an existing aws_autoscaling_group, my use case is that I am manually scaling the group and when I subsequently run terraform plan to change some other aspect, it will reset the desired_capacity rather than use the current desired_capacity. I have to notice the difference and edit the desired_capacity manually, which is error prone.

Just an observation for the current data source behavior, if I filter for multiple tags, let's say filter on two tags, it will show all asgs that match any one of the two tags, but the asg that match both tags will show up twice in the results.

I created https://www.terraform.io/docs/providers/aws/d/autoscaling_group.html to address this. The datasource provides access to the attributes of a single autoscaling group and you are expected to use the autoscaling_groups datasource for the querying/filtering.

However, a misjudgement on my part means you are unable to get the desired_capacity of an existing ASG while simultaneously updating that ASG. My initial tests involved creating an ASG with a dynamic name with the create_before_destroy lifecycle attribute set to true. So for any updates terraform would make a new ASG with a new name then destroy the old one. However, it wanted to use this new ASG for the data source instead of the existing ASG.

I'm currently brainstorming how to make this work, but am quite new to not only the terraform codebase, but the Go language as well.

Was this page helpful?
0 / 5 - 0 ratings