krishna g
krishna g

Reputation: 461

Unable to delete CloudFormation stack that defines a Lambda-backed custom resource

I created an AWS Lambda function using NodeJS to create VPC flow logs. This Lambda will be triggered by AWS CloudFormation every time I touch the custom resource.

My question: When I delete the CloudFormation stack, the Lambda function does not send a proper response back to CloudFormation, and since the stack is not getting deleted, it's reflecting back with this error:

Custom Resource failed to stabilize in expected time

What can I do to fix this?

Upvotes: 2

Views: 1939

Answers (1)

krishna g
krishna g

Reputation: 461

since custom resource using lambda function i have written sample script to give the response back as success since my requirement is creating NAT gateway using custom resource the sample delete function is as follows

 var deleteflowlogs = function(event, context) {
    var responseData = {};
    if (event.PhysicalResourceId && event.PhysicalResourceId.match(/^nat-/)) {
        console.log("in delete flow logs");
        ec2.deleteFlowLogs({
            FlowLogIds: [event.PhysicalResourceId]
        }, function(err, data) {
            if (err) {
                responseData = {
                    Error: "delete flowlogs failed " + err
                };
                console.log(responseData.Error);
                response.send(event, context, response.FAILED, responseData, event.PhysicalResourceId);
            } else {
                response.send(event, context, response.SUCCESS, {}, event.PhysicalResourceId)
            }
        })
    } else {
        console.log("No valid physical resource id passed to destroy - ignoring " + event.PhysicalResourceId);
        response.send(event, context, response.SUCCESS, responseData, event.PhysicalResourceId);
    }
}

Upvotes: 1

Related Questions