aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.71k stars 3.94k forks source link

Appsync: custom domains without Route53 #20023

Open romor opened 2 years ago

romor commented 2 years ago

Describe the bug

Trying to add a custom domain name to AppSync fails during cdk deploment with error Resource of type 'AWS::AppSync::DomainNameApiAssociation' with identifier 'null' was not found.

This might be related to the fact that the Route53 service is not used, as the DNS is hosted outside of AWS.

Expected Behavior

Deployment of cdk stack works and custom domain name is mapped to the AppSync API.

Current Behavior

During cdk deploy the following error appears:

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

Reproduction Steps

This is the TypeScript code used in the 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",
          },
          ...
        });
      }
    }

APPSYNC_CERT_ARN refers to the certificate's ARN in us-east-1 region (while AppSync resides in eu-central-1).

As I do not use Route53 service but host the DNS myself, there is no further code for Route53 configuration. Of course, the DNS entry exists and points to the AWS appsync URL xxx.appsync-api.eu-central-1.amazonaws.com.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.19.0

Framework Version

No response

Node.js Version

v14.19.1

OS

Debian Linux 11.3

Language

Typescript

Language Version

No response

Other information

No response

smeyffret commented 2 years ago

I'm having the same error, despite using Route53.

cdills commented 2 years ago

Looks like the alpha construct either fails to create the AWS::AppSync::DomainName or a dependsOn is missing from the AWS::AppSync::DomainNameApiAssociation resource.

I was able to get this working by using the Cfn constructs for the domain name and association:

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",
  }
);
abury commented 2 years ago

Looks like the alpha construct either fails to create the AWS::AppSync::DomainName or a dependsOn is missing from the AWS::AppSync::DomainNameApiAssociation resource.

I was able to get this working by using the Cfn constructs for the domain name and association:

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",
  }
);

@cdills hmm, I've tried your approach and I'm still seeing the same error. The only difference is I'm creating the cert inline, but I wouldn't have thought that would cause any problems.

Edit: apparently that's exactly what caused the problem,. The certificate can't be created in the same run. When I commented out the association and built the certificate, and then uncommented the association the next run, it worked perfectly.