terraform-aws-modules / terraform-aws-apigateway-v2

Terraform module to create AWS API Gateway v2 (HTTP/WebSocket) πŸ‡ΊπŸ‡¦
https://registry.terraform.io/modules/terraform-aws-modules/apigateway-v2/aws
Apache License 2.0
151 stars 203 forks source link

fix: Issues when fetching route53 zone #120

Closed atorrescogollo closed 3 months ago

atorrescogollo commented 3 months ago

Description

Records registration seems to be partially broken

Motivation and Context

Given this code:

module "api_gateway" {
  source = "../.."

  name          = "demo"
  description   = "API Gateway"
  protocol_type = "HTTP"

  # Custom domain
  domain_name = "demo.example.com" # route53 zone "example.com" already exists

  routes = {
    "POST /" = {
      integration = {
        uri                    = "arn:aws:lambda:eu-west-1:012345678901:function:my-function"
        payload_format_version = "2.0"
        timeout_milliseconds   = 12000
      }
    }
  }
}

It fails to get the zone_id since it tries demo.example.com instead of just example.com.

This PR was made to fix that. Also, it includes a way to avoid doing that data lookup and using a variable instead.

Now there are three ways to register the domain records:

  1. Fetch the zone_id and register demo.example.com records
    domain_name = "demo.example.com"
  2. Fetch the zone_id and register demo1.example.com and demo2.example.com records
    domain_name = "*.example.com"
    subdomains  = ["demo1", "demo2"]
  3. Register demo.example.com records in the given zone id
    domain_name    = "demo.example.com"
    domain_zone_id = "Z1234567890"

Breaking Changes

How Has This Been Tested?

tonnenpinguin commented 3 months ago

FYI: I implemented a fix for the same issue here: https://github.com/terraform-aws-modules/terraform-aws-apigateway-v2/pull/119 πŸ™‚

atorrescogollo commented 3 months ago

Hey @tonnenpinguin sorry , my fault, I was debugging the issue at first and I didn't realize your PR (I thought it was for just allowing var.hosted_zone_name).

However, your PR doesn't fix the issue I described here. It only allows you to set the var.hosted_zone_name ad-hoc. I can't successfully run a plan against this:


module "api_gateway" {
  source = "git@github.com:tonnenpinguin/terraform-aws-apigateway-v2.git?ref=fix/hosted_zone_name"

  name          = "demo"
  description   = "API Gateway"
  protocol_type = "HTTP"

  # Custom domain
  domain_name = "demo.example.com" # Having an example.com zone already created
}
β”‚ Error: no matching Route 53 Hosted Zone found
β”‚ 
β”‚   with module.api_gateway.data.aws_route53_zone.this[0],
β”‚   on .terraform/modules/api_gateway/main.tf line 134, in data "aws_route53_zone" "this":
β”‚  134: data "aws_route53_zone" "this" {
β”‚ 

I have to explicitly say this to make it work (which should behave in the same way AFAIK):

module "api_gateway" {
  ...

  hosted_zone_name = "example.com"
}

If you implement that fix, tell me and I'll close this one. Since you had reviews already, I think it makes more sense to keep yours instead.

atorrescogollo commented 3 months ago

On the other hand, I would prefer to forward the zone_id instead of the zone_name.

I have a personal reference about trying to send IDs and ARNs when possible and against doing data lookups inside modules. The plan is usually faster at scale, and you might see more "known after apply" references when using data lookups.

In my change, I was suggesting forwarding the zone_id so that you don't have to do the data lookup since you don't need it. But it's just an opinion.

antonbabenko commented 3 months ago

This issue has been resolved in version 5.2.0 :tada:

github-actions[bot] commented 2 months ago

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.