Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_autoscaling_schedule defaults desired_capacity to zero with no way of specifying an empty value #6379

Closed
ghost opened this issue Nov 7, 2018 · 2 comments
Labels
service/autoscaling Issues and PRs that pertain to the autoscaling service.

Comments

@ghost
Copy link

ghost commented Nov 7, 2018

This issue was originally opened by @daveadams as hashicorp/terraform#5681. It was migrated here as a result of the provider split. The original body of the issue is below.


Thanks to hashicorp/terraform#4690 we are no longer able to make use of aws_autoscaling_schedule resources. With a config like:

resource "aws_autoscaling_schedule" "asg-scale-up" {
  autoscaling_group_name = "my-asg"
  scheduled_action_name  = "scale-up"
  min_size               = 15
  recurrence             = "0 11 * * mon,tue,wed,thu,fri"
}

We get an error (via Terraform, from the AWS API) that "desired_capacity cannot be less than min_size". But we wish for desired_capacity to be managed by the autoscaling group, not by Terraform, and so we cannot specify a desired capacity. There should be a way to indicate that we do not wish for anything to be specified for desired_capacity.

@bflad bflad added the service/autoscaling Issues and PRs that pertain to the autoscaling service. label Nov 12, 2018
@aeschright aeschright added the needs-triage Waiting for first response or review from a maintainer. label Jun 24, 2019
@bflad bflad added bug Addresses a defect in current functionality. and removed needs-triage Waiting for first response or review from a maintainer. bug Addresses a defect in current functionality. labels Nov 13, 2019
@bflad
Copy link
Contributor

bflad commented Nov 13, 2019

I'm able to confirm this issue on the latest versions of Terraform CLI and the Terraform AWS Provider.

Self-contained Configuration:

terraform {
  required_providers {
    aws = "2.35.0"
  }
  required_version = "0.12.13"
}

provider "aws" {
  region = "us-east-2"
}

data "aws_ami" "test" {
  most_recent = true
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["amzn-ami-hvm-*-x86_64-gp2"]
  }
}

data "aws_availability_zones" "available" {}

resource "aws_launch_template" "test" {
  image_id      = data.aws_ami.test.id
  instance_type = "t3.micro"
  name          = "bflad-testing"
}

resource "aws_autoscaling_group" "test" {
  availability_zones = data.aws_availability_zones.available.names
  desired_capacity   = 0
  max_size           = 0
  min_size           = 0
  name               = "bflad-testing"

  launch_template {
    id = aws_launch_template.test.id
  }
}

resource "aws_autoscaling_schedule" "test" {
  autoscaling_group_name = aws_autoscaling_group.test.name
  scheduled_action_name  = "scale-up"
  min_size               = 15
  recurrence             = "0 11 * * mon,tue,wed,thu,fri"
}

Apply:

$ terraform apply
data.aws_availability_zones.available: Refreshing state...
data.aws_ami.test: Refreshing state...

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_autoscaling_group.test will be created
  + resource "aws_autoscaling_group" "test" {
      + arn                       = (known after apply)
      + availability_zones        = [
          + "us-east-2a",
          + "us-east-2b",
          + "us-east-2c",
        ]
      + default_cooldown          = (known after apply)
      + desired_capacity          = 0
      + force_delete              = false
      + health_check_grace_period = 300
      + health_check_type         = (known after apply)
      + id                        = (known after apply)
      + load_balancers            = (known after apply)
      + max_size                  = 0
      + metrics_granularity       = "1Minute"
      + min_size                  = 0
      + name                      = "bflad-testing"
      + protect_from_scale_in     = false
      + service_linked_role_arn   = (known after apply)
      + target_group_arns         = (known after apply)
      + vpc_zone_identifier       = (known after apply)
      + wait_for_capacity_timeout = "10m"

      + launch_template {
          + id   = (known after apply)
          + name = (known after apply)
        }
    }

  # aws_autoscaling_schedule.test will be created
  + resource "aws_autoscaling_schedule" "test" {
      + arn                    = (known after apply)
      + autoscaling_group_name = "bflad-testing"
      + desired_capacity       = (known after apply)
      + end_time               = (known after apply)
      + id                     = (known after apply)
      + max_size               = (known after apply)
      + min_size               = 15
      + recurrence             = "0 11 * * mon,tue,wed,thu,fri"
      + scheduled_action_name  = "scale-up"
      + start_time             = (known after apply)
    }

  # aws_launch_template.test will be created
  + resource "aws_launch_template" "test" {
      + arn             = (known after apply)
      + default_version = (known after apply)
      + id              = (known after apply)
      + image_id        = "ami-0c64dd618a49aeee8"
      + instance_type   = "t3.micro"
      + latest_version  = (known after apply)
      + name            = "bflad-testing"
    }

