Ole
Ole

Reputation: 208

How to make AWS Api Gateway deployment depend on dynamic list using Terraform

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"

Upvotes: 1

Views: 3218

Answers (1)

to0ns88
to0ns88

Reputation: 41

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'

Upvotes: 2

Related Questions