From c6407b5fa0e8ecb6adb3ea50496d7b7abee0926d Mon Sep 17 00:00:00 2001 From: Janario Oliveira Date: Tue, 2 Mar 2021 12:44:12 +0100 Subject: [PATCH 1/8] feat(amplify-domain): Added config for auto subdomain creation --- packages/@aws-cdk/aws-amplify/lib/domain.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/@aws-cdk/aws-amplify/lib/domain.ts b/packages/@aws-cdk/aws-amplify/lib/domain.ts index bf6ba7afe8017..f117f59730062 100644 --- a/packages/@aws-cdk/aws-amplify/lib/domain.ts +++ b/packages/@aws-cdk/aws-amplify/lib/domain.ts @@ -21,6 +21,20 @@ export interface DomainOptions { * @default - use `addSubDomain()` to add subdomains */ readonly subDomains?: SubDomain[]; + + /** + * Automatically create subdomains for connected branches + * + * @default false + */ + readonly domainEnableAutoSubDomain: boolean; + + /** + * Branches which should automatically create subdomains + * + * @default - all repository branches *, pr* + */ + readonly domainAutoSubDomainCreationPatterns?: string[]; } /** @@ -106,6 +120,8 @@ export class Domain extends Resource { appId: props.app.appId, domainName, subDomainSettings: Lazy.any({ produce: () => this.renderSubDomainSettings() }, { omitEmptyArray: true }), + enableAutoSubDomain: !!props.domainEnableAutoSubDomain, + autoSubDomainCreationPatterns: props.domainAutoSubDomainCreationPatterns, }); this.arn = domain.attrArn; From 19ac74d1034a9724a97b96bc3adf443474b6aa05 Mon Sep 17 00:00:00 2001 From: Janario Oliveira Date: Thu, 4 Mar 2021 08:47:50 +0100 Subject: [PATCH 2/8] feat(amplify-domain): Rename properties and adjusted optional. Code review suggestions --- packages/@aws-cdk/aws-amplify/lib/domain.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/@aws-cdk/aws-amplify/lib/domain.ts b/packages/@aws-cdk/aws-amplify/lib/domain.ts index f117f59730062..b666626866106 100644 --- a/packages/@aws-cdk/aws-amplify/lib/domain.ts +++ b/packages/@aws-cdk/aws-amplify/lib/domain.ts @@ -27,14 +27,14 @@ export interface DomainOptions { * * @default false */ - readonly domainEnableAutoSubDomain: boolean; + readonly enableAutoSubdomain?: boolean; /** * Branches which should automatically create subdomains * * @default - all repository branches *, pr* */ - readonly domainAutoSubDomainCreationPatterns?: string[]; + readonly autoSubdomainCreationPatterns?: string[]; } /** @@ -120,8 +120,8 @@ export class Domain extends Resource { appId: props.app.appId, domainName, subDomainSettings: Lazy.any({ produce: () => this.renderSubDomainSettings() }, { omitEmptyArray: true }), - enableAutoSubDomain: !!props.domainEnableAutoSubDomain, - autoSubDomainCreationPatterns: props.domainAutoSubDomainCreationPatterns, + enableAutoSubDomain: !!props.enableAutoSubdomain, + autoSubDomainCreationPatterns: props.autoSubdomainCreationPatterns, }); this.arn = domain.attrArn; From fa4e9369f5db268061cc6f27d98285b4c906ab9a Mon Sep 17 00:00:00 2001 From: Janario Oliveira Date: Sat, 6 Mar 2021 15:40:37 +0100 Subject: [PATCH 3/8] Added doc with `AutoSubdomain` feature --- packages/@aws-cdk/aws-amplify/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-amplify/README.md b/packages/@aws-cdk/aws-amplify/README.md index 7c15079d914d6..b8e3bd91c2306 100644 --- a/packages/@aws-cdk/aws-amplify/README.md +++ b/packages/@aws-cdk/aws-amplify/README.md @@ -122,7 +122,10 @@ mySinglePageApp.addCustomRule(amplify.CustomRule.SINGLE_PAGE_APPLICATION_REDIREC Add a domain and map sub domains to branches: ```ts -const domain = amplifyApp.addDomain('example.com'); +const domain = amplifyApp.addDomain('example.com', { + enableAutoSubdomain: true, // in case subdomains should be auto registered for branches + autoSubdomainCreationPatterns: ['*', 'pr*'], // regex for branches that should auto register subdomains +}); domain.mapRoot(master); // map master branch to domain root domain.mapSubDomain(master, 'www'); domain.mapSubDomain(dev); // sub domain prefix defaults to branch name From 1d60debf9b42451b1a6dd676cbcd845dce68150e Mon Sep 17 00:00:00 2001 From: Janario Oliveira Date: Sat, 6 Mar 2021 15:58:55 +0100 Subject: [PATCH 4/8] Added test case --- .../@aws-cdk/aws-amplify/test/domain.test.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/packages/@aws-cdk/aws-amplify/test/domain.test.ts b/packages/@aws-cdk/aws-amplify/test/domain.test.ts index ca7c211d14094..74a021c1016e8 100644 --- a/packages/@aws-cdk/aws-amplify/test/domain.test.ts +++ b/packages/@aws-cdk/aws-amplify/test/domain.test.ts @@ -120,3 +120,51 @@ test('throws at synthesis without subdomains', () => { // THEN expect(() => app.synth()).toThrow(/The domain doesn't contain any subdomains/); }); + +test('auto subdomain all branches', ()=>{ + // 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'), + }), + }); + + // WHEN + app.addDomain('amazon.com', { + enableAutoSubdomain: true, + }) + + // THEN + expect(stack).toHaveResource('AWS::Amplify::Domain', { + EnableAutoSubDomain: true, + AutoSubDomainCreationPatterns: ['*', 'pr*'], + }); +}); + +test('auto subdomain some branches', ()=>{ + // 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'), + }), + }); + + // WHEN + app.addDomain('amazon.com', { + enableAutoSubdomain: true, + autoSubdomainCreationPatterns: ['features/**'] + }) + + // THEN + expect(stack).toHaveResource('AWS::Amplify::Domain', { + EnableAutoSubDomain: true, + AutoSubDomainCreationPatterns: ['features/**'], + }); +}); + From 9ddeb48a428ec96f1d558eff0045634e57ab6ea4 Mon Sep 17 00:00:00 2001 From: Janario Oliveira Date: Sat, 6 Mar 2021 19:08:52 +0100 Subject: [PATCH 5/8] Fix lint --- packages/@aws-cdk/aws-amplify/test/domain.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-amplify/test/domain.test.ts b/packages/@aws-cdk/aws-amplify/test/domain.test.ts index 74a021c1016e8..12308eb1fac8b 100644 --- a/packages/@aws-cdk/aws-amplify/test/domain.test.ts +++ b/packages/@aws-cdk/aws-amplify/test/domain.test.ts @@ -135,7 +135,7 @@ test('auto subdomain all branches', ()=>{ // WHEN app.addDomain('amazon.com', { enableAutoSubdomain: true, - }) + }); // THEN expect(stack).toHaveResource('AWS::Amplify::Domain', { @@ -158,8 +158,8 @@ test('auto subdomain some branches', ()=>{ // WHEN app.addDomain('amazon.com', { enableAutoSubdomain: true, - autoSubdomainCreationPatterns: ['features/**'] - }) + autoSubdomainCreationPatterns: ['features/**'], + }); // THEN expect(stack).toHaveResource('AWS::Amplify::Domain', { From 3b6c1ef2d6a5bfbea605af139e5b522d328c21f3 Mon Sep 17 00:00:00 2001 From: Janario Oliveira Date: Sat, 6 Mar 2021 20:19:48 +0100 Subject: [PATCH 6/8] Fix test --- packages/@aws-cdk/aws-amplify/test/domain.test.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/@aws-cdk/aws-amplify/test/domain.test.ts b/packages/@aws-cdk/aws-amplify/test/domain.test.ts index 12308eb1fac8b..2a4f5cc112ba8 100644 --- a/packages/@aws-cdk/aws-amplify/test/domain.test.ts +++ b/packages/@aws-cdk/aws-amplify/test/domain.test.ts @@ -121,7 +121,7 @@ test('throws at synthesis without subdomains', () => { expect(() => app.synth()).toThrow(/The domain doesn't contain any subdomains/); }); -test('auto subdomain all branches', ()=>{ +test('auto subdomain all branches', () => { // GIVEN const stack = new Stack(); const app = new amplify.App(stack, 'App', { @@ -131,20 +131,21 @@ test('auto subdomain all branches', ()=>{ oauthToken: SecretValue.plainText('secret'), }), }); + const prodBranch = app.addBranch('master'); // WHEN - app.addDomain('amazon.com', { + const domain = app.addDomain('amazon.com', { enableAutoSubdomain: true, }); + domain.mapRoot(prodBranch); // THEN expect(stack).toHaveResource('AWS::Amplify::Domain', { EnableAutoSubDomain: true, - AutoSubDomainCreationPatterns: ['*', 'pr*'], }); }); -test('auto subdomain some branches', ()=>{ +test('auto subdomain some branches', () => { // GIVEN const stack = new Stack(); const app = new amplify.App(stack, 'App', { @@ -154,12 +155,14 @@ test('auto subdomain some branches', ()=>{ oauthToken: SecretValue.plainText('secret'), }), }); + const prodBranch = app.addBranch('master'); // WHEN - app.addDomain('amazon.com', { + const domain = app.addDomain('amazon.com', { enableAutoSubdomain: true, autoSubdomainCreationPatterns: ['features/**'], }); + domain.mapRoot(prodBranch); // THEN expect(stack).toHaveResource('AWS::Amplify::Domain', { From 3920c7b518a080f7a5416a0b7e806e4826ce3a43 Mon Sep 17 00:00:00 2001 From: Janario Oliveira Date: Sun, 7 Mar 2021 00:53:09 +0100 Subject: [PATCH 7/8] Propagated autoSubDomainIamRole from App --- packages/@aws-cdk/aws-amplify/lib/app.ts | 1 + packages/@aws-cdk/aws-amplify/lib/domain.ts | 8 +++ .../@aws-cdk/aws-amplify/test/domain.test.ts | 56 +++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/packages/@aws-cdk/aws-amplify/lib/app.ts b/packages/@aws-cdk/aws-amplify/lib/app.ts index bf1d5bc3d5e89..43f8e308cb8f9 100644 --- a/packages/@aws-cdk/aws-amplify/lib/app.ts +++ b/packages/@aws-cdk/aws-amplify/lib/app.ts @@ -289,6 +289,7 @@ export class App extends Resource implements IApp, iam.IGrantable { return new Domain(this, id, { ...options, app: this, + autoSubDomainIamRole: this.grantPrincipal as iam.IRole, }); } } diff --git a/packages/@aws-cdk/aws-amplify/lib/domain.ts b/packages/@aws-cdk/aws-amplify/lib/domain.ts index b666626866106..055f01e08a843 100644 --- a/packages/@aws-cdk/aws-amplify/lib/domain.ts +++ b/packages/@aws-cdk/aws-amplify/lib/domain.ts @@ -1,3 +1,4 @@ +import * as iam from '@aws-cdk/aws-iam'; import { Lazy, Resource, IResolvable } from '@aws-cdk/core'; import { Construct } from 'constructs'; import { CfnDomain } from './amplify.generated'; @@ -45,6 +46,12 @@ export interface DomainProps extends DomainOptions { * The application to which the domain must be connected */ readonly app: IApp; + + /** + * The IAM role with access to Route53 when using enableAutoSubdomain + * @default the IAM role from App.grantPrincipal + */ + readonly autoSubDomainIamRole?: iam.IRole; } /** @@ -122,6 +129,7 @@ export class Domain extends Resource { subDomainSettings: Lazy.any({ produce: () => this.renderSubDomainSettings() }, { omitEmptyArray: true }), enableAutoSubDomain: !!props.enableAutoSubdomain, autoSubDomainCreationPatterns: props.autoSubdomainCreationPatterns, + autoSubDomainIamRole: props.autoSubDomainIamRole?.roleArn, }); this.arn = domain.attrArn; diff --git a/packages/@aws-cdk/aws-amplify/test/domain.test.ts b/packages/@aws-cdk/aws-amplify/test/domain.test.ts index 2a4f5cc112ba8..a0c4e78e6218a 100644 --- a/packages/@aws-cdk/aws-amplify/test/domain.test.ts +++ b/packages/@aws-cdk/aws-amplify/test/domain.test.ts @@ -1,3 +1,4 @@ +import * as iam from '@aws-cdk/aws-iam'; import '@aws-cdk/assert/jest'; import { App, SecretValue, Stack } from '@aws-cdk/core'; import * as amplify from '../lib'; @@ -142,6 +143,12 @@ test('auto subdomain all branches', () => { // THEN expect(stack).toHaveResource('AWS::Amplify::Domain', { EnableAutoSubDomain: true, + AutoSubDomainIAMRole: { + 'Fn::GetAtt': [ + 'AppRole1AF9B530', + 'Arn', + ], + }, }); }); @@ -168,6 +175,55 @@ test('auto subdomain some branches', () => { expect(stack).toHaveResource('AWS::Amplify::Domain', { EnableAutoSubDomain: true, AutoSubDomainCreationPatterns: ['features/**'], + AutoSubDomainIAMRole: { + 'Fn::GetAtt': [ + 'AppRole1AF9B530', + 'Arn', + ], + }, }); }); +test('auto subdomain with IAM role', () => { + // 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'), + }), + role: iam.Role.fromRoleArn( + stack, + 'AmplifyRole', + `arn:aws:iam::${Stack.of(stack).account}:role/AmplifyRole`, + { mutable: false }, + ), + }); + const prodBranch = app.addBranch('master'); + + // WHEN + const domain = app.addDomain('amazon.com', { + enableAutoSubdomain: true, + autoSubdomainCreationPatterns: ['features/**'], + }); + domain.mapRoot(prodBranch); + + // THEN + expect(stack).toHaveResource('AWS::Amplify::Domain', { + EnableAutoSubDomain: true, + AutoSubDomainCreationPatterns: ['features/**'], + AutoSubDomainIAMRole: { + 'Fn::Join': [ + '', + [ + 'arn:aws:iam::', + { + Ref: 'AWS::AccountId', + }, + ':role/AmplifyRole', + ], + ], + }, + }); +}); \ No newline at end of file From d9a95407bb6221f1cb03a3d8745fc07be89a5576 Mon Sep 17 00:00:00 2001 From: Janario Oliveira Date: Sun, 7 Mar 2021 01:13:54 +0100 Subject: [PATCH 8/8] Added default AutoSubDomainCreationPatterns --- packages/@aws-cdk/aws-amplify/lib/domain.ts | 4 ++-- packages/@aws-cdk/aws-amplify/test/domain.test.ts | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-amplify/lib/domain.ts b/packages/@aws-cdk/aws-amplify/lib/domain.ts index 055f01e08a843..f8683f44b123d 100644 --- a/packages/@aws-cdk/aws-amplify/lib/domain.ts +++ b/packages/@aws-cdk/aws-amplify/lib/domain.ts @@ -33,7 +33,7 @@ export interface DomainOptions { /** * Branches which should automatically create subdomains * - * @default - all repository branches *, pr* + * @default - all repository branches ['*', 'pr*'] */ readonly autoSubdomainCreationPatterns?: string[]; } @@ -128,7 +128,7 @@ export class Domain extends Resource { domainName, subDomainSettings: Lazy.any({ produce: () => this.renderSubDomainSettings() }, { omitEmptyArray: true }), enableAutoSubDomain: !!props.enableAutoSubdomain, - autoSubDomainCreationPatterns: props.autoSubdomainCreationPatterns, + autoSubDomainCreationPatterns: props.autoSubdomainCreationPatterns || ['*', 'pr*'], autoSubDomainIamRole: props.autoSubDomainIamRole?.roleArn, }); diff --git a/packages/@aws-cdk/aws-amplify/test/domain.test.ts b/packages/@aws-cdk/aws-amplify/test/domain.test.ts index a0c4e78e6218a..7b0f28f75837d 100644 --- a/packages/@aws-cdk/aws-amplify/test/domain.test.ts +++ b/packages/@aws-cdk/aws-amplify/test/domain.test.ts @@ -143,6 +143,10 @@ test('auto subdomain all branches', () => { // THEN expect(stack).toHaveResource('AWS::Amplify::Domain', { EnableAutoSubDomain: true, + AutoSubDomainCreationPatterns: [ + '*', + 'pr*', + ], AutoSubDomainIAMRole: { 'Fn::GetAtt': [ 'AppRole1AF9B530',