Skip to content

Commit

Permalink
feat(acm): allow specifying region for validated certificates (#2626)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CaerusKaru authored and rix0rrr committed May 28, 2019
1 parent 56f544e commit 1a7d4db
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ let report = function (event, context, responseStatus, physicalResourceId, respo
* @param {string} hostedZoneId the Route53 Hosted Zone ID
* @returns {string} Validated certificate ARN
*/
const requestCertificate = async function (requestId, domainName, subjectAlternativeNames, hostedZoneId) {
const requestCertificate = async function (requestId, domainName, subjectAlternativeNames, hostedZoneId, region) {
const crypto = require('crypto');
const acm = new aws.ACM();
const acm = new aws.ACM({region});
const route53 = new aws.Route53();
if (waiter) {
// Used by the test suite, since waiters aren't mockable yet
Expand Down Expand Up @@ -157,8 +157,8 @@ const requestCertificate = async function (requestId, domainName, subjectAlterna
*
* @param {string} arn The certificate ARN
*/
const deleteCertificate = async function (arn) {
const acm = new aws.ACM();
const deleteCertificate = async function (arn, region) {
const acm = new aws.ACM({region});

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

Expand Down Expand Up @@ -189,7 +189,8 @@ exports.certificateRequestHandler = async function (event, context) {
event.RequestId,
event.ResourceProperties.DomainName,
event.ResourceProperties.SubjectAlternativeNames,
event.ResourceProperties.HostedZoneId
event.ResourceProperties.HostedZoneId,
event.ResourceProperties.Region,
);
responseData.Arn = physicalResourceId = certificateArn;
break;
Expand All @@ -198,7 +199,7 @@ exports.certificateRequestHandler = async function (event, context) {
// If the resource didn't create correctly, the physical resource ID won't be the
// certificate ARN, so don't try to delete it in that case.
if (physicalResourceId.startsWith('arn:')) {
await deleteCertificate(physicalResourceId);
await deleteCertificate(physicalResourceId, event.ResourceProperties.Region);
}
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ describe('DNS Validated Certificate Handler', () => {
ResourceProperties: {
DomainName: testDomainName,
SubjectAlternativeNames: [],
HostedZoneId: testHostedZoneId
HostedZoneId: testHostedZoneId,
Region: 'us-east-1',
}
})
.expectResolve(() => {
Expand Down Expand Up @@ -138,7 +139,10 @@ describe('DNS Validated Certificate Handler', () => {
.event({
RequestType: 'Delete',
RequestId: testRequestId,
PhysicalResourceId: testCertificateArn
PhysicalResourceId: testCertificateArn,
ResourceProperties: {
Region: 'us-east-1',
}
})
.expectResolve(() => {
sinon.assert.calledWith(deleteCertificateFake, sinon.match({
Expand All @@ -162,7 +166,10 @@ describe('DNS Validated Certificate Handler', () => {
.event({
RequestType: 'Delete',
RequestId: testRequestId,
PhysicalResourceId: testCertificateArn
PhysicalResourceId: testCertificateArn,
ResourceProperties: {
Region: 'us-east-1',
}
})
.expectResolve(() => {
sinon.assert.calledWith(deleteCertificateFake, sinon.match({
Expand All @@ -186,7 +193,10 @@ describe('DNS Validated Certificate Handler', () => {
.event({
RequestType: 'Delete',
RequestId: testRequestId,
PhysicalResourceId: testCertificateArn
PhysicalResourceId: testCertificateArn,
ResourceProperties: {
Region: 'us-east-1',
}
})
.expectResolve(() => {
sinon.assert.calledWith(deleteCertificateFake, sinon.match({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export interface DnsValidatedCertificateProps extends CertificateProps {
* must be authoritative for the domain name specified in the Certificate Request.
*/
readonly hostedZone: route53.IHostedZone;
/**
* AWS region that will host the certificate. This is needed especially
* for certificates used for CloudFront distributions, which require the region
* to be us-east-1.
*
* @default the region the stack is deployed in.
*/
readonly region?: string;
}

/**
Expand Down Expand Up @@ -64,7 +72,8 @@ export class DnsValidatedCertificate extends cdk.Construct implements ICertifica
properties: {
DomainName: props.domainName,
SubjectAlternativeNames: props.subjectAlternativeNames,
HostedZoneId: this.hostedZoneId
HostedZoneId: this.hostedZoneId,
Region: props.region,
}
});

Expand Down

0 comments on commit 1a7d4db

Please sign in to comment.