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.76k stars 9.12k forks source link

[Bug]: aws_appautoscaling_scheduled_action fails with multiple cron jobs on the same target #33428

Open samuelcortinhas opened 1 year ago

samuelcortinhas commented 1 year ago

Terraform Core Version

1.4.2

AWS Provider Version

5.10.0

Affected Resource(s)

aws_appautoscaling_scheduled_action

Expected Behavior

Update the write capacity of a dynamoDB table in an alternating pattern of scaling in, then scaling out and repeating. The write capacity should update every 15 minutes. (Use case: warm start a table)

Actual Behavior

The resources are successfully deployed. However, only one scheduled action is ever being applied. (E.g. scale out is happening every 30 minutes but scale in never happens.)

This seems to be a concurrency problem. The second scheduled action does not start until the first one ends, but the first one has been configured to never end (the cron job has no end date). Ideally, they should both be able to run at the same time so they can both make changes when they are scheduled too.

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

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

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

resource "aws_dynamodb_table" "users" {
  name           = "tf-test"
  billing_mode   = "PROVISIONED"
  read_capacity  = 10
  write_capacity = 10
  hash_key       = "UserId"

  attribute {
    name = "UserId"
    type = "S"
  }

  lifecycle {
    ignore_changes = [
      read_capacity,
      write_capacity,
    ]
  }
}

resource "aws_appautoscaling_target" "users_write" {
  resource_id        = "table/${aws_dynamodb_table.users.name}"
  scalable_dimension = "dynamodb:table:WriteCapacityUnits"
  service_namespace  = "dynamodb"
  min_capacity       = 10
  max_capacity       = 100

  lifecycle {
    ignore_changes = [
        tags_all,
    ]
  }
}

resource "aws_appautoscaling_scheduled_action" "scale_out" {
  name               = "dynamodb"
  service_namespace  = aws_appautoscaling_target.users_write.service_namespace
  resource_id        = aws_appautoscaling_target.users_write.resource_id
  scalable_dimension = aws_appautoscaling_target.users_write.scalable_dimension
  schedule           = "cron(*/30 * * * ? *)"

  scalable_target_action {
    min_capacity = 30
    max_capacity = 30
  }
}

resource "aws_appautoscaling_scheduled_action" "scale_in" {
  name               = "dynamodb"
  service_namespace  = aws_appautoscaling_target.users_write.service_namespace
  resource_id        = aws_appautoscaling_target.users_write.resource_id
  scalable_dimension = aws_appautoscaling_target.users_write.scalable_dimension
  schedule           = "cron(15-59/30 * * * ? *)"

  scalable_target_action {
    min_capacity = 15
    max_capacity = 15
  }

  depends_on = [
    aws_appautoscaling_scheduled_action.scale_out
  ]
}

Steps to Reproduce

Run apply on the provided Terraform configuration.

Debug Output

No response

Panic Output

No response

Important Factoids

The resource works when only one is used on a target. The problem seems to arise when multiple instances are used on the same target.

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

briody1 commented 8 months ago

I have also experienced this issue. The first "aws_appautoscaling_scheduled_action" is created but the second does not get created. In my case I was creating a scheduled target with "ecs" as the service.

ryutaro-asada commented 8 months ago

Maybe changing the names of the first and second scheduled_action differently could solve the problem.

pagmerek commented 7 months ago

Same issue here for ECS target. Even though the second action depends on the first I get the error

Error: Error creating application autoscaling target: ConcurrentUpdateException: You already have a pending update to an Auto Scaling resource
dbartokthomas commented 7 months ago

Maybe changing the names of the first and second scheduled_action differently could solve the problem.

This is the solution, thank you! Perhaps the provider can have some logic here which stops you from doing this?

ReedSoftware commented 4 months ago

Maybe changing the names of the first and second scheduled_action differently could solve the problem.

This solved it for me as well - whilst I probably should have given them different names anyway, the provider allowing you to give multiple actions the same name and not error and instead basically overwrite the same action in AWS feels like it violates the principle of least surprise.