简体   繁体   中英

How to kill/terminate a running AWS Lambda function?

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?

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

eg

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

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

为了停止我的 Lambda 函数处于无限循环中,因为我在 S3 存储桶内 ListObjects 的迭代逻辑中出错,我只是更改了 IAM 策略,该策略允许 Lambda 函数访问 S3 存储桶,导致它失败并因此中断在我的无限循环中,该循环旨在在遇到错误时从 Lambda 函数返回。

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:

在此处输入图片说明

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

在此处输入图片说明

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.

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,
)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM