AWS Lambda 导入错误: 无法导入模块 “lambda_function”

huangapple go评论84阅读模式
英文:

AWS Lambda import error: Unable to import module "lambda_function"

问题

I am trying to run a simple program in AWS Lambda. However, it is failing with DEFAULT_CIPHERS error. When I run the following program it fails with error ...unable to import lambda function:

import json
import boto3
import os
import requests

def lambda_handler(event, context):
    file = '2011-02-12-0.json.gz'
    download_res = requests.get(f"https://data.gharchive.org/{file}")
    print(f"Files downloaded successfully - Filename: {file}")
    os.environ.setdefault('AWS_PROFILE', 'myid')
    s3 = boto3.client('s3')
    upload_res = s3.put_object(Bucket='my-demo-bucket3', Key=file, Body=download_res.content)
    print("Files Uploaded successfully")
    return upload_res

Error:

Response
{
  "errorMessage": "Unable to import module 'lambda_function': cannot import name 'DEFAULT_CIPHERS' from 'urllib3.util.ssl_' (/var/task/urllib3/util/ssl_.py)",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "5d82baed-2ee6-41cd-a8e7-d24a59061dbc",
  "stackTrace": []
}

Can anyone please help me sort this out.
Thanks.

英文:

I am trying to run a simple program in AWS Lambda. However, it is failing with DEFAULT_CIPHERS error. When I run the following program it fails with error ...unable to import lambda function:

import json
import boto3
import os
import requests

def lambda_handler(event, context):
    file = '2011-02-12-0.json.gz'
    download_res = requests.get(f"https://data.gharchive.org/{file}")
    print(f"Files downloaded successfully - Filename: {file}")
    os.environ.setdefault('AWS_PROFILE', 'myid')
    s3 = boto3.client('s3')
    upload_res = s3.put_object(Bucket='my-demo-bucket3', Key=file, Body=download_res.content)
    print("Files Uploaded successfully")
    return upload_res

Error:

Response
{
  "errorMessage": "Unable to import module 'lambda_function': cannot import name 'DEFAULT_CIPHERS' from 'urllib3.util.ssl_' (/var/task/urllib3/util/ssl_.py)",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "5d82baed-2ee6-41cd-a8e7-d24a59061dbc",
  "stackTrace": []
}

Can anyone please help me sort this out.
Thanks.

答案1

得分: 1

问题出在软件包的版本问题。这两段代码的不同之处在于导致问题的boto3。在GitHub上有一个与此相关的已打开问题。解决方法,正如几个人在那里写的那样,是锁定urllib3的版本。可以在你的requirements.txt中添加类似以下内容:

requests
urllib3<2

第二个选项是将requests软件包锁定到2.29.0版本,就像有两个人提到的,这可以解决他们Lambda函数的问题。

要跟踪这个问题的进展,可以查看Github上的问题。

英文:

The issue is in the versioning of the packages. The difference in these 2 codes that makes the problem is boto3. There's an open issue on GitHub related to this.
A solution, as several people wrote there would be to pin down the version of urllib3

You can put something like this in your requirements.txt

requests
urllib3<2

The second option is to pin down the requests package to 2.29.0, as 2 guys mentioned this fixed issue with their Lambda functions.

To track the progress of this, you can check the issue in Github

huangapple
  • 本文由 发表于 2023年5月22日 14:39:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定