Plan: 3 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_launch_template.test: Creating...
aws_launch_template.test: Creation complete after 1s [id=lt-031a10a587afd2718]
aws_autoscaling_group.test: Creating...
aws_autoscaling_group.test: Creation complete after 1s [id=bflad-testing]
aws_autoscaling_schedule.test: Creating...

Error: Error Creating Autoscaling Scheduled Action: ValidationError: Desired capacity must be greater than or equal to min size
	status code: 400, request id: 50eeb002-0633-11ea-8d07-9fd23494c204

  on main.tf line 42, in resource "aws_autoscaling_schedule" "test":
  42: resource "aws_autoscaling_schedule" "test" {

The desired_capacity, min_size, and max_size attributes currently default to 0 when unset. Due to restrictions in the typing system used by Terraform Providers, there is currently no way for the resource to differentiate between 0 and an unset configuration for an integer type. To workaround this, the aws_autoscaling_schedule resource documentation does note that supplying -1 values will leave them unset.

Changing the above resource configuration to the following:

resource "aws_autoscaling_schedule" "test" {
  autoscaling_group_name = aws_autoscaling_group.test.name
  scheduled_action_name  = "scale-up"
  desired_capacity       = -1
  max_size               = -1
  min_size               = 15
  recurrence             = "0 11 * * mon,tue,wed,thu,fri"
}

Does apply successfully without a perpetual difference:

$ terraform apply
data.aws_ami.test: Refreshing state...
data.aws_availability_zones.available: Refreshing state...
aws_launch_template.test: Refreshing state... [id=lt-031a10a587afd2718]
aws_autoscaling_group.test: Refreshing state... [id=bflad-testing]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_autoscaling_schedule.test will be created
  + resource "aws_autoscaling_schedule" "test" {
      + arn                    = (known after apply)
      + autoscaling_group_name = "bflad-testing"
      + desired_capacity       = -1
      + end_time               = (known after apply)
      + id                     = (known after apply)
      + max_size               = -1
      + min_size               = 15
      + recurrence             = "0 11 * * mon,tue,wed,thu,fri"
      + scheduled_action_name  = "scale-up"
      + start_time             = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_autoscaling_schedule.test: Creating...
aws_autoscaling_schedule.test: Creation complete after 0s [id=scale-up]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

$ terraform apply
data.aws_availability_zones.available: Refreshing state...
data.aws_ami.test: Refreshing state...
aws_launch_template.test: Refreshing state... [id=lt-031a10a587afd2718]
aws_autoscaling_group.test: Refreshing state... [id=bflad-testing]
aws_autoscaling_schedule.test: Refreshing state... [id=scale-up]

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

$ aws autoscaling describe-scheduled-actions --auto-scaling-group-name bflad-testing --scheduled-action-names scale-up
{
    "ScheduledUpdateGroupActions": [
        {
            "AutoScalingGroupName": "bflad-testing",
            "ScheduledActionName": "scale-up",
            "ScheduledActionARN": "arn:aws:autoscaling:us-east-2:--OMITTED--:scheduledUpdateGroupAction:675882d8-30a5-4d5a-93a4-cf4102ba0b40:autoScalingGroupName/bflad-testing:scheduledActionName/scale-up",
            "Time": "2019-11-14T11:00:00Z",
            "StartTime": "2019-11-14T11:00:00Z",
            "Recurrence": "0 11 * * mon,tue,wed,thu,fri",
            "MinSize": 15
        }
    ]
}

Since this odd configuration scenario is documented in the resource documentation and working, we're going to opt to close this issue for now. If in the future the Terraform Plugin SDK does support rich data types that can differentiate between type zero values and an unset configuration (there should be an upstream issue coming here 🔜: https://github.com/hashicorp/terraform-plugin-sdk/issues), I would suggest filing a new feature request after that implementation is complete.

@bflad bflad closed this as completed Nov 13, 2019
@ghost
Copy link
Author

ghost commented Dec 14, 2019

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Dec 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
service/autoscaling Issues and PRs that pertain to the autoscaling service.
Projects
None yet
Development

No branches or pull requests

2 participants