I’m refactoring a cdk stack in ts that has hit the resource limit. Since it contains stateful resources, I want to be sure that the refactored stack refers to the same resources, and not new resources, which could trigger undesirable outcomes, such as orphaning or deletion.
To address this, I’m looking to write a unit test that asserts that the template generated from a stack contains resources with a given set of logical ids. I’ve seen ways to assert that resources with given properties exist, but not that resources with given ids exist.
How can I check that a stack contains resources with given logicalIds? I want to do something like the 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();
});
});
});