UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

2 lines (1 loc) 2 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.handler=void 0;const client_route_53_1=require("@aws-sdk/client-route-53"),credential_providers_1=require("@aws-sdk/credential-providers");async function handler(event){const resourceProps=event.ResourceProperties;switch(event.RequestType){case"Create":case"Update":return cfnEventHandler(resourceProps,!1);case"Delete":return cfnEventHandler(resourceProps,!0)}}exports.handler=handler;async function cfnEventHandler(props,isDeleteEvent){const{AssumeRoleArn,ParentZoneId,ParentZoneName,DelegatedZoneName,DelegatedZoneNameServers,TTL}=props;if(!ParentZoneId&&!ParentZoneName)throw Error("One of ParentZoneId or ParentZoneName must be specified");const timestamp=new Date().getTime(),route53=new client_route_53_1.Route53({credentials:(0,credential_providers_1.fromTemporaryCredentials)({clientConfig:{region:route53Region(process.env.AWS_REGION??process.env.AWS_DEFAULT_REGION??"")},params:{RoleArn:AssumeRoleArn,RoleSessionName:`cross-account-zone-delegation-${timestamp}`}})}),parentZoneId=ParentZoneId??await getHostedZoneIdByName(ParentZoneName,route53);await route53.changeResourceRecordSets({HostedZoneId:parentZoneId,ChangeBatch:{Changes:[{Action:isDeleteEvent?"DELETE":"UPSERT",ResourceRecordSet:{Name:DelegatedZoneName,Type:"NS",TTL,ResourceRecords:DelegatedZoneNameServers.map(ns=>({Value:ns}))}}]}})}async function getHostedZoneIdByName(name,route53){const matchedZones=(await route53.listHostedZonesByName({DNSName:name})).HostedZones?.filter(zone=>zone.Name===`${name}.`)??[];if(matchedZones&&matchedZones.length!==1)throw Error(`Expected one hosted zone to match the given name but found ${matchedZones.length}`);return matchedZones[0].Id}function route53Region(region){const partitions={cn:"cn-northwest-1","us-gov":"us-gov-west-1","us-iso":"us-iso-east-1","us-isob":"us-isob-east-1"};for(const[prefix,mainRegion]of Object.entries(partitions))if(region.startsWith(`${prefix}-`))return mainRegion;return"us-east-1"}