Page updated Jan 16, 2024

Configure Lambda function settings

You may want to override the Amplify CLI default configurations for your Lambda function or configure changes not available within the amplify add function workflow.

Example: When creating a Node.js function, the CLI will automatically configure a runtime version, a default memory size, and more. There are a few things you may want to override or configure:

  1. Runtime
  2. Memory size
  3. Environment variables

Let's look at how to update all of these things.

Updating the Runtime

You may want to tweak the runtime version to be either a newer or older version than the Amplify-generated default.

Let's say we've deployed a Lambda function using a Node.js runtime and you want to modify the version of the runtime to be 14.x.

To do so, open amplify/backend/function/function-name/function-name-cloudformation-template.json and set the Runtime property in the LambdaFunction resource to:

1"Resources": {
2 "LambdaFunction": {
3 ...
4 "Properties": {
5 "Runtime": "nodejs14.x", // Runtime now set to 14.x
6 "Layers": [],
7 }
8 ...
9 }
10 },
11}

Next, deploy the updates using the Amplify CLI:

1amplify push

Updating the default memory size

When you deploy a function with Amplify, the default memory size will be set to a low setting (128MB). Often you will want to increase the default memory size in order to improve performance. A popular memory setting in Lambda is 1024MB as it speeds the function noticeably while usually keeping the cost the same or close to it.

To update the memory size, open amplify/backend/function/function-name/function-name-cloudformation-template.json and set the MemorySize property in the LambdaFunction resource:

1"Resources": {
2 "LambdaFunction": {
3 ...
4 "Properties": {
5 "Runtime": "nodejs14.x",
6 "MemorySize": 1024, // Memory size now set to 1024 MB
7 "Layers": [],
8 }
9 ...
10 }
11 },
12}

Next, deploy the updates using the Amplify CLI:

1amplify push

To learn more about optimizing resources allocation for Lambda functions, check out this blog post.

Setting an environment variable

A very common scenario is the need to set and use an environment variable in your Lambda function.

There are generally two types of environment variables:

To view all configuration options available in AWS Lambda, check out the documentation here

To learn more about extending the Amplify CLI with custom resources, check out the documentation here