diff --git a/packages/@aws-cdk/aws-amplify/README.md b/packages/@aws-cdk/aws-amplify/README.md index af289b7054fda..9c1b33b01d44a 100644 --- a/packages/@aws-cdk/aws-amplify/README.md +++ b/packages/@aws-cdk/aws-amplify/README.md @@ -86,6 +86,7 @@ amplifyApp.addCustomRule({ Add a domain and map sub domains to branches: ```ts const domain = amplifyApp.addDomain('example.com'); +domain.mapRoot(master); // map master branch to domain root domain.mapSubDomain(master, 'www'); domain.mapSubDomain(dev); // sub domain prefix defaults to branch name ``` diff --git a/packages/@aws-cdk/aws-amplify/lib/domain.ts b/packages/@aws-cdk/aws-amplify/lib/domain.ts index 1b2d26a43d3b0..ddb5b6b6f04e8 100644 --- a/packages/@aws-cdk/aws-amplify/lib/domain.ts +++ b/packages/@aws-cdk/aws-amplify/lib/domain.ts @@ -95,12 +95,22 @@ export class Domain extends Resource { /** * Maps a branch to a sub domain + * + * @param branch The branch + * @param prefix The prefix. Use '' to map to the root of the domain. Defaults to branch name. */ public mapSubDomain(branch: IBranch, prefix?: string) { this.subDomains.push({ branch, prefix }); return this; } + /** + * Maps a branch to the domain root + */ + public mapRoot(branch: IBranch) { + return this.mapSubDomain(branch, ''); + } + protected validate() { if (this.subDomains.length === 0) { return ['The domain doesn\'t contain any subdomains']; @@ -112,7 +122,7 @@ export class Domain extends Resource { private renderSubDomainSettings() { return this.subDomains.map(s => ({ branchName: s.branch.branchName, - prefix: s.prefix || s.branch.branchName, + prefix: s.prefix === undefined ? s.branch.branchName : s.prefix, })); } } @@ -127,7 +137,7 @@ export interface SubDomain { readonly branch: IBranch; /** - * The prefix + * The prefix. Use '' to map to the root of the domain * * @default - the branch name */ diff --git a/packages/@aws-cdk/aws-amplify/test/domain.test.ts b/packages/@aws-cdk/aws-amplify/test/domain.test.ts index 8394c51a432d1..ca7c211d14094 100644 --- a/packages/@aws-cdk/aws-amplify/test/domain.test.ts +++ b/packages/@aws-cdk/aws-amplify/test/domain.test.ts @@ -63,6 +63,45 @@ test('create a domain', () => { }); }); +test('map a branch to the domain root', () => { + // GIVEN + const stack = new Stack(); + const app = new amplify.App(stack, 'App', { + sourceCodeProvider: new amplify.GitHubSourceCodeProvider({ + owner: 'aws', + repository: 'aws-cdk', + oauthToken: SecretValue.plainText('secret'), + }), + }); + const prodBranch = app.addBranch('master'); + + // WHEN + const domain = app.addDomain('amazon.com'); + domain.mapRoot(prodBranch); + + // THEN + expect(stack).toHaveResource('AWS::Amplify::Domain', { + AppId: { + 'Fn::GetAtt': [ + 'AppF1B96344', + 'AppId', + ], + }, + DomainName: 'amazon.com', + SubDomainSettings: [ + { + BranchName: { + 'Fn::GetAtt': [ + 'Appmaster71597E87', + 'BranchName', + ], + }, + Prefix: '', + }, + ], + }); +}); + test('throws at synthesis without subdomains', () => { // GIVEN const app = new App();