Replies: 4 comments 1 reply
-
Hi It would be helpful if you can provide some minimal self-sustainable sample code so we can discuss the workaround. This seems not a bug but a general guidance request here. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick reply! Below is a simplified code example of the custom resource lambda that captures my use case. import {
CloudFormationCustomResourceEvent,
CloudFormationCustomResourceResponse
} from 'aws-lambda';
const lambdaClient = new LambdaClient();
async function checkEventSourceMappingExists(functionName: string, eventSourceArn: string): Promise<boolean> {
// Logic to determine if the event source mapping exists;
}
async function deleteEventSourceMapping(uuid: string): Promise<void> {
// Logic to delete the event source mapping
}
async function createEventSourceMapping(functionName: string, eventSourceArn: string): Promise<string> {
// Logic to create the event source mapping
}
export const handler = async (event: CloudFormationCustomResourceEvent): Promise<CloudFormationCustomResourceResponse> => {
const functionName = event.ResourceProperties.FunctionName;
const eventSourceArn = event.ResourceProperties.EventSourceArn;
// Handle both create and update events
if (event.RequestType === 'Create' || event.RequestType === 'Update') {
const exists = await checkEventSourceMappingExists(functionName, eventSourceArn);
if (exists) {
// If the mapping exists, delete it before creating a new one
await deleteEventSourceMapping(...);
}
// Create a new mapping
const newUuid = await createEventSourceMapping(functionName, eventSourceArn);
return {
Status: 'SUCCESS',
PhysicalResourceId: newUuid, // CF will trigger a delete event since the PhysicalResourceId changed
StackId: event.StackId,
RequestId: event.RequestId,
LogicalResourceId: event.LogicalResourceId,
Data: {}
};
}
// Handle delete event
// ...
}; |
Beta Was this translation helpful? Give feedback.
-
We generally recommend using custom resource provider framework so you can just focus on the onCreate, onUpdate, onDelete event handling. Is there any reason you need to handle that rather than using the framework? |
Beta Was this translation helpful? Give feedback.
-
This doesn't seem to be a bug and I am moving this to discussion for general guidance. |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
I'm trying to update a subscription filter via a custom resource. I'd like to hook into the lifecycle as intended. This would look like so:
However, we can't update subscription filters since they're immutable. instead, i'm forced to delete the subscription, and create a new one. This new subscription will have a new PhysicalResourceId, which CloudFormation interprets as evidence we created a new subscription, and triggers a separate delete operation to remove the old resource. The issue here is, there is nothing to delete (In creating the new subscription, I had to delete the old one since you cant have 2 subscriptions with the same topic and destination).
My question is, how should I return PhysicalResourceId when replacing a resource in AWS? I currently return the same PhysicalResourceId every time, to prevent the extra delete from being triggered, which is certainly an anti-pattern. Any guidance here?
Expected Behavior
I expected CloudFormation not to send a delete request after an update operation when the custom resource logic has already handled the deletion of the old resource.
Current Behavior
CloudFormation sends a delete request for the old resource after an update operation, despite the fact that the custom resource logic has preemptively performed the deletion. This results in errors since the resource to be deleted no longer exists.
Reproduction Steps
Possible Solution
No response
Additional Information/Context
No response
CDK CLI Version
2.81.0 (build bd920f2)
Framework Version
No response
Node.js Version
v18.17.1
OS
MacOS 14.2.1 (23C71)
Language
TypeScript
Language Version
No response
Other information
No response
Beta Was this translation helpful? Give feedback.
All reactions