在AWS Lambda中的python3虚拟环境中使用boto3

时间:2016-08-19 20:58:40

标签: virtualenv python-3.4 aws-lambda boto3

我正在尝试使用 Python3.4 boto3 走一个 S3 存储桶并将一些文件位置发布到 RDS 实例。我遇到麻烦的部分工作是使用boto3。我的lambda函数如下所示:

import subprocess

def lambda_handler(event, context):
    args = ("venv/bin/python3.4", "run.py")
    popen = subprocess.Popen(args, stdout=subprocess.PIPE)
    popen.wait()
    output = popen.stdout.read()
    print(output)

并且,在我的 run.py 文件中,我有一些行:

import boto3

s3c = boto3.client('s3')

导致异常。然而, run.py 文件与此问题无关,所以为了使这篇文章更简洁,我发现执行lambda函数会产生此错误的原因:

import subprocess

def lambda_handler(event, context):
    args = ("python3.4", "-c", "import boto3; print(boto3.client('s3'))")
    popen = subprocess.Popen(args, stdout=subprocess.PIPE)
    popen.wait()
    output = popen.stdout.read()
    print(output)

我的日志流报告错误:

Event Data
START RequestId: 2b65421a-664d-11e6-81db-974c7c09d283 Version: $LATEST 
Traceback (most recent call last): 
 File "<string>", line 1, in <module> 
 File "/var/runtime/boto3/__init__.py", line 79, in client 
 return _get_default_session().client(*args, **kwargs) 
 File "/var/runtime/boto3/session.py", line 250, in client 
 aws_session_token=aws_session_token, config=config) 
 File "/var/runtime/botocore/session.py", line 818, in create_client 
 client_config=config, api_version=api_version) 
 File "/var/runtime/botocore/client.py", line 63, in create_client 
 cls = self._create_client_class(service_name, service_model) 
 File "/var/runtime/botocore/client.py", line 85, in _create_client_class 
 base_classes=bases) 
 File "/var/runtime/botocore/hooks.py", line 227, in emit 
 return self._emit(event_name, kwargs) 
 File "/var/runtime/botocore/hooks.py", line 210, in _emit 
 response = handler(**kwargs) 
 File "/var/runtime/boto3/utils.py", line 61, in _handler 
 module = import_module(module) 
 File "/var/runtime/boto3/utils.py", line 52, in import_module 
 __import__(name) 
 File "/var/runtime/boto3/s3/inject.py", line 13, in <module> 
 from boto3.s3.transfer import S3Transfer 
 File "/var/runtime/boto3/s3/transfer.py", line 135, in <module> 
 from concurrent import futures 
 File "/var/runtime/concurrent/futures/__init__.py", line 8, in <module> 
 from concurrent.futures._base import (FIRST_COMPLETED, 
 File "/var/runtime/concurrent/futures/_base.py", line 357 
 raise type(self._exception), self._exception, self._traceback 
 ^ 
SyntaxError: invalid syntax 
END RequestId: 2b65421a-664d-11e6-81db-974c7c09d283 
REPORT RequestId: 2b65421a-664d-11e6-81db-974c7c09d283  Duration: 2673.45 ms    Billed Duration: 2700 ms Memory Size: 1024 MB   Max Memory Used: 61 MB  

我需要在run.py的下游使用boto3。任何关于如何解决这个问题的想法都非常感谢。谢谢!

0 个答案:

没有答案