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: Do not create cors_configuration when variable not provided #114

Merged
merged 3 commits into from
Aug 2, 2024

Conversation

durnik-ivo
Copy link
Contributor

Description

Filters out null values when checking for var.cors_configuration content and deletes default value for var.cors_configuration.expose_headers, which is not required.

Motivation and Context

When var.cors_configuration is not passed to the module, its content is not {}, because a type is specified for the variable:

      + cors_configuration = {
          + allow_credentials = null
          + allow_headers     = null
          + allow_methods     = null
          + allow_origins     = null
          + expose_headers    = []
          + max_age           = null
        }

this causes condition for the dynamic block to be evaluted incorrectly:

resource "aws_apigatewayv2_api" "this" {
  # [...]

  dynamic "cors_configuration" {
    for_each = local.is_http && length(var.cors_configuration) > 0 ? [var.cors_configuration] : []

    content {
      # [...]
    }
  }

and adds an empty cors_configuration section to the resource:

  # module.api_gateway_orig.aws_apigatewayv2_api.this[0] will be created
  + resource "aws_apigatewayv2_api" "this" {
      + api_endpoint                 = (known after apply)
      + api_key_selection_expression = "$request.header.x-api-key"
      + arn                          = (known after apply)
      + execution_arn                = (known after apply)
      + id                           = (known after apply)
      + name                         = "idurnik_test_module"
      + protocol_type                = "HTTP"
      + route_selection_expression   = "$request.method $request.path"
      + tags                         = {
          + "terraform-aws-modules" = "apigateway-v2"
        }
      + tags_all                     = {
          + "terraform-aws-modules" = "apigateway-v2"
        }

      + cors_configuration {} # <---- Adds empty cors section
    }

instead this section should not be configured:

  # module.api_gateway_mine.aws_apigatewayv2_api.this[0] will be created
  + resource "aws_apigatewayv2_api" "this" {
      + api_endpoint                 = (known after apply)
      + api_key_selection_expression = "$request.header.x-api-key"
      + arn                          = (known after apply)
      + execution_arn                = (known after apply)
      + id                           = (known after apply)
      + name                         = "idurnik_test_module_mine"
      + protocol_type                = "HTTP"
      + route_selection_expression   = "$request.method $request.path"
      + tags                         = {
          + "terraform-aws-modules" = "apigateway-v2"
        }
      + tags_all                     = {
          + "terraform-aws-modules" = "apigateway-v2"
        }

      # <--- no cors section
    }

Breaking Changes

Yes.

How Has This Been Tested?

  • I have updated at least one of the examples/* to demonstrate and validate my change(s)
  • I have tested and validated these changes using one or more of the provided examples/* projects
  • I have executed pre-commit run -a on my pull request

@yungbender
Copy link

yungbender commented Aug 1, 2024

Adding on top of this, when you propagate this

      + cors_configuration {}

Into your api gateway, it forces the gateway to start overriding your CORS headers returned from the integrations, which makes this module a hassle to use.

@@ -16,7 +16,7 @@ resource "aws_apigatewayv2_api" "this" {
body = local.is_http ? var.body : null

dynamic "cors_configuration" {
for_each = local.is_http && length(var.cors_configuration) > 0 ? [var.cors_configuration] : []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a valid change

@@ -27,7 +27,7 @@ variable "cors_configuration" {
allow_headers = optional(list(string))
allow_methods = optional(list(string))
allow_origins = optional(list(string))
expose_headers = optional(list(string), [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be a valid change - will test it out

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not valid - updated in b7ec703

@bryantbiggs bryantbiggs merged commit 0ed8ffe into terraform-aws-modules:master Aug 2, 2024
8 checks passed
antonbabenko pushed a commit that referenced this pull request Aug 2, 2024
## [5.1.1](v5.1.0...v5.1.1) (2024-08-02)

### Bug Fixes

* Do not create cors_configuration when variable not provided ([#114](#114)) ([0ed8ffe](0ed8ffe))
@antonbabenko
Copy link
Member

This PR is included in version 5.1.1 🎉

Copy link

github-actions bot commented Sep 2, 2024

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants