red888
red888

Reputation: 31652

What command is aws lambda actually running to execute the handler for Python?

I can't find this in the docs

How is AWS lambda executing the Python handler? What is the actual python.exe commands being executed? I would like to understand this better. Lambda behind the scenes is surely starting a container and somehow copying the uploaded package onto a volume or something. But what command is it actually running to execute it?

Edit

I found this article which shows how to get a console session open to a lambda, have not checked to see if it still works: https://epsagon.com/blog/lambda-internals-exploring-aws-lambda/

Upvotes: 1

Views: 138

Answers (1)

Marcin
Marcin

Reputation: 238697

I don't have the exact answer as I think implementation details are secret, but to see how it could work, or potentially could be implemented in AWS, is the following project: lambci / docker-lambda:

A sandboxed local environment that replicates the live AWS Lambda environment almost identically – including installed software and libraries, file structure and permissions, environment variables, context objects and behaviors – even the user and running process are the same.

Since its open sourced, one would have to go through its source code to locate the exact thing you are after.

Obviously, this is not official AWS docker repository, but it should provide some good insights on how real AWS lambda environment could work in the back-end.

This docker project is also recommended by AWS for using local lambda environment:

When you get its python 3.8 runtime: https://lambci.s3.amazonaws.com/fs/python3.8.tgz you can find bootstrap.py file there. In the file there is the following function (only part is shown):

123 def handle_event_request(lambda_runtime_client, request_handler, invoke_id, event_body, content_type,
124                          client_context_json, cognito_identity_json, invoked_function_arn, epoch_deadline_time_in_ms,
125                          log_sink):
126     error_result = None
127     try:
128         lambda_context = create_lambda_context(client_context_json, cognito_identity_json, epoch_deadline_time_in_ms,
129                                                invoke_id, invoked_function_arn)
130         event = lambda_runtime_client.marshaller.unmarshal_request(event_body, content_type)
131         response = request_handler(event, lambda_context)

The last line, response = request_handler(event, lambda_context) is actual invocation of your handler.

Upvotes: 1

Related Questions