Skip to content

Commit 136d3a8

Browse files
authored
chore(custom-resources): create a partial cfn template that adds an alias to rd-service (#2616)
1. Creates a partial cfn template that adds an alias to rd-service 2. Patch to the previous PR #2589: when the lambda tries to delete a non-existent record in response to `DELETE` action, it shouldn't error out. 3. Patch to the previous PR #2589: instead of passing the hosted zone id from CFN, the lambda gets the hosted zone ID by making an API call. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent ea29075 commit 136d3a8

File tree

3 files changed

+391
-31
lines changed

3 files changed

+391
-31
lines changed

cf-custom-resources/lib/custom-domain-app-runner.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ function report (
8484

8585
exports.handler = async function (event, context) {
8686
const props = event.ResourceProperties;
87-
const [serviceARN, appDNSRole, customDomain] = [props.ServiceARN, props.AppDNSRole, props.CustomDomain,];
88-
appHostedZoneID = props.HostedZoneID;
87+
const [serviceARN, appDNSRole, customDomain, appDNSName] = [props.ServiceARN, props.AppDNSRole, props.CustomDomain, props.AppDNSName, ];
8988
const physicalResourceID = `/associate-domain-app-runner/${customDomain}`;
9089
let handler = async function () {
9190
// Configure clients.
@@ -96,7 +95,7 @@ exports.handler = async function (event, context) {
9695
}),
9796
});
9897
appRunnerClient = new AWS.AppRunner();
99-
98+
appHostedZoneID = await domainHostedZoneID(appDNSName);
10099
switch (event.RequestType) {
101100
case "Create":
102101
case "Update":
@@ -110,7 +109,6 @@ exports.handler = async function (event, context) {
110109
throw new Error(`Unsupported request type ${event.RequestType}`);
111110
}
112111
};
113-
114112
try {
115113
await Promise.race([exports.deadlineExpired(), handler(),]);
116114
await report(event, context, "SUCCESS", physicalResourceID);
@@ -130,6 +128,22 @@ exports.deadlineExpired = function () {
130128
});
131129
};
132130

131+
/**
132+
* Get the hosted zone ID of the domain name from the app account.
133+
* @param {string} domainName
134+
*/
135+
async function domainHostedZoneID(domainName) {
136+
const data = await appRoute53Client.listHostedZonesByName({
137+
DNSName: domainName,
138+
MaxItems: "1",
139+
}).promise();
140+
141+
if (!data.HostedZones || data.HostedZones.length === 0) {
142+
throw new Error(`couldn't find any Hosted Zone with DNS name ${domainName}`);
143+
}
144+
return data.HostedZones[0].Id.split("/").pop();
145+
}
146+
133147
/**
134148
* Add custom domain for service by associating and adding records for both the domain and the validation.
135149
* Errors are not handled and are directly passed to the caller.
@@ -364,9 +378,16 @@ async function updateCNAMERecordAndWait(recordName, recordValue, hostedZoneID, a
364378
HostedZoneId: hostedZoneID,
365379
};
366380

367-
const data = await appRoute53Client.changeResourceRecordSets(params).promise().catch((err) => {
381+
let data;
382+
try {
383+
data = await appRoute53Client.changeResourceRecordSets(params).promise();
384+
} catch (err) {
385+
let recordSetNotFoundErrMessageRegex = /Tried to delete resource record set \[name='.*', type='CNAME'] but it was not found/;
386+
if (action === "DELETE" && err.message.search(recordSetNotFoundErrMessageRegex) !== -1) {
387+
return; // If we attempt to `DELETE` a record that doesn't exist, the job is already done, skip waiting.
388+
}
368389
throw new Error(`update record ${recordName}: ` + err.message);
369-
});
390+
}
370391

371392
await appRoute53Client.waitFor('resourceRecordSetsChanged', {
372393
// Wait up to 5 minutes

0 commit comments

Comments
 (0)