Skip to content

Commit a3d9b10

Browse files
authored
fix: route53 CrossAccountZoneDelegationRecord fails at deployment time with imported delegatedZone (#30440)
### Issue # (if applicable) Closes #28581. ### Reason for this change An imported `delegatedZone` will not have info about the Name Servers. When it is passed to `CrossAccountZoneDelegationRecord`, the handler will see `undefined` when trying to retrieve the Name Servers info on `delegatedZone`, then throw exception during deployment. This change throws the exception at build time for a faster feedback loop. ### Description of changes `CrossAccountZoneDelegationRecord` throws exception if `delegatedZone.hostedZoneNameServers` is undefined. ### Description of how you validated changes Add unit test to cover the case of passing an imported HostedZone to `CrossAccountZoneDelegationRecord` ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent ddbbd00 commit a3d9b10

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

packages/aws-cdk-lib/aws-route53/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ const delegationRole = iam.Role.fromRoleArn(this, 'DelegationRole', delegationRo
313313

314314
// create the record
315315
new route53.CrossAccountZoneDelegationRecord(this, 'delegate', {
316-
delegatedZone: subZone,
316+
delegatedZone: subZone, // Note that an imported HostedZone is not supported as Name Servers info will not be available
317317
parentHostedZoneName: 'someexample.com', // or you can use parentHostedZoneId
318318
delegationRole,
319319
});

packages/aws-cdk-lib/aws-route53/lib/record-set.ts

+4
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,10 @@ export class CrossAccountZoneDelegationRecord extends Construct {
929929
throw Error('Only one of parentHostedZoneName and parentHostedZoneId is supported');
930930
}
931931

932+
if (!props.delegatedZone.hostedZoneNameServers) {
933+
throw Error(`Not able to retrieve Name Servers for ${props.delegatedZone.zoneName} due to it being imported.`);
934+
}
935+
932936
const provider = CrossAccountZoneDelegationProvider.getOrCreateProvider(this, CROSS_ACCOUNT_ZONE_DELEGATION_RESOURCE_TYPE);
933937

934938
const role = iam.Role.fromRoleArn(this, 'cross-account-zone-delegation-handler-role', provider.roleArn);

packages/aws-cdk-lib/aws-route53/test/record-set.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,27 @@ describe('record set', () => {
893893
});
894894
});
895895

896+
test('CrossAccountZoneDelegationRecord should throw if delegatedZone is imported', () => {
897+
// GIVEN
898+
const stack = new Stack();
899+
const parentZone = new route53.PublicHostedZone(stack, 'ParentHostedZone', {
900+
zoneName: 'myzone.com',
901+
});
902+
903+
// WHEN
904+
const childZone = route53.PublicHostedZone.fromPublicHostedZoneAttributes(stack, 'ChildHostedZone', {
905+
hostedZoneId: 'fake-id',
906+
zoneName: 'fake-name',
907+
});
908+
909+
//THEN
910+
expect(() => new route53.CrossAccountZoneDelegationRecord(stack, 'Delegation', {
911+
delegatedZone: childZone,
912+
parentHostedZoneId: parentZone.hostedZoneId,
913+
delegationRole: parentZone.crossAccountZoneDelegationRole!,
914+
})).toThrow(/Not able to retrieve Name Servers for fake-name due to it being imported./);
915+
});
916+
896917
testDeprecated('Cross account zone delegation record with parentHostedZoneName', () => {
897918
// GIVEN
898919
const stack = new Stack();

0 commit comments

Comments
 (0)