How fix Python Error using Boto3

0

Hi I have a Lambda function using a python script that runs a CLI query and send an email with result. I have two parts working separately but when I combine them it fails as show below. Do you know how it can be fixed?

#!/usr/bin/env python3
from boto3 import client
client = client('rds')

def lambda_handler(event, context):

    rds_instances = client.describe_db_instances(Filters=[{'Name':'engine', 'Values':['oracle-ee']}])

    #print out just the number of instances
    print(len(rds_instances['DBInstances']))

  #print out the engine
    if len(rds_instances['DBInstances']) != 0:
        for db_instance in rds_instances['DBInstances']:
            db_engine = db_instance.get('Engine')
            print(db_engine)

#Send email for result. This is where code fails after adding this section.
    ses_client = client("ses")
    subject = "Report"
    body = "Need to add the output of two print statements above with a title for each"
    message = {"Subject": {"Data":subject}, "Body":{"Html":{"Data": body}}}
    response = ses_client.send_email(Source = "mike10@gmail.com", 
                         Destination={"ToAddresses": ["mike10@gmail.com"]},
                         Message = message)
    return response

Test Event Name RDS-Event

Response { "errorMessage": "'RDS' object is not callable", "errorType": "TypeError", "requestId": "111907e9-371b-4836-b78d-3364aaa51598", "stackTrace": [ " File "/var/task/lambda_function.py", line 23, in lambda_handler\n ses_client = client("ses")\n" ] }

Function Logs START RequestId: 111907e9-371b-4836-b78d-3364aaa51598 Version: $LATEST 1 oracle-ee [ERROR] TypeError: 'RDS' object is not callable Traceback (most recent call last):   File "/var/task/lambda_function.py", line 23, in lambda_handler     ses_client = client("ses")END RequestId: 111907e9-371b-4836-b78d-3364aaa51598 REPORT RequestId: 111907e9-371b-4836-b78d-3364aaa51598 Duration: 729.27 ms Billed Duration: 730 ms Memory Size: 128 MB Max Memory Used: 78 MB Init Duration: 431.14 ms

Request ID 111907e9-371b-4836-b78d-3364aaa51598

1 Answer
1
Accepted Answer

Hello.

Please change the code as below.
The change is "rds_client = client('rds')" on the third line.
It's happening because a variable named client is already used in boto3's client function to create an RDS client.
So, setting different variable names for RDS client and SES client will resolve the error.

#!/usr/bin/env python3
from boto3 import client
rds_client = client('rds')

def lambda_handler(event, context):

    rds_instances = rds_client.describe_db_instances(Filters=[{'Name':'engine', 'Values':['oracle-ee']}])

    #print out just the number of instances
    print(len(rds_instances['DBInstances']))

  #print out the engine
    if len(rds_instances['DBInstances']) != 0:
        for db_instance in rds_instances['DBInstances']:
            db_engine = db_instance.get('Engine')
            print(db_engine)

#Send email for result. This is where code fails after adding this section.
    ses_client = client("ses")
    subject = "Report"
    body = "Need to add the output of two print statements above with a title for each"
    message = {"Subject": {"Data":subject}, "Body":{"Html":{"Data": body}}}
    response = ses_client.send_email(Source = "mike10@gmail.com", 
                         Destination={"ToAddresses": ["mike10@gmail.com"]},
                         Message = message)
    return response
profile picture
EXPERT
answered 4 months ago
profile picture
EXPERT
reviewed 4 months ago
profile pictureAWS
EXPERT
reviewed 4 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions