fix(cloudformation): keep uniqueness suffix when truncating generated resource names (#1802)

* fix(cloudformation): keep uniqueness suffix when truncating generated resource names

CloudFormation auto-generates a physical name as
`{stackName}-{logicalId}-{random}` for resources declared without an
explicit name. When that string exceeded the resource type's name-length
cap, generatePhysicalName truncated the whole string with
`substring(0, maxLength)`, which dropped the trailing random token. Every
auto-named resource in a long-named stack then collapsed onto the same
truncated name, breaking Ref/GetAtt lookups — most visibly a
`Custom::LogRetention` whose `ServiceToken` (a GetAtt of its provider
Lambda's Arn) resolved to a different, colliding function.

Truncate only the descriptive prefix and always keep the random suffix,
matching how CloudFormation shortens a generated name while preserving its
uniqueness token, so distinct logical resources keep distinct physical names.

* fix(cloudformation): preserve uniqueness suffix in extracted ProvisionContext name generator

The #1825 registry refactor lifted generatePhysicalName into ProvisionContext
verbatim, so migrated resource types (SQS) once again truncated the trailing
uniqueness token when a stack name neared the length limit. Apply the same
suffix-preserving truncation there so extracted provisioners stay consistent
with the monolith.

* fix(cloudformation): harden generated name regression test
This commit is contained in:
Alex Banna authored and GitHub committed 2026-08-04 22:21:23 -05:00
1 parent 91e06ebebf
commit 48b2139b4f
3 files changed
+150 -6

No files matched your search

@@ -4422,12 +4422,24 @@ public class CloudFormationResourceProvisioner {
*/
private String generatePhysicalName(String stackName, String logicalId, int maxLength, boolean lowercase) {
String suffix = UUID.randomUUID().toString().replace("-", "").substring(0, 12);
String name = stackName + "-" + logicalId + "-" + suffix;
String base = stackName + "-" + logicalId;
if (lowercase) {
name = name.toLowerCase();
base = base.toLowerCase();
}
String name = base + "-" + suffix;
if (maxLength > 0 && name.length() > maxLength) {
name = name.substring(0, maxLength);
// Truncate the descriptive prefix but always keep the trailing uniqueness token. When a
// stack's name approaches the length limit, distinct logical resources still get distinct
// physical names — CloudFormation preserves the random suffix when it shortens a generated
// name. Truncating the whole string (suffix included) would collapse every such resource
// onto one name and break Ref/GetAtt-based lookup (e.g. a custom resource's ServiceToken
// resolving to the wrong Lambda).
int keep = Math.max(0, maxLength - suffix.length() - 1);
String prefix = base.length() > keep ? base.substring(0, keep) : base;
while (prefix.endsWith("-")) {
prefix = prefix.substring(0, prefix.length() - 1);
}
name = prefix.isEmpty() ? suffix : prefix + "-" + suffix;
}
return name;
}
@@ -25,12 +25,24 @@ public record ProvisionContext(CloudFormationTemplateEngine engine, String regio
/** Generates a CloudFormation-style physical name: {@code <stack>-<logicalId>-<suffix>}. */
public String generatePhysicalName(String logicalId, int maxLength, boolean lowercase) {
String suffix = UUID.randomUUID().toString().replace("-", "").substring(0, 12);
String name = stackName + "-" + logicalId + "-" + suffix;
String base = stackName + "-" + logicalId;
if (lowercase) {
name = name.toLowerCase();
base = base.toLowerCase();
}
String name = base + "-" + suffix;
if (maxLength > 0 && name.length() > maxLength) {
name = name.substring(0, maxLength);
// Truncate the descriptive prefix but always keep the trailing uniqueness token. When a
// stack's name approaches the length limit, distinct logical resources still get distinct
// physical names — CloudFormation preserves the random suffix when it shortens a generated
// name. Truncating the whole string (suffix included) would collapse every such resource
// onto one name and break Ref/GetAtt-based lookup (e.g. a custom resource's ServiceToken
// resolving to the wrong Lambda).
int keep = Math.max(0, maxLength - suffix.length() - 1);
String prefix = base.length() > keep ? base.substring(0, keep) : base;
while (prefix.endsWith("-")) {
prefix = prefix.substring(0, prefix.length() - 1);
}
name = prefix.isEmpty() ? suffix : prefix + "-" + suffix;
}
return name;
}