Skip to content
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

fix: Allow for custom_iam_policy_arns that are unknown at apply #46

Merged
merged 1 commit into from
Oct 11, 2023
Merged

fix: Allow for custom_iam_policy_arns that are unknown at apply #46

merged 1 commit into from
Oct 11, 2023

Commits on Oct 11, 2023

  1. fix: Allow for custom_iam_policy_arns that don't exist yet

    PROBLEM:
    
    Prior to this, specifying `custom_iam_policy_arns` for IAM Policies that do not
    exist yet and would be created in the same Terraform run that creates the
    Lambda Execution Role would cause the following error:
    
    ```
    │ Error: Invalid for_each argument
    │
    │   on .terraform/modules/foo.test_lambda/iam-role.tf line 81, in resource "aws_iam_role_policy_attachment" "custom":
    │   81:   for_each = local.enabled && length(var.custom_iam_policy_arns) > 0 ? var.custom_iam_policy_arns : toset([])
    │     ├────────────────
    │     │ local.enabled is true
    │     │ var.custom_iam_policy_arns is set of string with 3 elements
    │
    │ The "for_each" set includes values derived from resource attributes that cannot be determined until apply, and so Terraform cannot determine the full set of keys that will identify the instances of this resource.
    │
    │ When working with unknown values in for_each, it's better to use a map value where the keys are defined statically in your configuration and where only the values contain apply-time results.
    │
    │ Alternatively, you could use the -target planning option to first apply only the resources that the for_each value depends on, and then apply a second time to fully converge.
    ```
    
    This is due to the ARN's of those policies not being known and the usage of sets in `for_each` for the `aws_iam_role_policy_attachment` resource. As the set's values are unknown at apply time, Terraform can't create a dependency graph.
    
    SOLUTION:
    
    * Don't use `toset()` for the `for_each` in the `aws_iam_role_policy_attachment` resource.
    * Build a map of ARNs to attach to the Lambda execution role. Use this map as the value of `for_each`.
    
    OUTCOME:
    
    * Terraform doesn't error when using `aws_iam_role_policy_attachment` for policies that don't exist yet but will exist after the apply.
    natemccurdy committed Oct 11, 2023
    Configuration menu
    Copy the full SHA
    a99e727 View commit details
    Browse the repository at this point in the history