From f879765229f12d4bf2e16fb3ca512787aa573331 Mon Sep 17 00:00:00 2001 From: nicholaschiasson Date: Sat, 19 Oct 2024 13:50:21 +0100 Subject: [PATCH] fix(aws_route53): cannot use CfnParameter.valueAsNumber for L2 RecordSet weight --- .../aws-cdk-lib/aws-route53/lib/record-set.ts | 2 +- .../aws-route53/test/record-set.test.ts | 62 ++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/aws-route53/lib/record-set.ts b/packages/aws-cdk-lib/aws-route53/lib/record-set.ts index 6706443b1f260..021affdd2b1de 100644 --- a/packages/aws-cdk-lib/aws-route53/lib/record-set.ts +++ b/packages/aws-cdk-lib/aws-route53/lib/record-set.ts @@ -296,7 +296,7 @@ export class RecordSet extends Resource implements IRecordSet { constructor(scope: Construct, id: string, props: RecordSetProps) { super(scope, id); - if (props.weight && (props.weight < 0 || props.weight > 255)) { + if (props.weight && !Token.isUnresolved(props.weight) && (props.weight < 0 || props.weight > 255)) { throw new Error(`weight must be between 0 and 255 inclusive, got: ${props.weight}`); } if (props.setIdentifier && (props.setIdentifier.length < 1 || props.setIdentifier.length > 128)) { diff --git a/packages/aws-cdk-lib/aws-route53/test/record-set.test.ts b/packages/aws-cdk-lib/aws-route53/test/record-set.test.ts index 20abd698ee68b..f66b283b1aaca 100644 --- a/packages/aws-cdk-lib/aws-route53/test/record-set.test.ts +++ b/packages/aws-cdk-lib/aws-route53/test/record-set.test.ts @@ -4,7 +4,7 @@ import * as cloudfront from '../../aws-cloudfront'; import * as origins from '../../aws-cloudfront-origins'; import * as iam from '../../aws-iam'; import * as targets from '../../aws-route53-targets'; -import { Duration, RemovalPolicy, Stack } from '../../core'; +import { CfnParameter, Duration, RemovalPolicy, Stack } from '../../core'; import * as route53 from '../lib'; describe('record set', () => { @@ -1248,6 +1248,66 @@ describe('record set', () => { }); }); + test('with weight provided by CfnParameter', () => { + // GIVEN + const stack = new Stack(); + + const zone = new route53.HostedZone(stack, 'HostedZone', { + zoneName: 'myzone', + }); + + const weightParameter = new CfnParameter(stack, 'RecordWeight', { + type: 'Number', + default: 0, + minValue: 0, + maxValue: 255, + }); + + // WHEN + new route53.RecordSet(stack, 'RecordSet', { + zone, + recordName: 'www', + recordType: route53.RecordType.CNAME, + target: route53.RecordTarget.fromValues('zzz'), + weight: weightParameter.valueAsNumber, + }); + + // THEN + Template.fromStack(stack).hasParameter('RecordWeight', { + Type: 'Number', + Default: 0, + MinValue: 0, + MaxValue: 255, + }); + + Template.fromStack(stack).hasResourceProperties('AWS::Route53::RecordSet', { + Name: 'www.myzone.', + Type: 'CNAME', + HostedZoneId: { + Ref: 'HostedZoneDB99F866', + }, + ResourceRecords: [ + 'zzz', + ], + TTL: '1800', + Weight: { + Ref: 'RecordWeight', + }, + SetIdentifier: { + 'Fn::Join': [ + '', + [ + 'WEIGHT_', + { + Ref: 'RecordWeight', + }, + '_ID_RecordSet', + ], + ], + }, + }); + }); + test.each([ [-1], [256],