From c1495f0b700cedc04b556844397048ee41a7d891 Mon Sep 17 00:00:00 2001 From: Robert Koch Date: Thu, 14 Jul 2022 10:07:02 +0800 Subject: [PATCH] fix(appsync): domain name api association fails when domain name creation is in the same stack (#20173) Fixes #18395 and other issues where defining domain name in `aws-appsync-alpha` will fail because domain name is not created before the domain name association. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../@aws-cdk/aws-appsync/lib/graphqlapi.ts | 6 ++- .../aws-appsync/test/appsync-domain.test.ts | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts diff --git a/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts b/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts index 3dab9518ed3df..ab740aac4cfb2 100644 --- a/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts +++ b/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts @@ -494,16 +494,18 @@ export class GraphqlApi extends GraphqlApiBase { this.schemaResource = this.schema.bind(this); if (props.domainName) { - new CfnDomainName(this, 'DomainName', { + const domainName = new CfnDomainName(this, 'DomainName', { domainName: props.domainName.domainName, certificateArn: props.domainName.certificate.certificateArn, description: `domain for ${this.name} at ${this.graphqlUrl}`, }); - new CfnDomainNameApiAssociation(this, 'DomainAssociation', { + const domainNameAssociation = new CfnDomainNameApiAssociation(this, 'DomainAssociation', { domainName: props.domainName.domainName, apiId: this.apiId, }); + + domainNameAssociation.addDependsOn(domainName); } if (modes.some((mode) => mode.authorizationType === AuthorizationType.API_KEY)) { diff --git a/packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts b/packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts new file mode 100644 index 0000000000000..6e445085e4264 --- /dev/null +++ b/packages/@aws-cdk/aws-appsync/test/appsync-domain.test.ts @@ -0,0 +1,43 @@ +import * as path from 'path'; +import { Template } from '@aws-cdk/assertions'; +import * as acm from '@aws-cdk/aws-certificatemanager'; +import { Certificate } from '@aws-cdk/aws-certificatemanager'; +import * as cdk from '@aws-cdk/core'; +import * as appsync from '../lib'; + +// GLOBAL GIVEN +let stack: cdk.Stack; +let certificate: acm.Certificate; + +beforeEach(() => { + stack = new cdk.Stack(); + certificate = new Certificate(stack, 'certificate', { + domainName: 'aws.amazon.com', + }); +}); + +describe('Tests of AppSync Domain Name', () => { + test('DomainNameAssociation depends on DomainName construct', () => { + new appsync.GraphqlApi(stack, 'baseApi', { + name: 'api', + schema: appsync.Schema.fromAsset( + path.join(__dirname, 'appsync.test.graphql'), + ), + domainName: { + certificate, + domainName: 'aws.amazon.com', + }, + }); + + const domainName = Template.fromStack(stack).findResources( + 'AWS::AppSync::DomainName', + ); + + Template.fromStack(stack).hasResource( + 'AWS::AppSync::DomainNameApiAssociation', + { + DependsOn: [Object.keys(domainName)[0]], + }, + ); + }); +});