Best practices with Lambda - Amazon DynamoDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

Best practices with Lambda

An Amazon Lambda function runs within a container—an execution environment that is isolated from other functions. When you run a function for the first time, Amazon Lambda creates a new container and begins executing the function's code.

A Lambda function has a handler that is run once per invocation. The handler contains the main business logic for the function. For example, the Lambda function shown in Step 4: Create and test a Lambda function has a handler that can process records in a DynamoDB stream.

You can also provide initialization code that runs one time only—after the container is created, but before Amazon Lambda runs the handler for the first time. The Lambda function shown in Step 4: Create and test a Lambda function has initialization code that imports the SDK for JavaScript in Node.js, and creates a client for Amazon SNS. These objects should only be defined once, outside of the handler.

After the function runs, Amazon Lambda might opt to reuse the container for subsequent invocations of the function. In this case, your function handler might be able to reuse the resources that you defined in your initialization code. (You cannot control how long Amazon Lambda will retain the container, or whether the container will be reused at all.)

For DynamoDB triggers using Amazon Lambda, we recommend the following:

  • Amazon service clients should be instantiated in the initialization code, not in the handler. This allows Amazon Lambda to reuse existing connections, for the duration of the container's lifetime.

  • In general, you do not need to explicitly manage connections or implement connection pooling because Amazon Lambda manages this for you.

For more information, see Best practices for working with Amazon Lambda functions in the Amazon Lambda Developer Guide.