35

I have a lambda function that runs over a period of ~10mins. Decently long for a Lambda but given our current needs/setup it's the simplest way for it to do what we need it to do.

However, a recent bug in our Lambda code made the lambda go haywire and basically DOS our own server. This is where I realized I have no idea how to kill this process if ever I need to (vs just wait for it to end/timeout). so...

Is there a way to do terminate a running lambda process from the AWS console? Is there a way to do it via AWS CLI?

1
  • 3
    Max run time for a Lambda = 15 minutes, so even if it's infinitely looping or hung, it only gets 15 minutes to make a mess of things.
    – fusion27
    Nov 5, 2020 at 22:19

8 Answers 8

41

There's no way to kill a running lambda. However, you can set concurrency limit to 0 to stop it from starting any more executions

e.g.

$ aws lambda put-function-concurrency --function-name my-function --reserved-concurrent-executions 0

https://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html

2
  • 1
    What's the default concurrency limit? (Edit, nvm found that it's 1000 by default) Sep 27, 2020 at 18:53
  • 8
    While this answer is true and will work for future invocations of the function... To kill currently running Lambdas you should DELETE the function. This is the only way to kill CURRENTLY RUNNING Lambdas. If you are using Infra-as-code then recreating the Lambda later should not be an issue. Jul 28, 2021 at 17:59
21

Following on from ubi's answer, you can also set the concurrency via the AWS console. Go to the Lambda you want to stop and click on the Throttle button:

enter image description here

You will get a modal explaining the concurrency will be set to 0 and no more Lambdas will run:

enter image description here

2
  • Will this only temporarily stop the running? Because I did it yesterday, and today it's back running. Jul 7, 2022 at 10:08
  • @yoges-nsamy I'm not sure, perhaps doing a deploy from a service like SAM or Serverless Framework or other action would reset it.
    – Jeff S.
    Jul 8, 2022 at 2:39
8

To stop my Lambda function which was in an infinite loop because I had an error in its iterative logic to ListObjects within an S3 bucket, I simply altered the IAM policy that gave the Lambda function access to the S3 bucket causing it to fail and thus breaking out of my infinite loop which is designed to return from the Lambda function upon encountering an error.

3

We had a problem of a aws lambda function running continuously. Figured it only after a week, by the time it was a huge bill. Setting concurrency to zero stopped it temporarily, but it was back when the concurrency was set back to normal. But a simple trick saved us, we revoked the active sessions, which stopped all the function calls. (Configurations>Permissions>Execution role) It attaches an inline policy named AWSRevokeOlderSessions to the role. Remove this policy after the job is done. Be careful with this as it stops all active sessions of all users logged in. Do it only in no use time. Also, if you don't remove the revoke policy it creates problems in new sessions too, though it is mentioned otherwise.

0

If you want to control it from a Lambda based on a alarm trigger you can use use Boto3 with Python to set the reserved concurrent to zero.

import boto3

lambda_client = boto3.client("lambda")

lambda_client.put_function_concurrency(
    FunctionName='function-name',
    ReservedConcurrentExecutions=0,
)
0

I think if you can generate a exception condition on a running lambda(before that disable the triggers) then only you can interrupt the current execution. Say, if your lambda connect to a database or read a file from storage, if you will delete that file there will be an exception, the lambda will stop.

0

As a few others have noted, my lambda did also continue to fire after we set the concurrency back to normal, however I was able to stop the loop.

Before updating the concurrency back to normal, I updated the lambda and commented out all the code that we were having trouble with. Then once we did set the concurrency back to normal, we received a few logs indicating those operations were complete and that was that.

0

Timeout feature in General Configuration of Lambda Function

I am using the Timeout feature in General Configuration.

I have a rough idea that my function takes 2 minutes to run. So, to avoid any overshoot, I am using 3 minutes and 30 seconds timeout. This way if due to some issue like importing modules, or creating unending while loops due to a mistake (during testing) my function automatically terminates in 3m30s. This has indeed helped me a lot.

If you know your function takes 10 minutes, you can use a timeout of 12-15 minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.