41

This question already exists here but I think it will have more impact on SO.

I created an AWS Lambda@Edge function in order to rewrite Cloudfront URLs before they reach the Origin.
AWS Lambda@Edge Function are automatically replicated through all regions when published, so I was not surprised to see this in every region:

enter image description here

Here began the problems:
I deleted the Lambda@Edge because it was a test and I wanted to start a fresh new one, but the replicas weren't deleted at the same time.
It was no big deal at the time, I thought that I simply could create a new Lambda@Edge function.
But you can't because the Cloudfront trigger can only be used one function at a time (as the replicas use it, you cannot create a new one).
Moreover, the trigger cannot be deleted either.

So now I'm stuck with Lambda@Edge replicas everywhere that I cannot delete and I cannot create similar ones.


TL;DR

  • I created a Lambda@Edge with a CloudFront trigger
  • I deleted it to create a new (similar) one
  • Now there is a replica still existing
  • I can't delete the replica
  • I can't create a new Lambda@Edge because the trigger is already "in use" by the replica (that I can't delete)

I'd be grateful to get some help on this
François


EDIT:

I definitely think it's a bug because in my replica's page, the link to the Master ARN responds with a 404.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Francois
  • 1,367
  • 1
  • 15
  • 23
  • Did you try to delete trigger in the source lambda function? I can see triggers in source lambda function specified by version qualifier, and delete works. However, even the trigger is deleted, the replica lambda functions are still there. – Yu Liang Jul 25 '17 at 08:30
  • By `source lambda function`, I guess you mean the Lambda@Edge I created at first. That's the one I deleted so I cannot delete the trigger as the Lambda itself doesn't exist anymore... – Francois Jul 25 '17 at 08:39

8 Answers8

33

I found part of an answer:

It turns out that I needed to delete the trigger in the behavior of Cloudfront.
Simply:

  • Go to your Cloudfront distribution's behaviors
  • Check the one triggering the LambdaEdge
  • Click Edit
  • Go at the very bottom of the page and click the X to delete the trigger

Still, I can't delete the replicas but at least I can create new lambdas for this trigger...

Francois
  • 1,367
  • 1
  • 15
  • 23
  • Yes, this is the only solution for now. – rfmoz Aug 29 '17 at 07:45
  • 4
    So I discussed this with an Architect at the AWS summit. Your solution is correct (i.e. delete the trigger in every behaviour it's associated with). You won't be able to delete the function immediately (as it's replicated it takes time to remove this association), however if you give it 24 hours for this to propagate you'll then be able to delete the function. – James Lelyveld May 18 '18 at 09:37
  • 3
    I had some Lambdas where the CloudFront distribution had been deleted for a week, but I still could not delete them. I ended up making a new CloudFront distribution, adding that as a trigger, then deleting the trigger. Then after the distribution had been updated, I could delete the Lambdas – MGH Aug 16 '18 at 12:53
  • 12
    What do you do if Cloudformation has already removed the Cloudfront distribution? – badsyntax Dec 27 '20 at 08:30
  • Other than wait, what can you do if the likes of Terraform have already removed the CloudFront distributions? – UrbanConor Apr 08 '22 at 08:56
10

There is now a documentation here stating that replicas are deleted after a "few hours" - which would imply that I then can also delete the Lambda function.

Update 2020-01-05: If you delete the Lambdas it takes ~1 h and everything is gone! So it is just slower but behave similar to normal Lambdas?

lony
  • 6,733
  • 11
  • 60
  • 92
  • 2
    If only this were true *all* the time. I've had this both work and not work. For example, I have an L@E function that hasn't been associated with a CF distribution for over 24 hrs, and I still can't delete it! – KJH Jan 31 '19 at 02:19
  • Sad to hear, for me it worked after 1-2 hours always so far. Have to admit I only have users in Europe. – lony Jan 31 '19 at 08:38
  • In my particular scenario I deleted the Cloudfront distribution altogether, so there was no way for me to modify its configuration as some of the other answers suggest. Thankfully, I just had to wait for about an hour and I was able to delete the associated Edge@Lambda. – Fearnbuster Jan 13 '22 at 22:01
  • Just to note, sometimes deletion can be quite quick, far less than ~1hr. So it's worth trying to delete early when you can – UrbanConor Apr 08 '22 at 09:05
5

I asked this problem to AWS support and its answer was like: "you cannot delete replicated lambda functions for now, but we understand many people requires this".

AWS Lambda@Edge http://docs.aws.amazon.com/es_es/lambda/latest/dg/lambda-edge.html

When you create a trigger, Lambda replicates the function to AWS Regions and CloudFront edge locations around the globe. Note that replicas can't be edited or deleted.

reki
  • 131
  • 5
  • You're right I read that in the documentation. Although, maybe they should explain how to delete a trigger in a behavior in order to be able to recreate a lambda on that trigger. – Francois Aug 02 '17 at 13:53
5

Hi francois, yves, reki I have figured out the solution to delete Lambda@edge replica.

  1. Firstly, Login to CloudFront Console and go to your Distribution.
  2. Under the Behaviors Tab - tick the listed Behavior and edit
  3. Scroll down to Lambda Function Associations and remove any Association by clicking the X.
  4. Press yes,edit to save the changes. --- Now that you have removed the Associations it's time to delete the Lambda@edge replicas
  5. Go to Lambda Console and open your lambda( you wish to delete).
  6. On the top menus - Qualifiers -> Versions-> choose the listed drop-down version enter image description here
  7. It will open that @edgeLambda Version
  8. On the top menus - Actions -> Delete version enter image description here
  9. This way , deleting all the versions - you are left with $LATEST
  10. Deleting that also - you are finally able to delete the Lambda@edge Function

Note!> Please remember to delete any IAM Roles and Permissions associated with Lambda@edge Functions.

I hope this will works for you, Enjoy :)

DHEERAJ
  • 571
  • 6
  • 9
  • 1
    I'm stuck being unable to delete the latest and the $LATEST versions as it keeps saying me cannot delete a replicated version :( – Pherrymason Jun 17 '20 at 06:04
  • Switching to the specific version still returns the same error. I have no CF distributions left and it's been 5 hours – Titan Jan 06 '21 at 15:15
1

Even I tried * To delete trigger then create lambda (in my case lambda reaches nearly 30)*,but i couldnt create new lambda@edge. Finally i did this

Removed the cloud-front distribution which is associated with lambda function, now its simple to move further. Please assure that distribution only used by that related lambda.

1

Best answer I have found to be able to delete the function, follow the steps below:

  1. For every version of the deployed function, delete the triggers of that specific function.
  2. Wait several hours until AWS will automatically delete all deployed replicas of that specific function.
  3. Once all replicas are automatically deleted, try again to delete the Lambda function. You should succeed.

Source: https://adrian.simionov.io/aws/2019/08/28/deleting-lambda-at-edge-functions.html

1

I had a similar issue where my Lambda@Edge did not have any Cloudfront triggers at all; however, I still wasn't able to delete it. I tried the following and it helped,

  • Create a new Cloudfront distribution, and associate your Lambda@Edge with this new distribution.
  • Wait for the distribution to be fully deployed.
  • Remove the association of your Lambda@Edge from the Cloudfront distribution that you just created.
  • Wait for the distribution to be fully deployed.
  • Additionally, wait for a few more minutes.
  • Then, try to delete your Lambda@Edge.
Ali Ameer
  • 49
  • 3
0

I encountered a similar problem, but resolved it by waiting a few minutes for the removal of replicas, as AWS takes some time to fully remove all replicas. After the process completed, I was able to successfully delete the edge lambda