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.83k stars 9.18k forks source link

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

Open mparmar19 opened 3 years ago

mparmar19 commented 3 years ago

Community Note

Terraform CLI and Terraform AWS Provider Version

Affected Resource(s)

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

mparmar19 commented 3 years ago

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

rgleme commented 3 years ago

I am facing same issue.

"create_before_destroy = true" solution does not work

Type1J commented 2 years ago

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

saiaman commented 1 year ago

same here

busla commented 1 year ago

same

magistrser commented 1 year ago

same

DieterDR commented 1 year ago

same

pavelpp-topia commented 10 months ago

+1

hansregeer commented 8 months ago

Same here

isaacpalomero commented 7 months ago

same here

demonfoo commented 7 months ago

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.

daniel-habib commented 4 months ago

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...

ziaulrehman40 commented 3 weeks ago

Does anyone cares to close issues in this repo? If terraform providers have such long due issues, people should start considering other tools maybe. Everything has issues, it's just how affectful they are.