Skip to content

Commit b51723c

Browse files
authored
feat(acm): validated certificate can use existing Role (#3785)
Allow specifying an existing Role for the Validated Certificate, to remove the dependency on `iam:CreateRole`. Fixes #3519, but needs implementation of #3753 for maximum usefulness.
1 parent b6f055a commit b51723c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

packages/@aws-cdk/aws-certificatemanager/lib/dns-validated-certificate.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ export interface DnsValidatedCertificateProps extends CertificateProps {
2323
* @default the region the stack is deployed in.
2424
*/
2525
readonly region?: string;
26+
27+
/**
28+
* Role to use for the custom resource that creates the validated certificate
29+
*
30+
* @default - A new role will be created
31+
*/
32+
readonly customResourceRole?: iam.IRole;
2633
}
2734

2835
/**
@@ -55,7 +62,8 @@ export class DnsValidatedCertificate extends cdk.Resource implements ICertificat
5562
code: lambda.Code.fromAsset(path.resolve(__dirname, '..', 'lambda-packages', 'dns_validated_certificate_handler', 'lib')),
5663
handler: 'index.certificateRequestHandler',
5764
runtime: lambda.Runtime.NODEJS_8_10,
58-
timeout: cdk.Duration.minutes(15)
65+
timeout: cdk.Duration.minutes(15),
66+
role: props.customResourceRole
5967
});
6068
requestorFunction.addToRolePolicy(new iam.PolicyStatement({
6169
actions: ['acm:RequestCertificate', 'acm:DescribeCertificate', 'acm:DeleteCertificate'],
@@ -74,7 +82,7 @@ export class DnsValidatedCertificate extends cdk.Resource implements ICertificat
7482
provider: cfn.CustomResourceProvider.lambda(requestorFunction),
7583
properties: {
7684
DomainName: props.domainName,
77-
SubjectAlternativeNames: cdk.Lazy.listValue({ produce: () => props.subjectAlternativeNames}, { omitEmpty: true}),
85+
SubjectAlternativeNames: cdk.Lazy.listValue({ produce: () => props.subjectAlternativeNames }, { omitEmpty: true }),
7886
HostedZoneId: this.hostedZoneId,
7987
Region: props.region,
8088
}

packages/@aws-cdk/aws-certificatemanager/test/test.dns-validated-certificate.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, haveResource } from '@aws-cdk/assert';
2+
import iam = require('@aws-cdk/aws-iam');
23
import { HostedZone, PublicHostedZone } from '@aws-cdk/aws-route53';
34
import { App, Stack } from '@aws-cdk/core';
45
import { Test } from 'nodeunit';
@@ -154,4 +155,30 @@ export = {
154155

155156
test.done();
156157
},
158+
159+
'works with imported role'(test: Test) {
160+
// GIVEN
161+
const app = new App();
162+
const stack = new Stack(app, 'Stack', {
163+
env: { account: '12345678', region: 'us-blue-5' },
164+
});
165+
const helloDotComZone = new PublicHostedZone(stack, 'HelloDotCom', {
166+
zoneName: 'hello.com'
167+
});
168+
const role = iam.Role.fromRoleArn(stack, 'Role', 'arn:aws:iam::account-id:role/role-name');
169+
170+
// WHEN
171+
new DnsValidatedCertificate(stack, 'Cert', {
172+
domainName: 'hello.com',
173+
hostedZone: helloDotComZone,
174+
customResourceRole: role
175+
});
176+
177+
// THEN
178+
expect(stack).to(haveResource('AWS::Lambda::Function', {
179+
Role: 'arn:aws:iam::account-id:role/role-name'
180+
}));
181+
182+
test.done();
183+
},
157184
};

0 commit comments

Comments
 (0)