-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
aws_api_gateway_deployment doesn't get updated after changes #162
Comments
+1 :) |
Hitting this right now as well, took me a long time to realise I had to deploy the API in the console to see all my changes ;) |
A seemingly decent suggestion from the comments on the original ticket was to add a 'triggers' map on the deployment resource: hashicorp/terraform#6613 (comment) - this seems like maybe the least painful quick fix to the issue. |
Do we have the fix for this now ? |
Sharing this here as well since the original was closed: Using stage_description seems like a good work-around to solve this issue in the short term.
|
If I'm understanding correctly, the mentioned "triggers" map idea isn't available to use here - it's just a proposal from one of the terraform core developers for something that could be implement as an interim solution, correct? Looking at the We could simply trigger a new deployment every time, e.g. using |
I'm encountering a similar issue. I've setup the stage variable based on a MD5 Hash of the terraform file but the problem I'm currently running into is that deploying a private API Gateway with a resource policy fails the first time, because the API Gateway gets created first, then the resource policy gets applied, but the API Gateway never gets redeployed after the resource policy is applied. This requires manually deploying the API Gateway the first time it's deployed, which is a lot for us as we are using branch based deployments in our CI/CD pipeline. |
@marcato15 I had some similar issues -- create API Gateway from swagger file, terraform api_gateway resource sets policy, then second run removes policy as it's not defined in swagger file, then third run creates policy again and it goes on and on.. Fixed by adding |
A workaround we use:
The effect is that when the API changes, the deployment is replaced and the stage updated. When the API doesn't change, the deployment doesn't change (a disadvantage of using timestamp() for the deployment variable value) One more "astuce": use
in the aws_api_gateway_deployment resource to avoid "Active stages" errors. |
This WA has serious disadvantages which actually I can't work around yet. Basically I have WAF association with each stage, after recreation of deployment WAF association get lost of course. And there is no chance in terraform to rerun WAF association at same time. |
Sharing our workaround for this issue. Initially we tried hashing the whole API definition file but then we ran into the issue that changes to variable values did not trigger a new deployment. For example, we had a lambda integration where the function name changed. The Currently we follow the pattern below for our APIs. Here we build a hash from the full JSON representations of all the resources that would affect the deployment, which means resource, methods, integrations, the various response types and the models. Maybe there are more types that need to go in here, but these are the ones we have found necessary. We need to keep this list updated when we add resources and that opens up for mistakes, but this is the approach that gives us new deployments at the right time without creating them unnecessarily. resource "aws_api_gateway_deployment" "demo" {
rest_api_id = aws_api_gateway_rest_api.demo.id
variables = {
// For new changes to the API to be correctly deployed, they need to
// be detected by terraform as a trigger to recreate the aws_api_gateway_deployment.
// This is because AWS keeps a "working copy" of the API resources which does not
// go live until a new aws_api_gateway_deployment is created.
// Here we use a dummy stage variable to force a new aws_api_gateway_deployment.
// We want it to detect if any of the API-defining resources have changed so we
// hash all of their configurations.
// IMPORTANT: This list must include all API resources that define the "content" of
// the rest API. That means anything except for aws_api_gateway_rest_api,
// aws_api_gateway_stage, aws_api_gateway_base_path_mapping, that are higher-level
// resources. Any change to a part of the API not included in this list might not
// trigger creation of a new aws_api_gateway_deployment and thus not fully deployed.
trigger_hash = sha1(join(",", [
jsonencode(aws_api_gateway_resource.demo),
jsonencode(aws_api_gateway_method.demo_get),
jsonencode(aws_api_gateway_integration.demo_get),
jsonencode(aws_api_gateway_integration_response.demo_get_200),
jsonencode(aws_api_gateway_integration_response.demo_get_400),
jsonencode(aws_api_gateway_integration_response.demo_get_500),
jsonencode(aws_api_gateway_method_response.demo_get_200),
jsonencode(aws_api_gateway_method_response.demo_get_400),
jsonencode(aws_api_gateway_method_response.demo_get_500),
jsonencode(aws_api_gateway_model.demo_request_body),
jsonencode(aws_api_gateway_model.demo_api_response),
jsonencode(aws_api_gateway_model.demo_error_response),
//
// Etc. ...
//
]))
}
lifecycle {
create_before_destroy = true
}
} |
@staffan-einarsson I tried your workaround, and got two issues. Hope you can help out:
|
Hi @zihaoyu! Thanks for trying it.
I'm not sure if this explains your issue but this might happen if you use the optional I think it works like this:
We opted for the first option to avoid the risk for downtime, but also because we had other attributes on the
Hmm, I'm thinking this is to be expected behavior given that But if you're already using terraform to manage your infrastructure, I don't see why I would use the deployment switching feature for rollbacks, since you have much more powerful and broad configuration management at your disposal. Just make sure you version your terraform files before applying and if you decide that you want to roll back, re-apply from an earlier version. |
@bassmanitram's solution works just fine when using an OpenAPI Spec file. However, I couldn't use |
I had some trouble using @staffan-einarsson's solution when encoding the resources into JSON:
Perhaps it was because we're running Terraform 0.11? I'm not sure. Instead, I'm now hashing the Terraform file and the module's variable values: locals {
api_depends_on = [
"${var.name}",
"${var.environment}",
# Etc...
"${sha1(file("${path.module}/main.tf"))}"
]
trigger_hash = "Terraform hash to trigger deploys: ${sha1(join(",", "${local.api_depends_on}"))}"
} I'm hoping that this means we'll only need to update |
Reference: #162 Reference: https://www.terraform.io/docs/providers/null/resource.html#triggers Reference: https://www.terraform.io/docs/providers/random/#resource-quot-keepers-quot- Reference: https://www.terraform.io/docs/providers/time/#resource-quot-triggers-quot- Since it does not appear there will be functionality added anytime soon in the Terraform core to support resource configuration that automatically triggers resource recreation when referenced resources are updated, this introduces a `triggers` map argument similar to those utilized by the `null`, `random`, and `time` providers. This can be used by operators to automatically force a new resource (redeployment) using key/value criteria of their choosing. Its usage is fairly advanced, so caveats are added to the documentation. We do not intend to add this class of argument to all Terraform AWS Provider resources due to its complexity and potentially awkward configuration, however, this is a pragmatic compromise for this particular resource which does not fit well into Terraform's usual provisioning model. Output from acceptance testing: ``` --- PASS: TestAccAWSAPIGatewayDeployment_basic (24.67s) --- PASS: TestAccAWSAPIGatewayDeployment_StageDescription (24.72s) --- PASS: TestAccAWSAPIGatewayDeployment_StageName (86.00s) --- PASS: TestAccAWSAPIGatewayDeployment_StageName_EmptyString (128.10s) --- PASS: TestAccAWSAPIGatewayDeployment_disappears_RestApi (157.14s) --- PASS: TestAccAWSAPIGatewayDeployment_Triggers (204.69s) --- PASS: TestAccAWSAPIGatewayDeployment_Description (250.19s) --- PASS: TestAccAWSAPIGatewayDeployment_Variables (432.41s) ```
Reference: #162 Reference: https://www.terraform.io/docs/providers/null/resource.html#triggers Reference: https://www.terraform.io/docs/providers/random/#resource-quot-keepers-quot- Reference: https://www.terraform.io/docs/providers/time/#resource-quot-triggers-quot- Since it does not appear there will be functionality added anytime soon in the Terraform core to support resource configuration that automatically triggers resource recreation when referenced resources are updated, this introduces a `triggers` map argument similar to those utilized by the `null`, `random`, and `time` providers. This can be used by operators to automatically force a new resource (redeployment) using key/value criteria of their choosing. Its usage is fairly advanced, so caveats are added to the documentation. We do not intend to add this class of argument to all Terraform AWS Provider resources due to its complexity and potentially awkward configuration, however, this is a pragmatic compromise for this particular resource which does not fit well into Terraform's usual provisioning model. Output from acceptance testing: ``` --- PASS: TestAccAWSAPIGatewayDeployment_basic (24.67s) --- PASS: TestAccAWSAPIGatewayDeployment_StageDescription (24.72s) --- PASS: TestAccAWSAPIGatewayDeployment_StageName (86.00s) --- PASS: TestAccAWSAPIGatewayDeployment_StageName_EmptyString (128.10s) --- PASS: TestAccAWSAPIGatewayDeployment_disappears_RestApi (157.14s) --- PASS: TestAccAWSAPIGatewayDeployment_Triggers (204.69s) --- PASS: TestAccAWSAPIGatewayDeployment_Description (250.19s) --- PASS: TestAccAWSAPIGatewayDeployment_Variables (432.41s) ```
Hi folks 👋 Since it does not appear there will be functionality added anytime soon in Terraform core to support a form of resource configuration that will automatically triggers resource recreation when referenced resources are updated, the If this type of enhancement does not fit your needs, we would encourage you to file a new issue (potentially upstream in Terraform core since there's not much else we can do at the provider level). Please also note that we do not intend to add this class of argument to all Terraform AWS Provider resources due to its complexity and potentially awkward configuration. |
This has been released in version 2.61.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks! |
…#13054) Reference: hashicorp#162 Reference: https://www.terraform.io/docs/providers/null/resource.html#triggers Reference: https://www.terraform.io/docs/providers/random/#resource-quot-keepers-quot- Reference: https://www.terraform.io/docs/providers/time/#resource-quot-triggers-quot- Since it does not appear there will be functionality added anytime soon in the Terraform core to support resource configuration that automatically triggers resource recreation when referenced resources are updated, this introduces a `triggers` map argument similar to those utilized by the `null`, `random`, and `time` providers. This can be used by operators to automatically force a new resource (redeployment) using key/value criteria of their choosing. Its usage is fairly advanced, so caveats are added to the documentation. We do not intend to add this class of argument to all Terraform AWS Provider resources due to its complexity and potentially awkward configuration, however, this is a pragmatic compromise for this particular resource which does not fit well into Terraform's usual provisioning model. Output from acceptance testing: ``` --- PASS: TestAccAWSAPIGatewayDeployment_basic (24.67s) --- PASS: TestAccAWSAPIGatewayDeployment_StageDescription (24.72s) --- PASS: TestAccAWSAPIGatewayDeployment_StageName (86.00s) --- PASS: TestAccAWSAPIGatewayDeployment_StageName_EmptyString (128.10s) --- PASS: TestAccAWSAPIGatewayDeployment_disappears_RestApi (157.14s) --- PASS: TestAccAWSAPIGatewayDeployment_Triggers (204.69s) --- PASS: TestAccAWSAPIGatewayDeployment_Description (250.19s) --- PASS: TestAccAWSAPIGatewayDeployment_Variables (432.41s) ```
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
This issue was originally opened by @blalor as hashicorp/terraform#6613. It was migrated here as part of the provider split. The original body of the issue is below.
aws_api_gateway_deployment
doesn't get updated after a dependent resource changes. For example, if I change aaws_api_gateway_integration
resource to modify therequest_template
property,aws_api_gateway_deployment
should be triggered. Since that doesn't happen, the stage specified in the deployment continues to use the old configuration.depends_on
doesn't seem to be sufficient; I tried capturing that dependency and it didn't work, and as I understand it,depends_on
only captures ordering.A workaround is to taint the
aws_api_gateway_deployment
resource after a successful apply and re-running apply.The text was updated successfully, but these errors were encountered: