hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.82k stars 9.17k forks source link

[Bug]: `aws_autoscaling_schedule` start_time is not recalculated when modifying the resource #38983

Open flostadler opened 2 months ago

flostadler commented 2 months ago

Terraform Core Version

1.5.7

AWS Provider Version

5.63.1

Affected Resource(s)

Expected Behavior

When creating a aws_autoscaling_schedule without start_time, AWS will calculate an appropriate start time based on the recurrence.

On subsequent modifications of the resource, the start_time shouldn't be sent in the API requests in order to trigger the re-computation again.

Actual Behavior

When the schedule is created, the computed start_time is saved to state and subsequent modifications will include this start_time in their API requests.

This can lead to scenarios where the schedule does not fire when expected or the modification fails because AWS whether the start time is in the future.

For example: When creating a schedule without start_time and the following recurrence 0 10 * * SAT (At 10:00 on Saturdays), the start_time will be set by AWS to the first occurrence of this cron job (e.g. 2024-08-24T10:00:00Z). If the recurrence is now modified to 0 8 * * SAT (At 08:00 on Saturdays), the start_time stays at 2024-08-24T10:00:00Z. This will have the effect that the schedule skips the first week.

If the recurrence is now updated on a day after 2024-08-24T10:00:00Z, AWS will fail the API request because the start_time is in the past and they do not allow that: ValidationError: Given start time is in the past.

Relevant Error/Panic Output Snippet

n/a

Terraform Configuration Files

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

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

resource "aws_launch_configuration" "foobar" {
  name = "tfbug-lc"
  image_id = "ami-21f78e11"
  instance_type = "t1.micro"
}

resource "aws_autoscaling_group" "foobar" {
  availability_zones = ["us-west-2a"]
  name = "tfbug-asg"
  max_size = 0
  min_size = 0
  launch_configuration = "${aws_launch_configuration.foobar.name}"
}

variable "recurrence" {
  default = "0 10 * * SAT"
  type = string
}

resource "aws_autoscaling_schedule" "foobar" {
  scheduled_action_name = "tfbug-sched"
  autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
  min_size = 0
  max_size = 0
  desired_capacity = 0
  recurrence = var.recurrence
}

output "start_time" {
  value = aws_autoscaling_schedule.foobar.start_time
}

Steps to Reproduce

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 2 months ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

jd-ucpa commented 1 month ago

There is a bug when we use the autoscaling_schedule.recurrence property. You can't remove it once TF set it.

When you set it then terraform apply, it works :

recurrence = "0 7 * * 1-5"

But when you remove it then then terraform plan or apply the recurrence is still defined in the AWS console

# recurrence = "0 7 * * 1-5"

You can update it to something else but you can't remove it :

recurrence = "0 8 * * 1-5"