Stack deletion deletes log group but re-creates it on lambda invocation

0

Hello,

I have a cloudformation template which defines a lambda, which is invoked by a custom resource.

The cloud formation creates a log group with the appropriate "/aws/lamba/<LAMBDA REFERENCE>" This is in hopes that it is removed when the stack is deleted.

Using "DependsOn"
The custom resource (invocation of lambda) depends on the log group (for lambda)
The log group (for lambda) depends on the lambda

When the cloud formation is run, I expect that it is creating the resources in a sequential order, lambda, log group (for lambda), custom resource (invocation of lambda of type "CREATE")

When I delete the stack, I also assume it will do it in reverse sequential order.
custom resource (invocation of lambda of type "DELETE"), log group (for lambda), lambda

However, what I am seeing is that the log group gets deleted (it disappears from the console), then gets recreated with the lambda logging the "DELETE" request since it is being execute, and then the lambda gets removed. This leaves an extra log group on the system after the stack has been removed.

This has the effect that I cannot re-run the stack to create it again, because the log group already exists.

Maybe I am not taking something into account, is this expected behavior?

I can post an example if it would be helpful.

asked 4 years ago2130 views
2 Answers
0

After running a few experiments, it does appear as if the order is being preserved, the issue is that there is a delay from the invocation of the lambda, it finished, and then wrote to the logs. No surprise, but writing is not immediate.

This delay is causing the log group to be recreated. I am finding that if I impose an artificial delay between the invocation and the deletion of the log group, the stack gets cleaned up appropriately.

Edited by: lsitzman on Apr 1, 2020 8:07 AM

answered 4 years ago
  • Could you please share more details on how you resolved this issue? How did you implement that artificial delay?

0

Another way to solve this is to take away the permission from the execution role to create a log group (logs:CreateLogGroup), but only if you don't care about logs produced by the Delete event.

AWS
jaehyi
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions