Skip to content

Commit 1a7d4db

Browse files
CaerusKarurix0rrr
authored andcommitted
feat(acm): allow specifying region for validated certificates (#2626)
CloudFront requires certificates to be registered in the us-east-1 region, so this allows users to override the default, which places the certificates in whatever region the stack exists in.
1 parent 56f544e commit 1a7d4db

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

packages/@aws-cdk/aws-certificatemanager/lambda-packages/dns_validated_certificate_handler/lib/index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ let report = function (event, context, responseStatus, physicalResourceId, respo
7474
* @param {string} hostedZoneId the Route53 Hosted Zone ID
7575
* @returns {string} Validated certificate ARN
7676
*/
77-
const requestCertificate = async function (requestId, domainName, subjectAlternativeNames, hostedZoneId) {
77+
const requestCertificate = async function (requestId, domainName, subjectAlternativeNames, hostedZoneId, region) {
7878
const crypto = require('crypto');
79-
const acm = new aws.ACM();
79+
const acm = new aws.ACM({region});
8080
const route53 = new aws.Route53();
8181
if (waiter) {
8282
// Used by the test suite, since waiters aren't mockable yet
@@ -157,8 +157,8 @@ const requestCertificate = async function (requestId, domainName, subjectAlterna
157157
*
158158
* @param {string} arn The certificate ARN
159159
*/
160-
const deleteCertificate = async function (arn) {
161-
const acm = new aws.ACM();
160+
const deleteCertificate = async function (arn, region) {
161+
const acm = new aws.ACM({region});
162162

163163
console.log(`Deleting certificate ${arn}`);
164164

@@ -189,7 +189,8 @@ exports.certificateRequestHandler = async function (event, context) {
189189
event.RequestId,
190190
event.ResourceProperties.DomainName,
191191
event.ResourceProperties.SubjectAlternativeNames,
192-
event.ResourceProperties.HostedZoneId
192+
event.ResourceProperties.HostedZoneId,
193+
event.ResourceProperties.Region,
193194
);
194195
responseData.Arn = physicalResourceId = certificateArn;
195196
break;
@@ -198,7 +199,7 @@ exports.certificateRequestHandler = async function (event, context) {
198199
// If the resource didn't create correctly, the physical resource ID won't be the
199200
// certificate ARN, so don't try to delete it in that case.
200201
if (physicalResourceId.startsWith('arn:')) {
201-
await deleteCertificate(physicalResourceId);
202+
await deleteCertificate(physicalResourceId, event.ResourceProperties.Region);
202203
}
203204
break;
204205
default:

packages/@aws-cdk/aws-certificatemanager/lambda-packages/dns_validated_certificate_handler/test/handler.test.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ describe('DNS Validated Certificate Handler', () => {
9898
ResourceProperties: {
9999
DomainName: testDomainName,
100100
SubjectAlternativeNames: [],
101-
HostedZoneId: testHostedZoneId
101+
HostedZoneId: testHostedZoneId,
102+
Region: 'us-east-1',
102103
}
103104
})
104105
.expectResolve(() => {
@@ -138,7 +139,10 @@ describe('DNS Validated Certificate Handler', () => {
138139
.event({
139140
RequestType: 'Delete',
140141
RequestId: testRequestId,
141-
PhysicalResourceId: testCertificateArn
142+
PhysicalResourceId: testCertificateArn,
143+
ResourceProperties: {
144+
Region: 'us-east-1',
145+
}
142146
})
143147
.expectResolve(() => {
144148
sinon.assert.calledWith(deleteCertificateFake, sinon.match({
@@ -162,7 +166,10 @@ describe('DNS Validated Certificate Handler', () => {
162166
.event({
163167
RequestType: 'Delete',
164168
RequestId: testRequestId,
165-
PhysicalResourceId: testCertificateArn
169+
PhysicalResourceId: testCertificateArn,
170+
ResourceProperties: {
171+
Region: 'us-east-1',
172+
}
166173
})
167174
.expectResolve(() => {
168175
sinon.assert.calledWith(deleteCertificateFake, sinon.match({
@@ -186,7 +193,10 @@ describe('DNS Validated Certificate Handler', () => {
186193
.event({
187194
RequestType: 'Delete',
188195
RequestId: testRequestId,
189-
PhysicalResourceId: testCertificateArn
196+
PhysicalResourceId: testCertificateArn,
197+
ResourceProperties: {
198+
Region: 'us-east-1',
199+
}
190200
})
191201
.expectResolve(() => {
192202
sinon.assert.calledWith(deleteCertificateFake, sinon.match({

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ export interface DnsValidatedCertificateProps extends CertificateProps {
1212
* must be authoritative for the domain name specified in the Certificate Request.
1313
*/
1414
readonly hostedZone: route53.IHostedZone;
15+
/**
16+
* AWS region that will host the certificate. This is needed especially
17+
* for certificates used for CloudFront distributions, which require the region
18+
* to be us-east-1.
19+
*
20+
* @default the region the stack is deployed in.
21+
*/
22+
readonly region?: string;
1523
}
1624

1725
/**
@@ -64,7 +72,8 @@ export class DnsValidatedCertificate extends cdk.Construct implements ICertifica
6472
properties: {
6573
DomainName: props.domainName,
6674
SubjectAlternativeNames: props.subjectAlternativeNames,
67-
HostedZoneId: this.hostedZoneId
75+
HostedZoneId: this.hostedZoneId,
76+
Region: props.region,
6877
}
6978
});
7079

0 commit comments

Comments
 (0)