Skip to content

Instantly share code, notes, and snippets.

@gene1wood
Last active April 11, 2025 18:04
Show Gist options
  • Select an option

  • Save gene1wood/c0d37dfcb598fc133a8c to your computer and use it in GitHub Desktop.

Select an option

Save gene1wood/c0d37dfcb598fc133a8c to your computer and use it in GitHub Desktop.
Details on the AWS Lambda Python LambdaContext context object when instantiated from a CloudFormation stack

LambdaContext

Here is the raw output from examining the Python LambdaContext context object in a AWS Lambda function when called from a CloudFormation stack. More information on the context object can be found here : http://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

LambdaContext object : print(context)

<__main__.LambdaContext object at 0x7fd706780710>

LambdaContext vars : vars(context)

{
    'aws_request_id': 'a3de505e-f16b-42f4-b3e6-bcd2e4a73903',
    'log_stream_name': '2015/10/26/[$LATEST]c71058d852474b9895a0f221f73402ad',
    'invoked_function_arn': 'arn:aws:lambda:us-west-2:123456789012:function:ExampleCloudFormationStackName-ExampleLambdaFunctionResourceName-AULC3LB8Q02F',
    'client_context': None,
    'log_group_name': '/aws/lambda/ExampleCloudFormationStackName-ExampleLambdaFunctionResourceName-AULC3LB8Q02F',
    'function_name': 'ExampleCloudFormationStackName-ExampleLambdaFunctionResourceName-AULC3LB8Q02F',
    'function_version': '$LATEST',
    'identity': <__main__.CognitoIdentity object at 0x7fd7042a2b90>,
    'memory_limit_in_mb': '128'
} 

LambdaContext dir : dir(context)

[
    '__class__',
    '__delattr__',
    '__dict__',
    '__doc__',
    '__format__',
    '__getattribute__',
    '__hash__',
    '__init__',
    '__module__',
    '__new__',
    '__reduce__',
    '__reduce_ex__',
    '__repr__',
    '__setattr__',
    '__sizeof__',
    '__str__',
    '__subclasshook__',
    '__weakref__',
    'aws_request_id',
    'client_context',
    'function_name',
    'function_version',
    'get_remaining_time_in_millis',
    'identity',
    'invoked_function_arn',
    'log',
    'log_group_name',
    'log_stream_name',
    'memory_limit_in_mb'
]

context.succeed()

The Python runtime does not have the equivalent of a context.succeed(), context.done() or context.fail().

In order to achieve the same thing, merely return from the handler function to succeed or raise an exception to fail

def lambda_handler(event, context):
    if event['variable_name'] == 1:
        return { 
            'message' : 'We are done'
        }
    else:
        raise Exception('Sending failure')

What does it look like from AWS's side

@NovemberOscar points out this code which shows what AWS sees on their side for the Python context

https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/main/awslambdaric/lambda_context.py

@msambol

msambol commented Jun 24, 2016

Copy link
Copy Markdown

Hi Gene –

Thanks for the gist. I've seen examples of people calling context.succeed() in the Node.js runtime. Is this possible with Python? I can't find anything in the docs.

@depado

depado commented Jul 27, 2016

Copy link
Copy Markdown

Thanks for the gist :)
I have the exact same problem. Can't find anything in the docs about the said succeed or done methods that seem to be used with Node.js runtime.

@cameck

cameck commented Aug 24, 2016

Copy link
Copy Markdown

Hi Michael and Gene,
I'm running into the same problem. Did either of you find a workaround to exit the program early without throwing an error?

@brandond

Copy link
Copy Markdown

@msambol @depado @cameck have you tried just returning from the handler function?

@aod7br

aod7br commented Dec 19, 2017

Copy link
Copy Markdown

@soundmasteraj

Copy link
Copy Markdown

Alexa's skill id is delivered the context object during certain audio playback requests. does that have an alias?

@bisoldi

bisoldi commented Sep 8, 2020

Copy link
Copy Markdown

Just found this, but for anyone else looking, AWS Lambda's Python runtime does not have a succeed() capability. I'm not sure why of the disparity between the two runtimes, but a successful return (whether controlled exceptions via a catch block or normal execution) is what tells Lambda to succeed the invocation.

@rscarrera27

Copy link
Copy Markdown

@doug65536

Copy link
Copy Markdown

Where do I put the response body? How can I mapping template response headers out of it? All I can do is set errorMessage with an exception, right? 304 Not Modified, where does ETag go?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment