AWS Lambda是否收取初始化代码所花费的时间? [英] Does AWS Lambda charge for the time spent initializing code?

查看:136
本文介绍了AWS Lambda是否收取初始化代码所花费的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我用Python编写的Lambda函数需要1.8秒的初始化时间(在冷启动期间)和400 ms的执行时间,那么我需要为400 ms的执行时间或整个2.2秒的初始化时间+执行时间付费吗?

If my Lambda function written in Python takes 1.8 seconds to initialize (during a cold start) and 400 ms to execute, am I charged for the 400 ms execution time or the entire 2.2 seconds of initialization + execution time?

从X射线中,我看到:

从CloudWatch日志中,我看到:

From CloudWatch logs, I see:

Duration: 404.42 ms Billed Duration: 500 ms Memory Size: 448 MB Max Memory Used: 113 MB

我从中了解到,我需要为500毫秒的执行时间付费,这是否意味着代码初始化(例如,导入内容)是免费的?

What I understand from this is that I was billed for 500ms of execution time, so does that mean code initialization (e.g. importing stuff) is free?

推荐答案

因此,我决定尝试通过一些实验自己弄清楚.我使用Python 2.7创建了Lambda函数,具有 128 MB RAM 15秒超时并启用了主动跟踪.我修改了示例代码,在import语句之后添加了10秒的睡眠时间:

So I decided to try and figure it out myself with a little experiment. I created a Lambda function using Python 2.7 with 128 MB of RAM, timeout of 15 seconds and active tracing enabled. I modified the sample code to add a 10 second sleep right after the import statement:

print "starting import"
import json
from time import sleep
sleep(10)
print "calling handler"

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

由于Lambda开始变冷,我在X射线输出中看到了这一点:

Since the Lambda started cold, I saw this in the X-ray output:

我在CloudWatch日志中看到了这一点:

And I saw this in CloudWatch logs:

22:06:47 starting import
22:06:57 calling handler
22:06:58 START RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Version: $LATEST
22:06:58 starting import
22:07:08 calling handler
22:07:08 END RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
22:07:08 REPORT RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Duration: 10022.57 ms   Billed Duration: 10100 ms Memory Size: 128 MB   Max Memory Used: 19 MB

该功能实际上运行了 TWICE .第一次休眠10秒钟后,它在调用处理程序方法时重新启动,基本上需要20秒钟才能完成执行,但要向我收取10秒钟的费用.

The function actually ran TWICE. After sleeping for 10 seconds the first time, it re-started when the handler method was called, essentially taking 20 seconds to finish execution but billing me for 10 seconds.

我又跑了一次,这一次热身,我明白了:

I ran it again, this time a warm-start and I got this:

X射线输出(热启动):

X-ray output (warm start):

CloudWatch日志(热启动):

CloudWatch logs (warm start):

22:23:16 START RequestId: yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy Version: $LATEST
22:23:16 END RequestId: yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy
22:23:16 REPORT RequestId: yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy Duration: 6.97 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 29 MB

那里没有可疑的东西.我将功能内存增加到192 MB,将其保存并恢复为128 MB,然后再次保存以确保它再次开始冷启动并再次调用它. X射线的输出与以前相同,但是CloudWatch日志有一些有趣的东西:

Nothing suspicious there. I increased the function memory to 192 MB, saved it and reverted it back to 128 MB and saved it again to ensure that it'd start cold again and invoked it once again. The output of X-ray was the same as before but CloudWatch logs had something interesting:

22:30:13 starting import
22:30:24 START RequestId: zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz Version: $LATEST
22:30:24 starting import
22:30:34 calling handler
22:30:34 END RequestId: zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz
22:30:34 REPORT RequestId: zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz Duration: 10010.85 ms   Billed Duration: 10100 ms Memory Size: 128 MB   Max Memory Used: 19 MB

在我的代码处于休眠状态10秒钟时, Lambda切断了它并重新启动了它.执行时间再次是20秒,但我被收取了10秒的费用.因此,我想如果我不添加1个睡眠语句,而是添加15个一秒钟的睡眠,怎么办?

It seems while my code was in the middle of sleeping for 10 seconds, Lambda cut it off and re-started it. The execution time was again 20 seconds but I was billed for 10 seconds. So I thought what if instead of 1 sleep statement, I add 15 one second sleeps?

更新的代码:

print "starting import"
import json
from time import sleep
for i in range(1, 16):
    sleep(1)
    print "completed {}th sleep".format(i)

print "calling handler"
def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

该功能超时!

X射线输出:

CloudWatch日志:

CloudWatch logs:

