7
votes

While trying to start and stop RDS instances with boto3 on AWS Lambda, I am getting an interesting error - 'RDS' object has no attribute 'stop_db_instance': AttributeError

Even the simplest code throws this error, e.g.

import boto3

def lambda_handler(event, context):    
    boto3.client('rds').stop_db_instance(DBInstanceIdentifier='myInstanceID')

I am using python3.6 runtime, so as per information available on this page, boto3 1.4.4 should be available (which I assume already has proper methods - https://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.stop_db_instance)

Any suggestions are appreciates!

4

4 Answers

9
votes

I was using boto3==1.4.1 and botocore==1.4.64 and receving same error as yours both locally and on lambda.

AWS Lambda must be using old botocore library. I tried using boto3==1.4.4 and botocore==1.5.75and it worked.

Therefore, decided to upload my own zip containing latest boto3 and botocore(mentioned above) and it works.

Creating a Deployment Package

UPDATE

Here is my aws lambda code snippet -

 import botocore
 import boto3

 def lambda_handler(event, context):
    print("Version is {}".format(botocore.__version__))
    boto3.client('rds').stop_db_instance(DBInstanceIdentifier='myInstanceID')

output: Version is 1.5.52

and 1.5.52 is responsible for not having stop_db_instance attribute in rds module. So, manually creating zip having the latest version will do the trick .

Thanks

0
votes

I think, you are missing creating a session in boto3 first. So ideally your code should look like

Assuming you have a profile aws_profile, set or else you can create a session from boto3 documentation here: http://boto3.readthedocs.io/en/latest/reference/core/session.html

session = boto3.Session(profile_name=aws_profile)
rds_client = session.client('rds')
rds_client.stop_db_instance(DBInstanceIdentifier='myInstanceID')
0
votes

Thanks for this solution!

I'm using lambci/docker-lambda with Docker to test my lambda functions, and just like real lambda, the botocore is currently outdated. To add botocore to your lambda project:

pip install botocore -t /your/project/dir

In case you're working on Mac OSX and installed pip using brew, the -t won't work. Execute the following command where your lambda_function.py is located, and you're good to go.

docker run -v "$PWD":/localdir python:2.7-alpine pip install botocore -t /localdir

-1
votes

Have you tried declaring rds explicitly:

import boto3

rds = boto3.client('rds')
rds.stop_db_instance(DBInstanceIdentifier='myInstanceID')

Do you still get the "RDS object has no attribute 'stop_db_instance': AttributeError" error.