简体   繁体   English

为什么 AWS Lambda 会因凭证错误而超时?

[英]Why does AWS Lambda time out with bad credentials?

While debugging Python 3 code in AWS Lambda, I notice that it times out a lot.在 AWS Lambda 中调试 Python 3 代码时,我注意到它超时很多。 It seems like this happens when I make an unauthorized API call or a resource doesn't exist.当我进行未经授权的 API 调用或资源不存在时,似乎会发生这种情况。 For example, a simple Lambda function:例如,一个简单的 Lambda 函数:

import boto3
def lambda_handler(event, context):
  aws = boto3.Session(region_name='us-west-1')
  s3 = aws.resource('s3')
  obj = s3.Object('mybucket', 'secretfile.xml')
  print(obj.get())

On my local machine, the code throws an exception:在我的本地机器上,代码抛出异常:

botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the GetObject operation: Access Denied

So why does Lambda time out instead of throwing the same exception?那么为什么 Lambda 超时而不是抛出相同的异常呢?

Check your CloudWatch logs.检查您的 CloudWatch 日志。 Are you certain you aren't seeing different issues on localhost than on Lambda?您确定在 localhost 和 Lambda 上没有看到不同的问题吗? If it is only timing out some of the time, but not all the time, that may indicate that you have a problem with your security.如果它只是在某些时间而不是所有时间超时,则可能表明您的安全性存在问题。 Lambda uses an ephemeral port range for its calls (see https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html#nacl-ephemeral-ports ), so it is possible whatever you are calling is only allowing communication on some ports, but not all. Lambda 为其调用使用临时端口范围(请参阅https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html#nacl-ephemeral-ports ),因此无论您做什么都可以正在调用仅允许在某些端口上进行通信,但不是全部。 So when it happens to use an allowed port it works, but when it uses an disallowed port you get timeouts.因此,当它碰巧使用允许的端口时,它可以工作,但是当它使用不允许的端口时,您会超时。 You would need to check the security groups and NACLS for whatever your lambda is calling to ensure that they allow the full ephemeral port range Lambda may use for calls.您需要检查安全组和 NACLS 以了解您的 lambda 正在调用的任何内容,以确保它们允许 Lambda 可能用于调用的完整临时端口范围。

Many boto3 calls will retry with exponential backoff, and that could contribute to timeouts ( https://github.com/boto/botocore/issues/864 may give some insights)许多 boto3 调用将重试指数退避,这可能会导致超时( https://github.com/boto/botocore/issues/864可能会提供一些见解)

I am making a lot of assumptions about what your lambda may be doing or calling, though, so you may need to add more detail about your function to get more precise responses.不过,我对您的 lambda 可能正在执行或调用的内容做了很多假设,因此您可能需要添加有关函数的更多详细信息以获得更精确的响应。

It is due to syntax error, refer to link这是由于语法错误,请参阅链接

Correct syntax should be:正确的语法应该是:

aws = boto3.session.Session(region_name='us-west-1')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么将 function 传递给装饰器时,AWS Lambda 会超时? - Why does AWS Lambda time out when passing a function to a decorator? 为什么 pyppeteer 需要这么长时间才能在 AWS Lambda 上加载单个网页 - Why does pyppeteer take such a long time to load a single webpage on AWS Lambda 将凭证从 Bitbucket 存储并传递到 AWS Lambda - Storing and passing credentials to AWS Lambda from Bitbucket 为什么此 AWS Lambda 函数返回 JSON 而不是 HTML? - Why does this AWS Lambda function return JSON instead of HTML? 为什么 AWS Lambda 找不到我的 main.py? - Why does AWS Lambda not able to find my main.py? 为什么带有 Chrome 驱动程序的 Selenium 在本地工作但在 AWS Lambda 上崩溃? - Why does Selenium with Chrome driver work locally but crashes on AWS Lambda? Python - 为什么 aws lambda 如此缓慢地运行多个线程? - Python - Why does aws lambda run multiple threads so slowly? 为什么我会收到“错误的处理程序 AWS Lambda - 没有足够的值来解包”错误? - Why do I get a "Bad handler AWS Lambda - not enough values to unpack" error? 如何(正确)在AWS Lambda函数中使用外部凭据? - How to (properly) use external credentials in an AWS Lambda function? AWS Lambda Python / Boto3 / psycopg2 Redshift临时凭证 - AWS Lambda Python/Boto3/psycopg2 Redshift temporary credentials
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM