Skip to content

Commit cc7e95b

Browse files
authored
fix(route53): allow records with a weight of 0 (#29595)
### Issue # (if applicable) Closes #29556 . ### Reason for this change When creating an ARecord object in Route53 with props.weight set to 0 an Error is thrown. ### Description of changes Change checks for `weight` to use `weight === undefined` instead of `!weight` ### Description of how you validated changes - Added a unit test with `weight: 0` - Updated integ tests to include a record with `weight: 0` ### 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 798ef67 commit cc7e95b

File tree

7 files changed

+95
-5
lines changed

7 files changed

+95
-5
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-route53/test/integ.weighted-record.js.snapshot/manifest.json

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-route53/test/integ.weighted-record.js.snapshot/tree.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-route53/test/integ.weighted-record.js.snapshot/weighted-record.assets.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-route53/test/integ.weighted-record.js.snapshot/weighted-record.template.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@
5353
"Type": "A",
5454
"Weight": 50
5555
}
56+
},
57+
"WeightedRecord328406D0A": {
58+
"Type": "AWS::Route53::RecordSet",
59+
"Properties": {
60+
"HostedZoneId": {
61+
"Ref": "HostedZoneDB99F866"
62+
},
63+
"Name": "www.cdk.dev.",
64+
"ResourceRecords": [
65+
"4.5.6.7"
66+
],
67+
"SetIdentifier": "WEIGHT_0_ID_weightedrecordWeightedRecord37AA25819",
68+
"TTL": "10",
69+
"Type": "A",
70+
"Weight": 0
71+
}
5672
}
5773
},
5874
"Parameters": {

packages/@aws-cdk-testing/framework-integ/test/aws-route53/test/integ.weighted-record.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class TestStack extends Stack {
1515
{ target: '1.2.3.4', weight: 20 },
1616
{ target: '2.3.4.5', weight: 30 },
1717
{ target: '3.4.5.6', weight: 50 },
18+
{ target: '4.5.6.7', weight: 0 },
1819
].forEach((data, index) => {
1920
new route53.ARecord(this, `WeightedRecord${index}`, {
2021
zone: hostedZone,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export class RecordSet extends Resource implements IRecordSet {
302302
if (props.setIdentifier && (props.setIdentifier.length < 1 || props.setIdentifier.length > 128)) {
303303
throw new Error(`setIdentifier must be between 1 and 128 characters long, got: ${props.setIdentifier.length}`);
304304
}
305-
if (props.setIdentifier && !props.weight && !props.geoLocation && !props.region && !props.multiValueAnswer) {
305+
if (props.setIdentifier && props.weight === undefined && !props.geoLocation && !props.region && !props.multiValueAnswer) {
306306
throw new Error('setIdentifier can only be specified for non-simple routing policies');
307307
}
308308
if (props.multiValueAnswer && props.target.aliasTarget) {
@@ -405,7 +405,7 @@ export class RecordSet extends Resource implements IRecordSet {
405405
return identifier;
406406
}
407407

408-
if (this.weight) {
408+
if (this.weight !== undefined) {
409409
const idPrefix = `WEIGHT_${this.weight}_ID_`;
410410
return this.createIdentifier(idPrefix);
411411
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,39 @@ describe('record set', () => {
11331133
});
11341134
});
11351135

1136+
test('with weight of 0', () => {
1137+
// GIVEN
1138+
const stack = new Stack();
1139+
1140+
const zone = new route53.HostedZone(stack, 'HostedZone', {
1141+
zoneName: 'myzone',
1142+
});
1143+
1144+
// WHEN
1145+
new route53.RecordSet(stack, 'RecordSet', {
1146+
zone,
1147+
recordName: 'www',
1148+
recordType: route53.RecordType.CNAME,
1149+
target: route53.RecordTarget.fromValues('zzz'),
1150+
weight: 0,
1151+
});
1152+
1153+
// THEN
1154+
Template.fromStack(stack).hasResourceProperties('AWS::Route53::RecordSet', {
1155+
Name: 'www.myzone.',
1156+
Type: 'CNAME',
1157+
HostedZoneId: {
1158+
Ref: 'HostedZoneDB99F866',
1159+
},
1160+
ResourceRecords: [
1161+
'zzz',
1162+
],
1163+
TTL: '1800',
1164+
Weight: 0,
1165+
SetIdentifier: 'WEIGHT_0_ID_RecordSet',
1166+
});
1167+
});
1168+
11361169
test.each([
11371170
[-1],
11381171
[256],

0 commit comments

Comments
 (0)