Skip to content

Commit 5d155d7

Browse files
TheRealAmazonKendrascanlonp
authored andcommitted
revert: fix(core): overrideLogicalId validation (#30695)
Reverts #29708 due to #30669 Closes #30669
1 parent d3695d4 commit 5d155d7

File tree

2 files changed

+8
-99
lines changed

2 files changed

+8
-99
lines changed

packages/aws-cdk-lib/core/lib/cfn-element.ts

+2-21
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,14 @@ export abstract class CfnElement extends Construct {
8181
/**
8282
* Overrides the auto-generated logical ID with a specific ID.
8383
* @param newLogicalId The new logical ID to use for this stack element.
84-
*
85-
* @throws an error if `logicalId` has already been locked
86-
* @throws an error if `newLogicalId` is empty
87-
* @throws an error if `newLogicalId` contains more than 255 characters
88-
* @throws an error if `newLogicalId` contains non-alphanumeric characters
89-
*
90-
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html#resources-section-structure-logicalid
9184
*/
9285
public overrideLogicalId(newLogicalId: string) {
9386
if (this._logicalIdLocked) {
9487
throw new Error(`The logicalId for resource at path ${Node.of(this).path} has been locked and cannot be overridden\n` +
9588
'Make sure you are calling "overrideLogicalId" before Stack.exportValue');
89+
} else {
90+
this._logicalIdOverride = newLogicalId;
9691
}
97-
98-
if (!Token.isUnresolved(newLogicalId)) {
99-
if (!newLogicalId) {
100-
throw new Error('Cannot set an empty logical ID');
101-
}
102-
if (newLogicalId.length > 255) {
103-
throw new Error(`Invalid logical ID override: '${newLogicalId}'. It must be at most 255 characters long, got ${newLogicalId.length} characters.`);
104-
}
105-
if (!newLogicalId.match(/^[A-Za-z0-9]+$/)) {
106-
throw new Error(`Invalid logical ID override: '${newLogicalId}'. It must only contain alphanumeric characters.`);
107-
}
108-
}
109-
110-
this._logicalIdOverride = newLogicalId;
11192
}
11293

11394
/**

packages/aws-cdk-lib/core/test/stack.test.ts

+6-78
Original file line numberDiff line numberDiff line change
@@ -1370,109 +1370,37 @@ describe('stack', () => {
13701370

13711371
// THEN - producers are the same
13721372
expect(() => {
1373-
resourceM.overrideLogicalId('OVERRIDELOGICALID');
1373+
resourceM.overrideLogicalId('OVERRIDE_LOGICAL_ID');
13741374
}).toThrow(/The logicalId for resource at path Producer\/ResourceXXX has been locked and cannot be overridden/);
13751375
});
13761376

1377-
test('throw error if overrideLogicalId contains non-alphanumeric characters', () => {
1378-
// GIVEN: manual
1379-
const appM = new App();
1380-
const producerM = new Stack(appM, 'Producer');
1381-
const resourceM = new CfnResource(producerM, 'ResourceXXX', { type: 'AWS::Resource' });
1382-
1383-
// THEN - producers are the same
1384-
expect(() => {
1385-
resourceM.overrideLogicalId('INVALID_LOGICAL_ID');
1386-
}).toThrow(/must only contain alphanumeric characters/);
1387-
});
1388-
1389-
test('throw error if overrideLogicalId is over 255 characters', () => {
1390-
// GIVEN: manual
1391-
const appM = new App();
1392-
const producerM = new Stack(appM, 'Producer');
1393-
const resourceM = new CfnResource(producerM, 'ResourceXXX', { type: 'AWS::Resource' });
1394-
1395-
// THEN - producers are the same
1396-
expect(() => {
1397-
resourceM.overrideLogicalId(
1398-
// 256 character long string of "aaaa..."
1399-
Array(256).fill('a').join(''),
1400-
);
1401-
}).toThrow(/must be at most 255 characters long, got 256 characters/);
1402-
});
1403-
1404-
test('throw error if overrideLogicalId is an empty string', () => {
1405-
// GIVEN: manual
1406-
const appM = new App();
1407-
const producerM = new Stack(appM, 'Producer');
1408-
const resourceM = new CfnResource(producerM, 'ResourceXXX', { type: 'AWS::Resource' });
1409-
1410-
// THEN - producers are the same
1411-
expect(() => {
1412-
resourceM.overrideLogicalId('');
1413-
}).toThrow('Cannot set an empty logical ID');
1414-
});
1415-
14161377
test('do not throw error if overrideLogicalId is used and logicalId is not locked', () => {
14171378
// GIVEN: manual
14181379
const appM = new App();
14191380
const producerM = new Stack(appM, 'Producer');
14201381
const resourceM = new CfnResource(producerM, 'ResourceXXX', { type: 'AWS::Resource' });
14211382

14221383
// THEN - producers are the same
1423-
resourceM.overrideLogicalId('OVERRIDELOGICALID');
1424-
producerM.exportValue(resourceM.getAtt('Att'));
1425-
1426-
const template = appM.synth().getStackByName(producerM.stackName).template;
1427-
expect(template).toMatchObject({
1428-
Outputs: {
1429-
ExportsOutputFnGetAttOVERRIDELOGICALIDAtt76AC816F: {
1430-
Export: {
1431-
Name: 'Producer:ExportsOutputFnGetAttOVERRIDELOGICALIDAtt76AC816F',
1432-
},
1433-
Value: {
1434-
'Fn::GetAtt': [
1435-
'OVERRIDELOGICALID',
1436-
'Att',
1437-
],
1438-
},
1439-
},
1440-
},
1441-
Resources: {
1442-
OVERRIDELOGICALID: {
1443-
Type: 'AWS::Resource',
1444-
},
1445-
},
1446-
});
1447-
});
1448-
1449-
test('do not throw if overrideLogicalId is unresolved', () => {
1450-
// GIVEN: manual
1451-
const appM = new App();
1452-
const producerM = new Stack(appM, 'Producer');
1453-
const resourceM = new CfnResource(producerM, 'ResourceXXX', { type: 'AWS::Resource' });
1454-
1455-
// THEN - producers are the same
1456-
resourceM.overrideLogicalId(Lazy.string({ produce: () => 'INVALID_LOGICAL_ID' }));
1384+
resourceM.overrideLogicalId('OVERRIDE_LOGICAL_ID');
14571385
producerM.exportValue(resourceM.getAtt('Att'));
14581386

14591387
const template = appM.synth().getStackByName(producerM.stackName).template;
14601388
expect(template).toMatchObject({
14611389
Outputs: {
1462-
ExportsOutputFnGetAttINVALIDLOGICALIDAtt6CB9E5B9: {
1390+
ExportsOutputFnGetAttOVERRIDELOGICALIDAtt2DD28019: {
14631391
Export: {
1464-
Name: 'Producer:ExportsOutputFnGetAttINVALIDLOGICALIDAtt6CB9E5B9',
1392+
Name: 'Producer:ExportsOutputFnGetAttOVERRIDELOGICALIDAtt2DD28019',
14651393
},
14661394
Value: {
14671395
'Fn::GetAtt': [
1468-
'INVALID_LOGICAL_ID',
1396+
'OVERRIDE_LOGICAL_ID',
14691397
'Att',
14701398
],
14711399
},
14721400
},
14731401
},
14741402
Resources: {
1475-
INVALID_LOGICAL_ID: {
1403+
OVERRIDE_LOGICAL_ID: {
14761404
Type: 'AWS::Resource',
14771405
},
14781406
},

0 commit comments

Comments
 (0)