I have written some python code that I am running in AWS Lambda. When I test the Lambda in the Lambda AWS dashboard and it runs without any errors I see the following in the "Execution Results" tab :
Response: null
Request ID: "421fd7da-20f7-4029-aa8b-f7281e7c90d9"
If I get any errors when I run the Lambda I see JSON formated output in the "Execution Results" tab.
Here is an example :
Response:
{
"errorMessage": "An error occurred (DBClusterNotFoundFault) when calling the CreateDBClusterSnapshot operation: DBCluster not found: ernie-export-test-db-clusterr",
"errorType": "DBClusterNotFoundFault",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 90, in main\n response = create_snapshot(rds, snaptype, datestamp, deleteAfterDate)\n",
" File \"/var/task/lambda_function.py\", line 33, in create_snapshot\n 'Value': deleteafterdate\n",
" File \"/var/runtime/botocore/client.py\", line 272, in _api_call\n return self._make_api_call(operation_name, kwargs)\n",
" File \"/var/runtime/botocore/client.py\", line 576, in _make_api_call\n raise error_class(parsed_response, operation_name)\n"
]
}
How can I return this "Response" back into my Python code so I can use it/read it? In particular I want to read the "errorMessage" so my python code in the Lambda can print it out or forward it to - lets say to use in in an SNS.
I have added a return with response but its contents do not match up with what is in the response I get in the "Execution Results" tab.
Here is my Python code -
from __future__ import print_function
from boto3 import client
import datetime
from datetime import datetime
# Database cluster identifier that the backup will be performed on
CLUSTER_ID = "export-test-db-cluster"
# Name of the company that the script is used on
COMPANY = "Test"
# AWS region in which the db instances exist
REGION = "us-east-1"
def create_snapshot(rds, snaptype, datestamp, deleteafterdate):
snapname = COMPANY + "-" + snaptype + "-" + datestamp
response = rds.create_db_cluster_snapshot(
DBClusterSnapshotIdentifier=snapname,
DBClusterIdentifier=CLUSTER_ID,
Tags=[
{
'Key': 'Name',
'Value': snapname
},
{
'Key': 'expirationDate',
'Value': deleteafterdate
},
]
)
return response
def main(event, context):
rds = client("rds", region_name=REGION)
now = datetime.now()
# Should we leave time in the name?
datestamp = now.strftime("%m-%d-%Y-%H-%M-%S")
snaptype = "TestBackup"
deleteAfterDate = "Today-test"
create_snapshot(rds, snaptype, datestamp, deleteAfterDate)
Any help is appreciated!!
Should be something like this.
from __future__ import print_function
from boto3 import client
import datetime
from datetime import datetime
# Database cluster identifier that the backup will be performed on
CLUSTER_ID = "export-test-db-cluster"
# Name of the company that the script is used on
COMPANY = "Test"
# AWS region in which the db instances exist
REGION = "us-east-1"
def create_snapshot(rds, snaptype, datestamp, deleteafterdate):
snapname = COMPANY + "-" + snaptype + "-" + datestamp
try:
response = rds.create_db_cluster_snapshot(
DBClusterSnapshotIdentifier=snapname,
DBClusterIdentifier=CLUSTER_ID,
Tags=[
{
'Key': 'Name',
'Value': snapname
},
{
'Key': 'expirationDate',
'Value': deleteafterdate
},
]
)
except Exception as e:
response = e
pass
return response
def main(event, context):
rds = client("rds", region_name=REGION)
now = datetime.now()
# Should we leave time in the name?
datestamp = now.strftime("%m-%d-%Y-%H-%M-%S")
snaptype = "TestBackup"
deleteAfterDate = "Today-test"
my_response = create_snapshot(rds, snaptype, datestamp, deleteAfterDate)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.