Skip to content

Commit 8d76778

Browse files
authored
feat(amplify): support custom certificate (#30791)
### Issue # (if applicable) Closes #30594. ### Reason for this change To use custom domain for Amplify by setting custom certificate. ### Description of changes Add `customCertificate` property. ### Description of how you validated changes Add unit test and integ test. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 768145c commit 8d76778

13 files changed

+1062
-0
lines changed

packages/@aws-cdk/aws-amplify-alpha/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ domain.mapSubDomain(main, 'www');
138138
domain.mapSubDomain(dev); // sub domain prefix defaults to branch name
139139
```
140140

141+
To specify a custom certificate for your custom domain use the `customCertificate` property:
142+
143+
```ts
144+
declare const customCertificate: acm.Certificate;
145+
declare const amplifyApp: amplify.App;
146+
147+
const domain = amplifyApp.addDomain('example.com', {
148+
customCertificate, // set your custom certificate
149+
});
150+
```
151+
141152
## Restricting access
142153

143154
Password protect the app with basic auth by specifying the `basicAuth` prop.

packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
12
import * as iam from 'aws-cdk-lib/aws-iam';
23
import { Lazy, Resource, IResolvable } from 'aws-cdk-lib/core';
34
import { Construct } from 'constructs';
@@ -36,6 +37,13 @@ export interface DomainOptions {
3637
* @default - all repository branches ['*', 'pr*']
3738
*/
3839
readonly autoSubdomainCreationPatterns?: string[];
40+
41+
/**
42+
* The type of SSL/TLS certificate to use for your custom domain
43+
*
44+
* @default - Amplify uses the default certificate that it provisions and manages for you
45+
*/
46+
readonly customCertificate?: acm.ICertificate;
3947
}
4048

4149
/**
@@ -130,6 +138,10 @@ export class Domain extends Resource {
130138
enableAutoSubDomain: !!props.enableAutoSubdomain,
131139
autoSubDomainCreationPatterns: props.autoSubdomainCreationPatterns || ['*', 'pr*'],
132140
autoSubDomainIamRole: props.autoSubDomainIamRole?.roleArn,
141+
certificateSettings: props.customCertificate ? {
142+
certificateType: 'CUSTOM',
143+
customCertificateArn: props.customCertificate.certificateArn,
144+
} : undefined,
133145
});
134146

135147
this.arn = domain.attrArn;

packages/@aws-cdk/aws-amplify-alpha/rosetta/default.ts-fixture

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { SecretValue, Stack } from 'aws-cdk-lib';
33
import { Construct } from 'constructs';
44
import * as amplify from '@aws-cdk/aws-amplify-alpha';
5+
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
56

67
class Fixture extends Stack {
78
constructor(scope: Construct, id: string) {

packages/@aws-cdk/aws-amplify-alpha/test/domain.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Template } from 'aws-cdk-lib/assertions';
2+
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
23
import * as iam from 'aws-cdk-lib/aws-iam';
34
import { App, SecretValue, Stack } from 'aws-cdk-lib';
45
import * as amplify from '../lib';
@@ -64,6 +65,78 @@ test('create a domain', () => {
6465
});
6566
});
6667

68+
test('create a domain with custom certificate', () => {
69+
// GIVEN
70+
const stack = new Stack();
71+
const app = new amplify.App(stack, 'App', {
72+
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
73+
owner: 'aws',
74+
repository: 'aws-cdk',
75+
oauthToken: SecretValue.unsafePlainText('secret'),
76+
}),
77+
});
78+
const prodBranch = app.addBranch('main');
79+
const devBranch = app.addBranch('dev');
80+
81+
const customCertificate = new acm.Certificate(stack, 'Cert', {
82+
domainName: '*.example.com',
83+
});
84+
85+
// WHEN
86+
const domain = app.addDomain('example.com', {
87+
subDomains: [
88+
{
89+
branch: prodBranch,
90+
prefix: 'prod',
91+
},
92+
],
93+
customCertificate,
94+
});
95+
domain.mapSubDomain(devBranch);
96+
97+
// THEN
98+
Template.fromStack(stack).hasResourceProperties('AWS::Amplify::Domain', {
99+
AppId: {
100+
'Fn::GetAtt': [
101+
'AppF1B96344',
102+
'AppId',
103+
],
104+
},
105+
DomainName: 'example.com',
106+
CertificateSettings: {
107+
CertificateType: 'CUSTOM',
108+
CustomCertificateArn: {
109+
Ref: 'Cert5C9FAEC1',
110+
},
111+
},
112+
SubDomainSettings: [
113+
{
114+
BranchName: {
115+
'Fn::GetAtt': [
116+
'AppmainF505BAED',
117+
'BranchName',
118+
],
119+
},
120+
Prefix: 'prod',
121+
},
122+
{
123+
BranchName: {
124+
'Fn::GetAtt': [
125+
'AppdevB328DAFC',
126+
'BranchName',
127+
],
128+
},
129+
Prefix: {
130+
'Fn::GetAtt': [
131+
'AppdevB328DAFC',
132+
'BranchName',
133+
],
134+
},
135+
},
136+
],
137+
});
138+
});
139+
67140
test('map a branch to the domain root', () => {
68141
// GIVEN
69142
const stack = new Stack();

packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/amplifyappcustomdomainintegDefaultTestDeployAssert5F8CD1EB.assets.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/amplifyappcustomdomainintegDefaultTestDeployAssert5F8CD1EB.template.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk-amplify-app-custom-domain.assets.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)