1 Answer
- Newest
- Most votes
- Most comments
1
Hello.
Try your Python code as below.
The Lambda function executes the handler method when the function is called, so you need to define the function as follows.
https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html
#!/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 like your shell script
print(len(rds_instances['DBInstances']))
#Just a little additional code to show you how to access the results from the describe_db_instances call:
if len(rds_instances['DBInstances']) != 0:
for db_instance in rds_instances['DBInstances']:
db_engine = db_instance.get('Engine')
print(db_engine)
Relevant content
- asked 9 months ago
- asked 4 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated a year ago
I did add that new line after client variable statement but I get a runtime error now. See below. <<< def lambda_handler(event, context): >>>>
I also tried addding the "Return" statement at the end but still did not resolve error.
====================================== Test Event Name RDS-Event
Response { "errorMessage": "Syntax error in module 'lambda_function': expected an indented block after function definition on line 7 (lambda_function.py, line 9)", "errorType": "Runtime.UserCodeSyntaxError", "requestId": "32bc3e87-6cd7-4524-9c4e-2f2a45d21dab", "stackTrace": [ " File "/var/task/lambda_function.py" Line 9\n rds_instances = client.describe_db_instances(Filters=[{'Name':'engine', 'Values':['oracle-ee']}])\n" ] }
Function Logs START RequestId: 32bc3e87-6cd7-4524-9c4e-2f2a45d21dab Version: $LATEST [ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': expected an indented block after function definition on line 7 (lambda_function.py, line 9) Traceback (most recent call last): File "/var/task/lambda_function.py" Line 9 rds_instances = client.describe_db_instances(Filters=[{'Name':'engine', 'Values':['oracle-ee']}])END RequestId: 32bc3e87-6cd7-4524-9c4e-2f2a45d21dab REPORT RequestId: 32bc3e87-6cd7-4524-9c4e-2f2a45d21dab Duration: 1.75 ms Billed Duration: 2 ms Memory Size: 128 MB Max Memory Used: 34 MB
Request ID 32bc3e87-6cd7-4524-9c4e-2f2a45d21dab
There seems to be an indentation problem in the error. However, I was unable to reproduce the same error even when running it with my AWS account. So please make sure you have copied and pasted my code correctly.
You are correct. It is strange that when I copy/paste your code which is exactly what I had it worked. Can I ask what permission roles did you grant the IAM role attached to Lambda so it can run the function? I used the "AdministratorAccess" but that is not secure. I tried "LambdaFullAccess" role before but it gave me permissions issue.
Python is a programming language that divides blocks by indentation, so if the indentation is even slightly off, an error will occur.
We have set permissions to output logs from Lambda to CloudWatch Logs and permissions to view RDS instances.
Are you suggesting to create a custom policy you listed and assign it to a new role for lambda access instead of using the ones provided by amazon?
Does the policy depend on which region the service is running that needs to be updated because i see "ap-northeast"