From 2147c2154a1a7e53528d9c15bdc0d59296f34b73 Mon Sep 17 00:00:00 2001 From: Gonzalo Peci Date: Mon, 23 Sep 2024 12:50:08 +0200 Subject: [PATCH] Add cfnresponse --- src/cfnresponse.py | 55 +++++++++++++++++++++++++++++++++++++++++++++ src/unsubscriber.py | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/cfnresponse.py diff --git a/src/cfnresponse.py b/src/cfnresponse.py new file mode 100644 index 0000000..96d4875 --- /dev/null +++ b/src/cfnresponse.py @@ -0,0 +1,55 @@ +# DO NOT EDIT +# Source: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-lambda-function-code-cfnresponsemodule.html#cfn-lambda-function-code-cfnresponsemodule-source-python + +from __future__ import print_function +import urllib3 # type: ignore +import json + +SUCCESS = "SUCCESS" +FAILED = "FAILED" + +http = urllib3.PoolManager() + + +def send( + event, + context, + responseStatus, + responseData, + physicalResourceId=None, + noEcho=False, + reason=None, +): + responseUrl = event["ResponseURL"] + + print(responseUrl) + + responseBody = { + "Status": responseStatus, + "Reason": reason + or "See the details in CloudWatch Log Stream: {}".format( + context.log_stream_name + ), + "PhysicalResourceId": physicalResourceId or context.log_stream_name, + "StackId": event["StackId"], + "RequestId": event["RequestId"], + "LogicalResourceId": event["LogicalResourceId"], + "NoEcho": noEcho, + "Data": responseData, + } + + json_responseBody = json.dumps(responseBody) + + print("Response body:") + print(json_responseBody) + + headers = {"content-type": "", "content-length": str(len(json_responseBody))} + + try: + response = http.request( + "PUT", responseUrl, headers=headers, body=json_responseBody + ) + print("Status code:", response.status) + + except Exception as e: + print("send(..) failed executing http.request(..):", e) diff --git a/src/unsubscriber.py b/src/unsubscriber.py index c9a2881..d4fcb8f 100644 --- a/src/unsubscriber.py +++ b/src/unsubscriber.py @@ -4,7 +4,7 @@ from typing import Optional, TypedDict import boto3 # type: ignore -import cfnresponse # type: ignore +from . import cfnresponse level = os.getenv("log_level", "INFO") logging.basicConfig(level=level)