@bitblit/ratchet-aws
Version:
Common tools for use with AWS browser and node
59 lines • 2.4 kB
JavaScript
import { Logger } from '@bitblit/ratchet-common/logger/logger';
import { ChangeResourceRecordSetsCommand, waitUntilResourceRecordSetsChanged, } from '@aws-sdk/client-route-53';
import { WaiterState } from '@smithy/util-waiter';
export class Route53Ratchet {
route53;
hostedZoneId;
constructor(route53, hostedZoneId) {
this.route53 = route53;
this.hostedZoneId = hostedZoneId;
if (!this.route53) {
throw 'route53 may not be null';
}
}
get route53Client() {
return this.route53;
}
async changeCnameRecordTarget(domainName, target, hostedZoneId = this.hostedZoneId, ttlSeconds = 600) {
Logger.info('Updating %s to point to %s', domainName, target);
try {
const params = {
ChangeBatch: {
Changes: [
{
Action: 'UPSERT',
ResourceRecordSet: {
Name: domainName,
ResourceRecords: [
{
Value: target,
},
],
TTL: ttlSeconds,
Type: 'CNAME',
},
},
],
},
HostedZoneId: hostedZoneId,
};
const result = await this.route53.send(new ChangeResourceRecordSetsCommand(params));
Logger.debug('Updated domain result: %j', result);
const waitParams = {
Id: result.ChangeInfo.Id,
};
const waitResult = await waitUntilResourceRecordSetsChanged({ client: this.route53, maxWaitTime: 300 }, waitParams);
Logger.debug('Wait responsed: %j', waitResult);
if (waitResult.state === WaiterState.SUCCESS) {
Logger.info('Updated %s to point to %s', domainName, hostedZoneId);
return true;
}
}
catch (err) {
Logger.warn('Error update CName for %s with value %s: %j', domainName, target, err);
}
Logger.info('Cannot update %s to point to %s', domainName, target);
return false;
}
}
//# sourceMappingURL=route-53-ratchet.js.map