Skip to content

Commit

Permalink
fix(route53): allow records with a weight of 0 (#29595)
Browse files Browse the repository at this point in the history
### 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*
  • Loading branch information
clementallen committed Mar 27, 2024
1 parent 798ef67 commit cc7e95b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 5 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@
"Type": "A",
"Weight": 50
}
},
"WeightedRecord328406D0A": {
"Type": "AWS::Route53::RecordSet",
"Properties": {
"HostedZoneId": {
"Ref": "HostedZoneDB99F866"
},
"Name": "www.cdk.dev.",
"ResourceRecords": [
"4.5.6.7"
],
"SetIdentifier": "WEIGHT_0_ID_weightedrecordWeightedRecord37AA25819",
"TTL": "10",
"Type": "A",
"Weight": 0
}
}
},
"Parameters": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TestStack extends Stack {
{ target: '1.2.3.4', weight: 20 },
{ target: '2.3.4.5', weight: 30 },
{ target: '3.4.5.6', weight: 50 },
{ target: '4.5.6.7', weight: 0 },
].forEach((data, index) => {
new route53.ARecord(this, `WeightedRecord${index}`, {
zone: hostedZone,
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-route53/lib/record-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export class RecordSet extends Resource implements IRecordSet {
if (props.setIdentifier && (props.setIdentifier.length < 1 || props.setIdentifier.length > 128)) {
throw new Error(`setIdentifier must be between 1 and 128 characters long, got: ${props.setIdentifier.length}`);
}
if (props.setIdentifier && !props.weight && !props.geoLocation && !props.region && !props.multiValueAnswer) {
if (props.setIdentifier && props.weight === undefined && !props.geoLocation && !props.region && !props.multiValueAnswer) {
throw new Error('setIdentifier can only be specified for non-simple routing policies');
}
if (props.multiValueAnswer && props.target.aliasTarget) {
Expand Down Expand Up @@ -405,7 +405,7 @@ export class RecordSet extends Resource implements IRecordSet {
return identifier;
}

if (this.weight) {
if (this.weight !== undefined) {
const idPrefix = `WEIGHT_${this.weight}_ID_`;
return this.createIdentifier(idPrefix);
}
Expand Down
33 changes: 33 additions & 0 deletions packages/aws-cdk-lib/aws-route53/test/record-set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,39 @@ describe('record set', () => {
});
});

test('with weight of 0', () => {
// GIVEN
const stack = new Stack();

const zone = new route53.HostedZone(stack, 'HostedZone', {
zoneName: 'myzone',
});

// WHEN
new route53.RecordSet(stack, 'RecordSet', {
zone,
recordName: 'www',
recordType: route53.RecordType.CNAME,
target: route53.RecordTarget.fromValues('zzz'),
weight: 0,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Route53::RecordSet', {
Name: 'www.myzone.',
Type: 'CNAME',
HostedZoneId: {
Ref: 'HostedZoneDB99F866',
},
ResourceRecords: [
'zzz',
],
TTL: '1800',
Weight: 0,
SetIdentifier: 'WEIGHT_0_ID_RecordSet',
});
});

test.each([
[-1],
[256],
Expand Down

0 comments on commit cc7e95b

Please sign in to comment.