terraform-provider-aws icon indicating copy to clipboard operation
terraform-provider-aws copied to clipboard

BadRequestException: Active stages pointing to this deployment must be moved or deleted

Open mparmar19 opened this issue 4 years ago • 13 comments
trafficstars

Community Note

  • I have tried couple of solution but I am seeing the same general error. People have provided multiple solutions using create_before_destroy = true, use trigger etc but none of the solutions work if aws_api_gateway_base_path_mapping is used in same .tf file.

  • The only solution works is to using taint or comment the aws_api_gateway_base_path_mapping block and uncomment again and run the terraform apply twice.

  • In our use case we already have stage deployed using aws_api_gateway_deployment and now we want to enable API logs exported to CloudWatch. The only way is to use aws_api_gateway_stage and aws_api_gateway_method_settings so we are modifying stage as well.

  • I have tried https://github.com/hashicorp/terraform/issues/10674 , https://stackoverflow.com/questions/42760387/terraform-aws-api-gateway-dependency-conundrum/42783769#42783769 , https://github.com/hashicorp/terraform-provider-aws/issues/2918 and couple more I can't even remember.

Terraform CLI and Terraform AWS Provider Version

  • provider version - 3.45.0 (I think I tried couple more)
  • terraform version - tried in 0.13 & 0.14 all different versions.

Affected Resource(s)

  • aws_api_gateway_stage

Terraform Configuration Files

Please include all Terraform configurations required to reproduce the bug. Bug reports without a functional reproduction may be closed without investigation.

resource "aws_api_gateway_deployment" "api_deployment" {
  rest_api_id = aws_api_gateway_rest_api.api.id

  variables = {
    // Forces api deployment  everytime tf apply is run.
    build_time = timestamp()
  }

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_api_gateway_stage" "api_stage" {
  deployment_id = aws_api_gateway_deployment.api_deployment.id
  rest_api_id   = aws_api_gateway_rest_api.api.id
  stage_name    = var.environment
  access_log_settings {
    destination_arn = aws_cloudwatch_log_group.api_gateway_access_logs.arn
    format          = "$context.identity.sourceIp $context.identity.caller $context.identity.user [$context.requestTime] \"$context.httpMethod $context.resourcePath $context.protocol\" $context.status $context.responseLength $context.requestId"
  }
}

resource "aws_cloudwatch_log_group" "api_gateway_access_logs" {
  name = "${aws_api_gateway_rest_api.api.name}-${var.environment}-api-gateway-access-logs"
}

resource "aws_api_gateway_method_settings" "api_method_settings" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  stage_name  = aws_api_gateway_stage.api_stage.stage_name
  method_path = "*/*"

  settings {
    metrics_enabled    = true
    logging_level      = "INFO"
    data_trace_enabled = false
  }
}

resource "aws_api_gateway_base_path_mapping" "stage_mapping" {
  api_id      = aws_api_gateway_rest_api.api.id
  stage_name  = aws_api_gateway_stage.api_stage.stage_name
  domain_name = aws_api_gateway_domain_name.ldl_domain.domain_name
}

Debug Output

terraform apply

021/06/10 14:39:02 [ERROR] eval: *terraform.EvalApplyPost, err: Error creating API Gateway Stage: ConflictException: Stage already exists
2021/06/10 14:39:02 [ERROR] eval: *terraform.EvalSequence, err: Error creating API Gateway Stage: ConflictException: Stage already exists
2021/06/10 14:39:02 [ERROR] eval: *terraform.EvalApplyPost, err: error deleting API Gateway Deployment (dvff3p): BadRequestException: Active stages pointing to this deployment must be moved or deleted
2021/06/10 14:39:02 [ERROR] eval: *terraform.EvalSequence, err: error deleting API Gateway Deployment (dvff3p): BadRequestException: Active stages pointing to this deployment must be moved or deleted
aws_api_gateway_account.aws_api_gateway_cloudwatch_role_assignment: Creation complete after 0s [id=api-gateway-account]

