Pyjo.App.Lambda - AWS Lambda event function

from Pyjo.App.Lambda import Lambda

app = Lambda(function='myfunction.handler')

# Or import the module, as in Google Python Style Guide

import Pyjo.App.Lambda

app = Pyjo.App.Lambda.new(function='myfunction.handler')

# Another runtime, with the events of a REST API
app = Pyjo.App.Lambda.new(command=['node', 'runtime.js'], payload='1.0')

# Mount it in an application of Pyjoyment
import Pyjoyment

pyjoyment = Pyjoyment.new()
pyjoyment.plugin('Mount', {'/function': app})

Pyjo.App.Lambda is an application of Pyjo that runs an event function of AWS Lambda, so that a server of Pyjo serves it, a route of Pyjoyment embeds it, as Pyjoyment.Plugin.Mount does, and Pyjo.Test.Pyjo tests it, as API Gateway, a Function URL or an Application Load Balancer runs it on AWS.

The function runs in a process of its own, as an execution environment of Lambda does, which Pyjo.IOLoop.ChildProcess starts on the first request and keeps for the next ones, which it gets one after another. Each event loop has a process of its own, and the child of a fork, as a worker of Pyjo.Server.Prefork, leaves the processes of its parent to it and starts its own. The process gets its invocations from the Runtime API of AWS_LAMBDA_RUNTIME_API, which a Pyjo.Server.Daemon on a port of the loopback serves on the same event loop, so that a handler of Python runs with the runtime client of Pyjo.Server.Lambda, also one that runs an event loop of its own, and a function of another runtime with its own runtime client, without the event loop waiting for it. The graceful stop of the event loop, as the one of TERM of a server, ends the process once it has answered the invocations of the requests in progress, as its request for the next invocation would keep the event loop running.

The request is the event of the payload format of payload, as Pyjo.Server.Lambda takes it, and the path that a route embeds the application under is the stage of API Gateway, in front of the path, which Pyjo.Server.Lambda takes for the base path of the URL. A body that is not text, JSON, XML, JavaScript or YAML in UTF-8 is in Base64. The response is the one of the payload format, with its statusCode, its headers, its cookies and its body, which can be in Base64, and in the format 2.0 a value without statusCode is a response 200 of the value as JSON, as API Gateway takes it.

A streaming response of the runtime client, as the one of Pyjo.Server.Lambda with the invoke mode RESPONSE_STREAM, is written as it comes, as a Function URL with the invoke mode RESPONSE_STREAM writes it: the status and the headers of its prelude, in the content type application/vnd.awslambda.http-integration-response, before 8 null bytes, or 200 and the content type of the response, and its body in chunks. The timeout of the function goes on until its end, and an error in its trailers, or the timeout, in the middle of it closes the connection, as the response cannot change anymore.

# The streaming responses of an application on Pyjo.Server.Lambda
app = Pyjo.App.Lambda.new(function='myapp.handler', env={'PYJO_LAMBDA_INVOKE_MODE': 'RESPONSE_STREAM'})

An error of the function or of its initialization, a response that is not valid, a process that ends before its response, and an invocation that takes longer than timeout, whose process is killed, as Lambda does, are a response 502, logged, and the next request starts a new process. The output of the process is logged, as Lambda sends it to CloudWatch. The event loop does not watch pipes on Windows, so the response is 502 there.

Attributes

Pyjo.App.Lambda inherits all attributes from Pyjo.App and implements the following new ones.

See also

Pyjoyment, Pyjoyment.Plugin.Mount, Pyjo.Server.Lambda, Pyjo.App.CGI, https://mojolicious.org.

It has no counterpart in Mojolicious; it is the reverse of Pyjo.Server.Lambda.

Classes

class Pyjo.App.Lambda.Pyjo_App_Lambda(attrs: dict[str, Any] | None = None, /, **kwargs: Any)[source]

Pyjo.App.Lambda inherits all methods from Pyjo.App and implements the following new ones.

close() → None[source]
app.close()

End the processes of the function, and close the connections and the servers of ua, as the end of a with block does.

command: list[str] | None
command = app.command
app.command = ['python', '-m', 'awslambdaric', 'myfunction.handler']
app.command = ['./bootstrap']

Program of the function, with its arguments, as the runtime client of another runtime or the bootstrap of a custom runtime, in task_root, or the runtime client of Pyjo.Server.Lambda with function and the interpreter of this process.

env: dict[str, str]
env = app.env
app.env = {'TABLE_NAME': 'users'}

Variables of the environment of the function, which are added to the ones of this process and of Lambda, and replace them.

function: str | None
handler = app.function
app.function = 'myfunction.handler'

Handler of the function, module.function, in _HANDLER, which the runtime client of Pyjo.Server.Lambda runs without command.

function_name
name = app.function_name
app.function_name = 'myfunction'

Name of the function, in AWS_LAMBDA_FUNCTION_NAME and in its ARN, defaults to pyjo.

handler(tx: Any) → None[source]
app.handler(Pyjo.Transaction.HTTP.new())
app.handler(Pyjoyment.Controller.new())

Invoke the function with the event of the request of the transaction, or of the controller of a route that embeds the application, and write its response.

memory_size
size = app.memory_size
app.memory_size = 512

Memory of the function in megabytes, in AWS_LAMBDA_FUNCTION_MEMORY_SIZE, defaults to 128.

multi_value_headers
boolean = app.multi_value_headers
app.multi_value_headers = True

The headers and the parameters of the query of the events of an Application Load Balancer with multiple values, as a target group with them has them, false by default.

payload
payload = app.payload
app.payload = '1.0'
app.payload = 'alb'

Payload format of the events, 2.0 of HTTP APIs and Function URLs, the default, 1.0 of REST APIs, or alb of Application Load Balancers.

task_root: str | PathLike[str] | None
root = app.task_root
app.task_root = '/var/task'

Directory of the function, in LAMBDA_TASK_ROOT, which its process runs in, and which the runtime client of Pyjo.Server.Lambda imports the handler from, the current one by default.

timeout: float
timeout = app.timeout
app.timeout = 3

Maximum amount of time in seconds an invocation can take before its process is killed, defaults to 30, as an integration of API Gateway has it.