serverless-next.js icon indicating copy to clipboard operation
serverless-next.js copied to clipboard

Remove does not clean all resources

Open Meemaw opened this issue 5 years ago • 3 comments

It seems that remove does not keep all resources. Specifically, Cloudfront Distribution and Lamba@Edge associated with it.

I see Cloudfront distribution is just being disabled: https://github.com/serverless-nextjs/serverless-next.js/blob/6f68bf1910d80cc69b09f6529081e6ccad74552e/packages/serverless-components/aws-cloudfront/lib/index.js#L319

It would be great if the component had an option to wait for it to be disabled and delete it (and also the associated lambda). I know this might take much longer so perhaps it could be made optional.

AFAIK, you can wait for distribution to be disabled by aws cloudfront wait distribution-deployed --id $id.

Meemaw avatar Nov 02 '20 07:11 Meemaw

This would be nice. However deleting the associated lambda is a little tricky. If you refer to https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-edge-delete-replicas.html,

You can delete a Lambda@Edge function only when the replicas of the function have been deleted by CloudFront Replicas are typically deleted within a few hours. You cannot manually delete Lambda@Edge function replicas

So I'm not sure if is feasible to do this via the remove command. I remember thinking in the past we could have some sort of background cleanup task for this but that feels outside of the scope of this project tbh.

danielcondemarin avatar Nov 04 '20 18:11 danielcondemarin

Deleting the cloudfront distribution should not be a problem. It is usually disabled in ~3 minutes. We're using the following snippet to delete it:

aws cloudfront wait distribution-deployed --id $DISTRIBUTION_ID
ETAG=$(aws cloudfront get-distribution-config --id $DISTRIBUTION_ID | jq -r '.ETag')
aws cloudfront delete-distribution --id $DISTRIBUTION_ID --if-match $ETAG

This will delete the CF distribution and trigger the cleanup of replicated lambdas and help people avoid the quota limit on number of cloudfront distributions associated with Lambda@Edge (25 afaik).

As for the cleanup of deleting lambdas, I'm still investigating a good solution.

Meemaw avatar Nov 10 '20 18:11 Meemaw

@Meemaw did you got any solution so far for deleting lambdas

Hamza2080 avatar Apr 22 '22 21:04 Hamza2080