Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More informative logging #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions cfnlambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from functools import wraps
import boto3
from botocore.vendored import requests
import traceback
import httplib

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -92,7 +94,7 @@ def cfn_response(event,
CloudWatch Logs log stream is used.

Returns:
None
requests.Response object

Raises:
No exceptions raised
Expand Down Expand Up @@ -120,12 +122,18 @@ def cfn_response(event,
try:
response = requests.put(event['ResponseURL'],
data=response_body)
logger.debug("Status code: %s" % response.status_code)
body_text = ""
if response.status_code // 100 != 2:
body_text = "\n" + response.text
logger.debug("Status code: %s %s%s" % (response.status_code, httplib.responses[response.status_code], body_text))

# logger.debug("Status message: %s" % response.status_message)
# how do we get the status message?
return response
except Exception as e:
logger.error("send(..) failed executing https.request(..): %s" %
e.message)
logger.debug(traceback.format_exc())


def handler_decorator(delete_logs=True,
Expand Down Expand Up @@ -208,6 +216,7 @@ def handler_wrapper(event, context):
logger.info('REQUEST RECEIVED: %s' % json.dumps(event))
logger.info('LambdaContext: %s' %
json.dumps(vars(context), cls=PythonObjectEncoder))
result = None
try:
result = handler(event, context)
status = Status.SUCCESS if result else Status.FAILED
Expand All @@ -221,6 +230,10 @@ def handler_wrapper(event, context):
(handler.__name__, e.message))
result = {'result': message}
logger.error(message)
logger.debug(traceback.format_exc())

if not result:
result = {}

if event['RequestType'] == RequestType.DELETE:
if status == Status.FAILED and hide_stack_delete_failure:
Expand All @@ -230,7 +243,7 @@ def handler_wrapper(event, context):
'despite the fact that the stack status may be '
'DELETE_COMPLETE.')
logger.error(message)
result['result'] += ' %s' % message
result['result'] = result.get('result', '') + ' %s' % message
status = Status.SUCCESS

if status == Status.SUCCESS and delete_logs:
Expand All @@ -243,6 +256,6 @@ def handler_wrapper(event, context):
status,
(result if type(result) is dict else
{'result': result}))
return handler(event, context)
return result
return handler_wrapper
return inner_decorator