hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.74k stars 9.09k forks source link

[Bug]: Creating an API Mapping with Existing Domain #27358

Open justiceamoh opened 1 year ago

justiceamoh commented 1 year ago

Terraform Core Version

1.3.2

AWS Provider Version

4.34.0

Affected Resource(s)

aws_apigatewayv2_domain_name aws_apigatewayv2_api_mapping

Expected Behavior

I'm trying to create api gateway endpoints for multiple lambda functions, all using the same base custom domain name: api.example.io. One function is to be mapped to api.example.io/project , and another to api.example.io/report. I should be able to create the custom domain using aws_apigatewayv2_domain_name in one function, then use aws_apigatewayv2_api_mapping separately in each function to map to different api_mapping_keys.

Actual Behavior

aws_apigatewayv2_domain_name throws an error when the domain is already created (error output below). 🚨Furthermore, it clears out all existing custom domain configurations in API Gateway Console:

CleanShot 2022-10-19 at 16 37 04

When I try to add an existing domain in aws_apigatewayv2_api_mapping, its domain_name argument does not accept the string of an existing domain or its arn. It throws the error that the domain name identifier is invalid (see below)

Relevant Error/Panic Output Snippet

----------------------------------------------------------------------------
From aws_apigatewayv2_domain_name
----------------------------------------------------------------------------
│ Error: error creating API Gateway v2 domain name (api.example.io): BadRequestException: The domain name you provided already exists.
│
│   with module.api_gateway.aws_apigatewayv2_domain_name.apigw_domain,
│   on ../../../terraform-modules/apigw_lambda/main.tf line 29, in resource "aws_apigatewayv2_domain_name" "apigw_domain":
│   29: resource "aws_apigatewayv2_domain_name" "apigw_domain" {

----------------------------------------------------------------------------
From aws_apigatewayv2_api_mapping
----------------------------------------------------------------------------
â•·
│ Error: creating API Gateway v2 API mapping: NotFoundException: Invalid domain name identifier specified
│ {
│   RespMetadata: {
│     StatusCode: 404,
│     RequestID: "41ccd5de-73a4-4f31-99e6-a84b5748828d"
│   },
│   Message_: "Invalid domain name identifier specified"
│ }
│ 
│   with module.api_gateway.aws_apigatewayv2_api_mapping.apigw_mapping,
│   on ../../../terraform-modules/apigw_lambda/main.tf line 38, in resource "aws_apigatewayv2_api_mapping" "apigw_mapping":
│   38: resource "aws_apigatewayv2_api_mapping" "apigw_mapping" {
│ 
╵

Terraform Configuration Files

data "aws_acm_certificate" "api_acm_cert" {
    domain   = var.domain_name
    statuses = ["ISSUED"]
}

resource "aws_apigatewayv2_domain_name" "apigw_domain" {
  domain_name = var.domain_name
  domain_name_configuration {
    certificate_arn = data.aws_acm_certificate.api_acm_cert.arn
    endpoint_type   = "REGIONAL"
    security_policy = "TLS_1_2"
  }
}

resource "aws_apigatewayv2_api_mapping" "apigw_mapping" {
  api_id      = aws_apigatewayv2_api.lambda_fxn_api.id
  domain_name = aws_apigatewayv2_domain_name.apigw_domain.domain_name
  stage       = var.stage
  api_mapping_key = var.mapping_key
}

Steps to Reproduce

  1. Create first lambda function with custom domain api gateway mapping as above
  2. Create second lambda function with same custom domain but different api mapping key.

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

No response

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue