Nihar Mitra
Nihar Mitra

Reputation: 13

Can't create s3 resource/client in boto3

EDIT: I believe this traceback stems from some sort of issue with the dependencies. Using pip to upgrade the packages didn't work but I created a new folder and installed from scratch and that worked out

I'm a Python novice so I'm struggling to debug an AWS Lambda I'm writing.

I've narrowed it down to this line of code s3_client = botoSession.resource('s3'), which is giving a long traceback with Syntax Error: invalid syntax. The botoSession variable is just for the credentials - botoSession = boto3.session.Session(aws_access_token, aws_secret_access_token).

I've also tried s3_client = boto3.client('s3'), s3_client = boto3.resource('s3'), s3_client = botoSession.resource('s3').

When I used botoSession.client('ses', region) I had no issues sending emails.

I found Error: client = boto3.client('s3') | AWS Elastic Beanstalk Worker Environment which appeared to be a similar issue, but it appeared to be fairly old and I wasn't able to figure out what the solution was. I tried adding import sys sys.path = [p for p in sys.path if not p.endswith('futures-3.0.3-py3.4.egg')] to the top of my file which didn't seem to work.

The entire traceback is as follows:

Traceback (most recent call last):
  File "smartsheetExporter.py", line 45, in <module>
    s3_client = botoSession.resource('s3')
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/session.py", line 389, in resource
    aws_session_token=aws_session_token, config=config)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/session.py", line 263, in client
    aws_session_token=aws_session_token, config=config)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/session.py", line 836, in create_client
    client_config=config, api_version=api_version)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/client.py", line 65, in create_client
    cls = self._create_client_class(service_name, service_model)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/client.py", line 90, in _create_client_class
    base_classes=bases)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/hooks.py", line 227, in emit
    return self._emit(event_name, kwargs)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/hooks.py", line 210, in _emit
    response = handler(**kwargs)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/utils.py", line 61, in _handler
    module = import_module(module)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/utils.py", line 52, in import_module
    __import__(name)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/s3/inject.py", line 15, in <module>
    from boto3.s3.transfer import create_transfer_manager
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/s3/transfer.py", line 127, in <module>
    from s3transfer.exceptions import RetriesExceededError as \
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/s3transfer/__init__.py", line 134, in <module>
    import concurrent.futures
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/concurrent/futures/__init__.py", line 8, in <module>
    from concurrent.futures._base import (FIRST_COMPLETED,
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/concurrent/futures/_base.py", line 381
    raise exception_type, self._exception, self._traceback
                        ^
SyntaxError: invalid syntax

Upvotes: 1

Views: 5579

Answers (4)

I had the same problem with python 3.6 and AWS Lambda.

I found another answer that helped me here.

You should use futures==2.2.0

Upvotes: 0

Justin Rice
Justin Rice

Reputation: 1301

I just had this same issue with boto3 and ended up having to downgrade the Python version that my lambda was running from Python 3.6 to Python 2.7. If you're using Serverless Framework for this, your serverless.yml file looks like this.

provider:
  name: aws
  runtime: python3.6
  memorySize: 3008

cool_function:
    name: cool-function
    description: This lambda goes and performs magic.
    handler: cool_function.lambda_handler
    runtime: python2.7
      - schedule:
          rate: rate(4 hours)
    timeout: 180

Upvotes: 0

John Rotenstein
John Rotenstein

Reputation: 269330

Whenever strange things are happening, it's always a good idea to update things:

sudo pip install pip --upgrade
sudo pip install boto --upgrade
sudo pip install boto3 --upgrade
sudo pip install awscli --upgrade

If you're using Python 3, try pip3 instead of pip.

Upvotes: 2

John Rotenstein
John Rotenstein

Reputation: 269330

If you are running your code on an Amazon EC2 instance with a Role assigned to the instance, then you only need this:

import boto3
s3_client = boto3.client('s3')
s3_resource = boto3.resource('s3') # Pick whichever is wish to use

If you are not on an Amazon EC2 instance, this works:

import boto3
session = boto3.Session(aws_access_key_id='AKIAxxx',aws_secret_access_key='yyy')
s3_client = session.client('s3')
s3_resource = session.resource('s3')

Of course, you should never put your credentials in the code file. Instead, put them in a credentials file (easiest is via aws configure) or in Environment Variables. That way, they won't be copied into any code repository.

See: Boto3 credentials

Upvotes: -1

Related Questions