Movatterモバイル変換


[0]ホーム

URL:


Building a custom runtime for AWS Lambda - AWS Lambda
DocumentationAWS LambdaDeveloper Guide
RequirementsImplementing response streaming in a custom runtime

Building a custom runtime for AWS Lambda

You can implement an AWS Lambda runtime in any programming language. A runtime is a program that runs a Lambda function's handler method when the function is invoked. You can include the runtime in your function's deployment package or distribute it in alayer. When you create the Lambda function, choose anOS-only runtime (theprovided runtime family).

For a walkthrough of the custom runtime deployment process, seeTutorial: Building a custom runtime.

Requirements

Custom runtimes must complete certain initialization and processing tasks. A runtime runs the function's setup code, reads the handler name from an environment variable, and reads invocation events from the Lambda runtime API. The runtime passes the event data to the function handler, and posts the response from the handler back to Lambda.

Initialization tasks

The initialization tasks run onceper instance of the function to prepare the environment to handle invocations.

  • Retrieve settings – Read environment variables to get details about the function and environment.

    • _HANDLER – The location to the handler, from the function's configuration. The standard format isfile.method, wherefile is the name of the file without an extension, andmethod is the name of a method or function that's defined in the file.

    • LAMBDA_TASK_ROOT – The directory that contains the function code.

    • AWS_LAMBDA_RUNTIME_API – The host and port of the runtime API.

    For a full list of available variables, seeDefined runtime environment variables.

  • Initialize the function – Load the handler file and run any global or static code that it contains. Functions should create static resources like SDK clients and database connections once, and reuse them for multiple invocations.

  • Handle errors – If an error occurs, call theinitialization error API and exit immediately.

Initialization counts towards billed execution time and timeout. When an execution triggers the initialization of a new instance of your function, you can see the initialization time in the logs andAWS X-Ray trace.

Processing tasks

While it runs, a runtime uses theLambda runtime interface to manage incoming events and report errors. After completing initialization tasks, the runtime processes incoming events in a loop. In your runtime code, perform the following steps in order.

  • Get an event – Call thenext invocation API to get the next event. The response body contains the event data. Response headers contain the request ID and other information.

  • Propagate the tracing header – Get the X-Ray tracing header from theLambda-Runtime-Trace-Id header in the API response. Set the_X_AMZN_TRACE_ID environment variable locally with the same value. The X-Ray SDK uses this value to connect trace data between services.

  • Create a context object – Create an object with context information from environment variables and headers in the API response.

  • Invoke the function handler – Pass the event and context object to the handler.

  • Handle the response – Call theinvocation response API to post the response from the handler.

  • Handle errors – If an error occurs, call theinvocation error API.

  • Cleanup – Release unused resources, send data to other services, or perform additional tasks before getting the next event.

Entrypoint

A custom runtime's entry point is an executable file namedbootstrap. The bootstrap file can be the runtime, or it can invoke another file that creates the runtime. If the root of your deployment package doesn't contain a file namedbootstrap, Lambda looks for the file in the function's layers. If thebootstrap file doesn't exist or isn't executable, your function returns aRuntime.InvalidEntrypoint error upon invocation.

Here's an examplebootstrap file that uses a bundled version of Node.js to run a JavaScript runtime in a separate file namedruntime.js.

Implementing response streaming in a custom runtime

Forresponse streaming functions, theresponse anderror endpoints have slightly modified behavior that lets the runtime stream partial responses to the client and return payloads in chunks. For more information about the specific behavior, see the following:

OS-only runtimes
Custom runtime tutorial

[8]
ページ先頭

©2009-2025 Movatter.jp