diff --git a/packages/@aws-cdk/aws-apigateway/lib/restapi.ts b/packages/@aws-cdk/aws-apigateway/lib/restapi.ts index 25cf73991db69..a66bba21fbd22 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/restapi.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/restapi.ts @@ -212,13 +212,8 @@ export class RestApi extends Resource implements IRestApi { */ public deploymentStage!: Stage; - /** - * The domain name mapped to this API, if defined through the `domainName` - * configuration prop. - */ - public readonly domainName?: DomainName; - private readonly methods = new Array(); + private _domainName?: DomainName; private _latestDeployment: Deployment | undefined; constructor(scope: Construct, id: string, props: RestApiProps = { }) { @@ -253,10 +248,18 @@ export class RestApi extends Resource implements IRestApi { this.restApiRootResourceId = resource.attrRootResourceId; if (props.domainName) { - this.domainName = this.addDomainName('CustomDomain', props.domainName); + this.addDomainName('CustomDomain', props.domainName); } } + /** + * The first domain name mapped to this API, if defined through the `domainName` + * configuration prop, or added via `addDomainName` + */ + public get domainName() { + return this._domainName; + } + /** * API Gateway deployment that represents the latest changes of the API. * This resource will be automatically updated every time the REST API model changes. @@ -292,10 +295,14 @@ export class RestApi extends Resource implements IRestApi { * @param options custom domain options */ public addDomainName(id: string, options: DomainNameOptions): DomainName { - return new DomainName(this, id, { + const domainName = new DomainName(this, id, { ...options, mapping: this }); + if (!this._domainName) { + this._domainName = domainName; + } + return domainName; } /** diff --git a/packages/@aws-cdk/aws-apigateway/test/test.domains.ts b/packages/@aws-cdk/aws-apigateway/test/test.domains.ts index 3591cf9558717..a285b472f3dd9 100644 --- a/packages/@aws-cdk/aws-apigateway/test/test.domains.ts +++ b/packages/@aws-cdk/aws-apigateway/test/test.domains.ts @@ -177,6 +177,112 @@ export = { } })); + test.done(); + }, + + 'a domain name can be added later'(test: Test) { + // GIVEN + const domainName = 'my.domain.com'; + const stack = new Stack(); + const certificate = new acm.Certificate(stack, 'cert', { domainName: 'my.domain.com' }); + + // WHEN + const api = new apigw.RestApi(stack, 'api', {}); + + api.root.addMethod('GET'); + + api.addDomainName('domainId', { domainName, certificate }); + + // THEN + expect(stack).to(haveResource('AWS::ApiGateway::DomainName', { + "DomainName": domainName, + "EndpointConfiguration": { + "Types": [ + "REGIONAL" + ] + }, + "RegionalCertificateArn": { + "Ref": "cert56CA94EB" + } + })); + expect(stack).to(haveResource('AWS::ApiGateway::BasePathMapping', { + "DomainName": { + "Ref": "apidomainId102F8DAA" + }, + "RestApiId": { + "Ref": "apiC8550315" + }, + "Stage": { + "Ref": "apiDeploymentStageprod896C8101" + } + })); + + test.done(); + }, + + 'multiple domain names can be added'(test: Test) { + // GIVEN + const domainName = 'my.domain.com'; + const stack = new Stack(); + const certificate = new acm.Certificate(stack, 'cert', { domainName: 'my.domain.com' }); + + // WHEN + const api = new apigw.RestApi(stack, 'api', {}); + + api.root.addMethod('GET'); + + const domainName1 = api.addDomainName('domainId', { domainName, certificate }); + api.addDomainName('domainId1', { domainName: 'your.domain.com', certificate }); + api.addDomainName('domainId2', { domainName: 'our.domain.com', certificate }); + + test.deepEqual(api.domainName, domainName1); + + // THEN + expect(stack).to(haveResource('AWS::ApiGateway::DomainName', { + "DomainName": 'my.domain.com', + "EndpointConfiguration": { + "Types": [ + "REGIONAL" + ] + }, + "RegionalCertificateArn": { + "Ref": "cert56CA94EB" + } + })); + expect(stack).to(haveResource('AWS::ApiGateway::DomainName', { + "DomainName": 'your.domain.com', + "EndpointConfiguration": { + "Types": [ + "REGIONAL" + ] + }, + "RegionalCertificateArn": { + "Ref": "cert56CA94EB" + } + })); + expect(stack).to(haveResource('AWS::ApiGateway::DomainName', { + "DomainName": 'our.domain.com', + "EndpointConfiguration": { + "Types": [ + "REGIONAL" + ] + }, + "RegionalCertificateArn": { + "Ref": "cert56CA94EB" + } + })); + expect(stack).to(haveResource('AWS::ApiGateway::BasePathMapping', { + "DomainName": { + "Ref": "apidomainId102F8DAA" + }, + "RestApiId": { + "Ref": "apiC8550315" + }, + "Stage": { + "Ref": "apiDeploymentStageprod896C8101" + } + })); + test.done(); } };