簡體   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