'Custom domain name with AWS AppSync using CDK v2

I try to add a custom domain name to my AWS AppSync API using the AWS CDK (v2).

First, I manually added a certificate for my domain. I did this in the us-east-1 region (while my API is hosted in eu-central-1) as this seems to be necessary. APPSYNC_CERT_ARN refers to this certificate's ARN.

This is the TypeScript code I have in my cdk stack:

import * as cdk from "aws-cdk-lib";
import * as appsync from "@aws-cdk/aws-appsync-alpha";
const APPSYNC_CERT_ARN = "arn:aws:acm:us-east-1:xxxx:certificate/xxxx";

export class ApiStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const certificate = cdk.aws_certificatemanager.Certificate.fromCertificateArn(
      this,
      "cert",
      APPSYNC_CERT_ARN,
    );
    const api = new appsync.GraphqlApi(this, "Api", {
      name: "my-api",
      domainName: {
        certificate,
        domainName: "my.domain.com",
      },
      ...
    });
  }
}

However, as I add the domainName member, I get the following error during cdk deploy:

Resource of type 'AWS::AppSync::DomainNameApiAssociation' with identifier 'null' was not found.

The feature to add custom AppSync domains via cdk is rather new, so I did not find any hints on what I do wrong. Any help is appreciated.



Solution 1:[1]

In order to create an AppsyncDomainNameApiAssociation (which is the underlying cloudformation resource created by the CDK construct you are using) you have to create both the GraphqlAPI and an AppsyncDomainName prior to creating the association. Although the docs don't really reflect this relationship you can read about "AWS::AppSync::DomainName" here.

Including the "domainName" object in your GraphqlApi instantiation creates the association resources but fails to create the actual domain name resources. You'll need to create it before hand by using the L1 construct for CFNDomainName then manually create the association using the L1 construct CfnDomainNameApiAssociation.

The constructs aren't included in the aws_appsync_alpha library. You'll need to import them from aws_appsync and use them like this:

const certificate = cdk.aws_certificatemanager.Certificate.fromCertificateArn(
  this,
  "cert",
  APPSYNC_CERT_ARN,
);Ï

const appsyncDomainName = new aws_appsync.CfnDomainName(
  this,
  'AppsyncDomainName',
  {
    certificateArn: certificate.certificateArn,
    domainName: "my.domain.com",
  }
);


const api = new appsync.GraphqlApi(this, "Api", {
  name: "my-api",
  // Omit the domainName object
  ...
});

const assoc = new aws_appsync.CfnDomainNameApiAssociation(
  this,
  'MyCfnDomainNameApiAssociation',
  {
    apiId: api.apiId,
    domainName: "my.domain.com",
  }
);

//  Required to ensure the resources are created in order
assoc.addDependsOn(appsyncDomainName);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cody DIlls