Skip to content

(apigatewayv2): ApiMapping does not add a dependency on DomainName #15464

@misterjoshua

Description

@misterjoshua

ApiMapping does not declare a dependency on the DomainName it maps to an API. So, when both are created in the same stack, CloudFormation may fail to create the mapping with an error like this:

Invalid domain name identifier specified (Service: AmazonApiGatewayV2; Status Code: 404; Error Code: NotFoundException; Re quest ID: <removed>; Proxy: null)

Reproduction Steps

import * as apigatewayv2 from '@aws-cdk/aws-apigatewayv2';
import * as apigatewayv2_integration from '@aws-cdk/aws-apigatewayv2-integrations';
import * as acm from '@aws-cdk/aws-certificatemanager';
import * as cdk from '@aws-cdk/core';

const BUG_DOMAIN_NAME = 'yourdomain.com';
const BUG_WILDCARD_CERT_ARN = 'arn:aws:acm:ca-central-1:1111111111111111:certificate/x';

class BugStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props: cdk.StackProps = {}) {
    super(scope, id, props);

    const api = new apigatewayv2.HttpApi(this, 'Api', {
      apiName: cdk.Stack.of(this).stackName,
      defaultIntegration: new apigatewayv2_integration.HttpProxyIntegration({
        url: 'https://www.example.com',
        method: apigatewayv2.HttpMethod.GET,
      }),
    });

    // Cloudformation races, so this increases odds that a mapping will beat the domain name
    for (let i = 0; i < 10; i++) {
      const domainName = new apigatewayv2.DomainName(this, `DomainName${i}`, {
        domainName: `bug${i}.${BUG_DOMAIN_NAME}`,
        certificate: acm.Certificate.fromCertificateArn(this, `Certificate${i}`, BUG_WILDCARD_CERT_ARN),
      });

      new apigatewayv2.ApiMapping(this, `ApiMapping${i}`, {
        api,
        domainName,
      });
    }
  }
}

const app = new cdk.App();
new BugStack(app, 'BugStack');

app.synth();

What did you expect to happen?

I expected the stack to deploy.

What actually happened?

The stack failed to deploy with an error:

Invalid domain name identifier specified (Service: AmazonApiGatewayV2; Status Code: 404; Error Code: NotFoundException; Re quest ID: <removed>; Proxy: null)

Environment

  • CDK CLI Version : 1.111.0
  • Framework Version: 1.111.0
  • Node.js Version: v14.15.4
  • OS : Windows
  • Language (Version): Typescript 3.9.7

Other

I can work around this by manually adding a dependency between the ApiMapping and the DomainName, but I would have expected the L2 constructs to do this for me.


This is 🐛 Bug Report

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-apigatewayv2Related to Amazon API Gateway v2bugThis issue is a bug.effort/smallSmall work item – less than a day of effortp2

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions