diff --git a/packages/aws-cdk-lib/aws-sns/lib/topic.ts b/packages/aws-cdk-lib/aws-sns/lib/topic.ts index a57569060be8d..f78b32bf8cfe3 100644 --- a/packages/aws-cdk-lib/aws-sns/lib/topic.ts +++ b/packages/aws-cdk-lib/aws-sns/lib/topic.ts @@ -12,6 +12,9 @@ export interface TopicProps { /** * A developer-defined string that can be used to identify this SNS topic. * + * The display name must be maximum 100 characters long, including hyphens (-), + * underscores (_), spaces, and tabs. + * * @default None */ readonly displayName?: string; @@ -296,6 +299,10 @@ export class Topic extends TopicBase { throw new Error(`signatureVersion must be "1" or "2", received: "${props.signatureVersion}"`); } + if (props.displayName && !Token.isUnresolved(props.displayName) && props.displayName.length > 100) { + throw new Error(`displayName must be less than or equal to 100 characters, got ${props.displayName.length}`); + } + const resource = new CfnTopic(this, 'Resource', { archivePolicy: props.messageRetentionPeriodInDays ? { MessageRetentionPeriod: props.messageRetentionPeriodInDays, diff --git a/packages/aws-cdk-lib/aws-sns/test/sns.test.ts b/packages/aws-cdk-lib/aws-sns/test/sns.test.ts index fc1f1cd4c2846..6c8f04fb096b6 100644 --- a/packages/aws-cdk-lib/aws-sns/test/sns.test.ts +++ b/packages/aws-cdk-lib/aws-sns/test/sns.test.ts @@ -176,6 +176,16 @@ describe('Topic', () => { signatureVersion: '3', })).toThrow(/signatureVersion must be "1" or "2", received: "3"/); }); + + test('throw error when displayName is too long', () => { + const stack = new cdk.Stack(); + + expect(() => { + new sns.Topic(stack, 'MyTopic', { + displayName: 'a'.repeat(101), + }); + }).toThrow('displayName must be less than or equal to 100 characters, got 101'); + }); }); test('can add a policy to the topic', () => {