Skip to content

Commit

Permalink
Add cfnresponse
Browse files Browse the repository at this point in the history
  • Loading branch information
pecigonzalo committed Sep 23, 2024
1 parent d672a15 commit 2147c21
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/cfnresponse.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion src/unsubscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2147c21

Please sign in to comment.