-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Description
Describe the bug
When deploying a ServerlessCache without explicitly specifying serverlessCacheName, the auto-generated name using Names.uniqueId() can exceed the AWS ElastiCache 40 character limit, causing deployment failure.
Current implementation in serverless-cache.ts:
this.serverlessCacheName = props.serverlessCacheName ?? Lazy.string({ produce: () => Names.uniqueId(this) });Names.uniqueId() has no length limit, so when the stack name or construct path is long, the generated name exceeds 40 characters.
Regression Issue
- Select this option if this issue appears to be a regression.
Last Known Working CDK Library Version
No response
Expected Behavior
ServerlessCache should deploy successfully with an auto-generated name that respects the AWS 40 character limit. It should use Names.uniqueResourceName() with maxLength like other CDK constructs.
Current Behavior
Deployment fails with the following error:
Resource handler returned message: "Serverless Cache Name cannot be longer than 40 characters.
(Service: ElastiCache, Status Code: 400, Request ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)"
(RequestToken: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, HandlerErrorCode: InvalidRequest)
Reproduction Steps
import { App, Stack } from 'aws-cdk-lib';
import { Vpc } from 'aws-cdk-lib/aws-ec2';
import { ServerlessCache, CacheEngine } from '@aws-cdk/aws-elasticache-alpha';
const app = new App();
// Use a long stack name to reproduce the issue
const stack = new Stack(app, 'my-application-serverless-cache-stack');
const vpc = new Vpc(stack, 'VPC');
// Do not specify serverlessCacheName - rely on auto-generation
new ServerlessCache(stack, 'MyServerlessCache', {
vpc,
engine: CacheEngine.MEMCACHED_1_6,
});Possible Solution
Replace Names.uniqueId() with Names.uniqueResourceName() and specify maxLength: 40:
this.serverlessCacheName = props.serverlessCacheName ?? Lazy.string({
produce: () => Names.uniqueResourceName(this, { maxLength: 40 }),
});This is consistent with other CDK constructs:
aws-appconfig:Names.uniqueResourceName(this, { maxLength: 64 })aws-cloudfront:Names.uniqueResourceName(this, { maxLength: 64 })aws-rds/proxy:Names.uniqueResourceName(this, { maxLength: 60 })
Note: user-group.ts has the same issue (also uses Names.uniqueId()).
Additional Information/Context
No response
AWS CDK Library version (aws-cdk-lib)
v2.233.0
AWS CDK CLI version
2.1033.0
Node.js Version
v22.14.0
OS
OSX
Language
TypeScript
Language Version
No response
Other information
No response