Boto3 start / stop RDS instance with AWS Lambda

When I try to start and stop RDS instances using boto3 on AWS Lambda, I get an interesting error - 'RDS' object has no attribute 'stop_db_instance': AttributeError

Even the simplest code causes this error, for example

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

I am using python3.6 runtime since the information available on this page should be accessible by boto3 1.4.4 (which I assume that they already have the appropriate methods - https://boto3.readthedocs.io/en/latest/reference/ services / rds.html # RDS.Client.stop_db_instance )

Any suggestions are welcome!

+5
source share
4 answers

I used boto3==1.4.1 and botocore==1.4.64 and got the same error as yours, both locally and on lambda.

AWS Lambda should use the old botocor library. I tried using boto3==1.4.4 and botocore==1.5.75 and it worked.

So I decided to download my own zip code containing the latest boto3 and botocore (mentioned above) and it works.

Create 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 1.5.52

and 1.5.52 is responsible for the absence of the stop_db_instance attribute in the rds module. So manually creating a zip with the latest version will do the trick.

thanks

+7
source

I think you are missing the session creation in boto3. Therefore, ideally, your code should look like

Assuming you have an aws_profile profile, install or you can create a session from the 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
source

Thank you for this decision!

I am using lambci / docker-lambda with Docker to test my lambda functions, and like real lambda, botocors are out of date now. To add botokur to the lambda project:

pip install botocore -t /your/project/dir

If you are using Mac OSX and installing pip using brew, -t will not work. Run the following command where your lambda_function.py is located and you are good to go.

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

0
source

You tried to explicitly declare rds:

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

You still get the "RDS object does not have the stop_db_instance" attribute: AttributeError error.

-1
source

Source: https://habr.com/ru/post/1269178/


All Articles