繁体   English   中英

如何从 AWS Python Lambda 获取响应

[英]How to get the response from an AWS Python Lambda

我编写了一些在 AWS Lambda 中运行的 Python 代码。 当我在 Lambda AWS 仪表板中测试 Lambda 并且它运行没有任何错误时,我在“执行结果”选项卡中看到以下内容:

Response: null
Request ID: "421fd7da-20f7-4029-aa8b-f7281e7c90d9"

如果我在运行 Lambda 时遇到任何错误,我会在“执行结果”选项卡中看到 JSON 格式的输出。

这是一个例子:

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"
  ]
}

如何将此“响应”返回到我的 Python 代码中,以便我可以使用/阅读它? 特别是我想阅读“errorMessage”,以便我在 Lambda 中的 python 代码可以将其打印出来或转发到 - 比如说在 SNS 中使用。

我添加了一个带有响应的返回,但它的内容与我在“执行结果”选项卡中得到的响应中的内容不匹配。

这是我的 Python 代码 -

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)

任何帮助表示赞赏!

应该是这样的。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM