terraform-provider-aws
terraform-provider-aws copied to clipboard
BadRequestException: Active stages pointing to this deployment must be moved or deleted
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_mappingis used in same .tf file. -
The only solution works is to using
taintor comment theaws_api_gateway_base_path_mappingblock and uncomment again and run theterraform applytwice. -
In our use case we already have stage deployed using
aws_api_gateway_deploymentand now we want to enable API logs exported to CloudWatch. The only way is to useaws_api_gateway_stageandaws_api_gateway_method_settingsso 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
Feel free to ask if you are looking for more details.
I am facing same issue.
"create_before_destroy = true" solution does not work
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
same here
same
same
same
+1
Same here
same here
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.
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...