1

When I generate resources, methods etc. using "for_each", how can I make a deployment depend on them? Terraform requires a static list as value for "depends_on"

1 Answer 1

2

I think what you are looking for here is this (somewhat hidden) reference in terraform documents about triggers

I was facing the same issue (using for_each to create gateway methods, integrations) but was not able to reliably trigger api-gateway redeployment, until this...

... or removing the .id references to calculate a hash against whole resources. Be aware that using whole resources will show a difference after the initial implementation. It will stabilize to only change when resources change afterwards

This allow us to do the following in triggers

triggers = {
redeployment = sha1(jsonencode([
  aws_api_gateway_resource.gateway_resources,
  aws_api_gateway_method.gateway_methods,
  aws_api_gateway_integration.gateway_integrations,
]))}

By removing .id (and thus not needing to reference each.key, or any element in the dynamic list) you let terraform decide if the hash of the file changed. If it did it will redeploy, if it doesnt change, then no redeploy required :)

Reference https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_deployment#terraform-resources

  • Look at the comments on 'triggers'
Sign up to request clarification or add additional context in comments.

2 Comments

The problem is that Terraform will often try to create the deployment before the integrations are ready, which will end in failure
Yeh, I think this is sometimes unavoidable as api-gateway deployments are not 'atomic' (at least from what I've seen. You could also add depends_on to ensure thats at least completed in the terraform.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.