Terraform: aws_autoscaling_schedule - cannot set zero to max/min/desired

Created on 15 Jan 2016  ยท  11Comments  ยท  Source: hashicorp/terraform

Hi!

So I'm attempting to create a aws_autoscaling_schedule resource that shuts down all my instances overnight. When I try to do this with terraform however, any field of max_size/min_size/desired_capacity that is set to zero is effectively ignored. If they're all set to zero, you get this error:

Error Creating Autoscaling Scheduled Action: ValidationError: At least one of max size, min size, or desired capacity must be specified
    status code: 400, request id: 02af3c57-bba3-11e5-a6c3-99fcfc3310db

If I set any single field to a non-zero value, then my autoscaling schedule (via aws autoscaling describe-scheduled-action) looks like this (min/desired set to zero, max set to 2):

{
    "ScheduledUpdateGroupActions": [
        {
            "AutoScalingGroupName": "xxx",
            "MaxSize": 2,
            "ScheduledActionARN": "xxx",
            "ScheduledActionName": "ShutdownAfterBusinessHours",
            "StartTime": "2016-01-15T16:20:00Z",
            "Time": "2016-01-15T16:20:00Z",
            "EndTime": "2016-01-15T16:30:00Z"
        }
    ]
}

Looking at the code in resource_aws_autoscheduling_schedule.go, I noticed the following:

    if attr, ok := d.GetOk("min_size"); ok {
        params.MinSize = aws.Int64(int64(attr.(int)))
    }

    if attr, ok := d.GetOk("max_size"); ok {
        params.MaxSize = aws.Int64(int64(attr.(int)))
    }

    if attr, ok := d.GetOk("desired_capacity"); ok {
        params.DesiredCapacity = aws.Int64(int64(attr.(int)))
    }

The d.GetOk call is specifically meant to not return zero values. Changing this to just Ok wouldn't work very well since then (as I understand it), not specifying a field would be equivalent to a zero value.

So, looking for a little guidance on how you'd like to resolve this, then I can submit a PR. Thanks!

bug provideaws

Most helpful comment

Unfortunately this "fix" broke our use case for this resource. We don't want to specify desired_capacity or max_size in our scheduled action. But thanks to the default of zero, we now get an error that desired_capacity cannot be less than min_size.

All 11 comments

Oh, I should note - I did attempt to use zero values through the AWS CLI manually, and it does fully accept a scheduled scaling action that sets min/desired to zero, which is the effect we're looking for.

@AirbornePorcine please can you post a short snippet of your terraform config that I can use as an acceptance test to fix this bug? (Remember to remove secrets from the config if there are any)

For sure.

resource "aws_autoscaling_schedule" "ScaleDown" {
  provider = "aws"
  autoscaling_group_name = "MyAutoScalingGroup"
  scheduled_action_name = "ShutdownAfterBusinessHours"
  max_size = 0
  min_size = 0
  desired_capacity = 0
  start_time = "2016-01-16T07:00:00Z"
  end_time = "2016-01-16T13:00:00Z"
}

thanks @AirbornePorcine, I will submit a PR to fix this shortly

@AirbornePorcine ok, I have been able to add a test to prove this is the case. _unfortunately_, I believe this is actually a known bug and will need solving in Terraform core

I have opened a PR with the test to show the issue to the core team #4693

Just wanted to leave a "me too!" note, as i was trying to shut down our integration/staging environments over night and now have 1 instance still running ;-)

@AirbornePorcine (and @jangrewe) so a PR has just been merged that sets default values for min_size, max_size and desired_capacity to 0. This means that your 0 will now work by default :)

Hope this helps!

It does, thanks! =)

Thanks, works for us!

Unfortunately this "fix" broke our use case for this resource. We don't want to specify desired_capacity or max_size in our scheduled action. But thanks to the default of zero, we now get an error that desired_capacity cannot be less than min_size.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings