Pending update to Auto Scaling resource: Terraform ConcurrentUpdateException

I’m experiencing an issue with Application Auto Scaling when I run apply in Terraform. This error occurs when a pending update is requested to an Application Auto Scaling resource:

Error creating application autoscaling target: ConcurrentUpdateException: You already have a pending update to an Auto Scaling resource.
    status code: 400, request id: dd93d5d3-314c-11ea-aeb2-d9246c0fa0b9

  on autoscaling.tf line 1, in resource "aws_appautoscaling_target" "autoscaling-lambda-target":
   1: resource "aws_appautoscaling_target" "autoscaling-lambda-target" {

How can I ensure that this error is avoided?

My autoscaling.tf:

resource "aws_appautoscaling_target" "lambda-target" {

  depends_on = [
    aws_lambda_alias.alias-qa
  ]
  for_each = aws_lambda_function.lambda-function

  max_capacity = local.lambda_functions[each.key].max 
  min_capacity = local.lambda_functions[each.key].min 
  resource_id = "function:${each.value.function_name}:${var.stage}"
  scalable_dimension = "lambda:function:ProvisionedConcurrency"
  service_namespace = "lambda"
}

I am trying to configure Application Auto Scaling to manage provisioned concurrency in a lambda function via Terraform. I occasionally receive the error ConcurrentUpdateException: You already have a pending update to an Auto Scaling resource when running apply. How can I ensure that this error is avoided?

To avoid the ConcurrentUpdateException error in Application Auto Scaling when running apply in Terraform, you can add the lifecycle block to your aws_appautoscaling_target resource and set the prevent_destroy parameter to true. This will ensure that Terraform will not try to update or delete the resource when there is a pending update to the Auto Scaling resource.

resource "aws_appautoscaling_target" "lambda-target" {
  depends_on = [
    aws_lambda_alias.alias-qa
  ]
  for_each = aws_lambda_function.lambda-function

  max_capacity        = local.lambda_functions[each.key].max 
  min_capacity        = local.lambda_functions[each.key].min 
  resource_id         = "function:${each.value.function_name}:${var.stage}"
  scalable_dimension  = "lambda:function:ProvisionedConcurrency"
  service_namespace   = "lambda"

  lifecycle {
    prevent_destroy = true
  }
}