Currently, I am overhauling a CDK stack in TypeScript that has reached the resource limit. Given that it includes stateful resources, I need to ensure that the revamped stack points to the same resources and not new ones that could lead to unfavorable results such as orphaning or deletion.
In order to tackle this issue, my goal is to write a unit test that verifies the template produced from a stack consists of resources with specified logical IDs. While there are methods to confirm the existence of resources with certain properties, I have yet to come across a way to validate the presence of resources based on their IDs.
Is there a way for me to verify that a stack comprises resources with specific logicalIDs? Ideally, I would like to accomplish something similar to the example below:
describe('MyStack', () => {
test('synthesizes with expected logical IDs', () => {
const app = new cdk.App();
const stack = new MyStack(app, 'MyStackDev', {
deploymentEnvironment: 'dev',
});
const template = Template.fromStack(stack);
const expectedLogicalIds = [
apiGatewayDev,
lambdaDev,
webSocketLambdaDev,
devLambdaWithConcurrentProvisioning,
neptuneBastionHostDev,
neptuneClusterDev,
auroraClusterDev,
sevenndevimages,
sevenndevprivate,
sparkQueryLogs,
shareLinks,
webSocketInstances,
flowInteractionLog,
];
expectedLogicalIds.forEach((logicalId) => {
// NOTE: if I need to apply the type, then I'll need to do some more work
// to make sure the right type is matched with the right logical id
// either way, I don't expect it to work as written because I don't
// believe this is proper usage of hasResourceProperties, as the
// logical id is actually the key of the resource object
expect(
template.hasResourceProperties('AWS::Lambda::Function', { LogicalId: logicalId }),
).toBeTruthy();
});
});
});