Error: error deleting API Gateway Deployment (dvff3p): BadRequestException: Active stages pointing to this deployment must be moved or deleted



Error: Error creating API Gateway Stage: ConflictException: Stage already exists

  on main.tf line 24, in resource "aws_api_gateway_stage" "api_stage":
  24: resource "aws_api_gateway_stage" "api_stage" {

Important Factoids

References

  • #0000

mparmar19 avatar Jun 15 '21 13:06 mparmar19

Feel free to ask if you are looking for more details.

mparmar19 avatar Jun 15 '21 13:06 mparmar19

I am facing same issue.

"create_before_destroy = true" solution does not work

rgleme avatar Aug 11 '21 22:08 rgleme

Same here. "create_before_destroy = true" isn't working. Error: error deleting API Gateway Deployment (zxcbp2): BadRequestException: Active stages pointing to this deployment must be moved or deleted

Type1J avatar Mar 07 '22 22:03 Type1J

same here

saiaman avatar Nov 30 '22 12:11 saiaman

same

busla avatar Nov 03 '23 13:11 busla

same

magistrser avatar Nov 09 '23 10:11 magistrser

same

DieterDR avatar Nov 09 '23 15:11 DieterDR

+1

pavelpp-topia avatar Jan 04 '24 15:01 pavelpp-topia

Same here

hansregeer avatar Mar 18 '24 11:03 hansregeer

same here

isaacpalomero avatar Mar 26 '24 17:03 isaacpalomero

I have also encountered this, after switching from using a stage_name in the aws_api_gateway_deployment block directly to use an aws_api_gateway_stage block. This is a significant problem and really needs to be addressed, as it's a major degradation of functionality.

demonfoo avatar Apr 03 '24 22:04 demonfoo

for what's it worth:

  lifecycle {
    create_before_destroy = true
  }

on aws_api_gateway_deployment resource worked for me.

% terraform --version
Terraform v1.5.7
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v5.55.0

example of output:

aws_api_gateway_deployment.this["a"]: Creating...
aws_api_gateway_deployment.this["b"]: Creating...
aws_api_gateway_deployment.this["c"]: Creating...
aws_api_gateway_deployment.this["c"]: Creation complete after 0s [id=SANITIZED]
aws_api_gateway_deployment.this["a"]: Creation complete after 0s [id=SANITIZED]
aws_api_gateway_deployment.this["b"]: Creation complete after 2s [id=SANITIZED]
aws_api_gateway_stage.this["b"]: Modifying... [id=SANITIZED]
aws_api_gateway_stage.this["c"]: Modifying... [id=SANITIZED]
aws_api_gateway_stage.this["a"]: Modifying... [id=SANITIZED]
aws_api_gateway_stage.this["b"]: Modifications complete after 1s [id=SANITIZED]
aws_api_gateway_stage.this["c"]: Modifications complete after 1s [id=SANITIZED]
aws_api_gateway_stage.this["a"]: Modifications complete after 1s [id=SANITIZED]
aws_api_gateway_deployment.this["a"] (deposed object 6048297a): Destroying... [id=SANITIZED]
aws_api_gateway_deployment.this["c"] (deposed object 24f3073a): Destroying... [id=SANITIZED]
aws_api_gateway_deployment.this["b"] (deposed object 95a2357a): Destroying... [id=SANITIZED]
aws_api_gateway_deployment.this["a"]: Destruction complete after 0s
aws_api_gateway_deployment.this["b"]: Destruction complete after 0s
aws_api_gateway_deployment.this["c"]: Destruction complete after 0s

meaning: created the new deployment - pointed the stage to new deployment, deleted the deployment.

maybe i was lucky or api-gateway service became faster at switching stages between deployments...

daniel-habib avatar Jun 26 '24 22:06 daniel-habib