22:51:54 starting import
22:51:55 completed 1th sleep
22:51:56 completed 2th sleep
22:51:57 completed 3th sleep
22:51:58 completed 4th sleep
22:51:59 completed 5th sleep
22:52:00 completed 6th sleep
22:52:01 completed 7th sleep
22:52:02 completed 8th sleep
22:52:03 completed 9th sleep
22:52:04 START RequestId: 11111111-1111-1111-1111-111111111111 Version: $LATEST
22:52:04 starting import
22:52:05 completed 1th sleep
22:52:06 completed 2th sleep
22:52:07 completed 3th sleep
22:52:08 completed 4th sleep
22:52:09 completed 5th sleep
22:52:10 completed 6th sleep
22:52:11 completed 7th sleep
22:52:12 completed 8th sleep
22:52:13 completed 9th sleep
22:52:14 completed 10th sleep
22:52:15 completed 11th sleep
22:52:16 completed 12th sleep
22:52:17 completed 13th sleep
22:52:18 completed 14th sleep
22:52:19 END RequestId: 11111111-1111-1111-1111-111111111111
22:52:19 REPORT RequestId: 11111111-1111-1111-1111-111111111111 Duration: 15015.16 ms   Billed Duration: 15000 ms Memory Size: 192 MB   Max Memory Used: 19 MB
22:52:19
2019-03-29T22:52:19.621Z 11111111-1111-1111-1111-111111111111 Task timed out after 15.02 seconds
22:52:19 starting import
22:52:20 completed 1th sleep
22:52:21 completed 2th sleep
22:52:22 completed 3th sleep
22:52:23 completed 4th sleep
22:52:24 completed 5th sleep
22:52:25 completed 6th sleep
22:52:26 completed 7th sleep
22:52:27 completed 8th sleep
22:52:28 completed 9th sleep
22:52:29 completed 10th sleep

它实际上执行了25.8秒,但随后超时并向我收取了15秒的费用.在调用处理程序之前执行的代码运行了大约9秒钟,然后Lambda将其切断并重新启动该函数,但未完成,最终在25.8秒后超时.如果我将Lambda超时增加到16秒,它会在25.8秒内完成执行(如X射线所示),并向我收费15100毫秒.

It actually executed for 25.8 seconds but then timed out and billed me for 15 seconds. The code that executes before the handler is called ran for about 9 seconds then Lambda cut it off and re-started the function but didn't finish and ultimately timed out after 25.8 seconds. If I increase the Lambda timeout to 16 seconds, it finished executing in 25.8 seconds (as shown in X-Ray) and billed me for 15100 ms.

所以这使我相信如果在初始化后约9-10秒内未调用处理程序函数,Lambda将重新启动该函数.那么,如果代码初始化花费的时间少于10秒怎么办?

So this leads me to believe that if the handler function isn't called within about 9-10 seconds after initialization, Lambda will restart the function. So what if the code initialization takes less than 10 seconds?

更新的代码:

print "starting import"
import json
from time import sleep
for i in range(1, 10):
    sleep(1)
    print "completed {}th sleep".format(i)

print "calling handler"
def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

我将此功能冷运行了大约10次,并且计费时间始终为100毫秒. 我什至将我的lambda超时更改为1秒,它仍然成功执行完毕!

I ran this function cold for about 10 times and my billed duration was always 100 ms. I even changed my lambda timeout to 1 second and it still finished executing successfully!

X射线输出:

CloudWatch日志:

CloudWatch logs:

23:23:43 starting import
23:23:44 completed 1th sleep
23:23:45 completed 2th sleep
23:23:46 completed 3th sleep
23:23:47 completed 4th sleep
23:23:48 completed 5th sleep
23:23:49 completed 6th sleep
23:23:50 completed 7th sleep
23:23:51 completed 8th sleep
23:23:52 completed 9th sleep
23:23:52 calling handler
23:23:52 START RequestId: 22222222-2222-2222-2222-222222222222 Version: $LATEST
23:23:52 END RequestId: 22222222-2222-2222-2222-222222222222
23:23:52 REPORT RequestId: 22222222-2222-2222-2222-222222222222 Duration: 0.73 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 44 MB

正如 Steve HOUEL 正确指出的那样,这使我相信 Lambda不会对您初始化代码(例如导入东西)的时间收取费用,只要它在大约9秒内完成.但是,如果花费的时间更长,Lambda将重新启动您的函数,并假设您设置了足够大的超时,则函数执行有效会花费10秒+常规的冷启动执行时间,但您仍需为此付费冷启动执行时间,不增加10秒.

As Steve HOUEL rightfully pointed out, this leads me to believe that Lambda won't charge you for the time it takes to initialize your code (e.g. importing stuff) as long as it finishes in about 9 seconds. However, if it takes longer than that, Lambda re-starts your function and assuming you set a large enough timeout, function execution effectively takes 10 seconds + regular cold start execution time but you are still billed for just the cold start execution time without the added 10 seconds.

这篇关于AWS Lambda是否收取初始化代码所花费的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