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:
- Runtime
- Memory size
- 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:
"Resources": { "LambdaFunction": { ... "Properties": { "Runtime": "nodejs14.x", // Runtime now set to 14.x "Layers": [], } ... } },}
Next, deploy the updates using the Amplify CLI:
amplify 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:
"Resources": { "LambdaFunction": { ... "Properties": { "Runtime": "nodejs14.x", "MemorySize": 1024, // Memory size now set to 1024 MB "Layers": [], } ... } },}
Next, deploy the updates using the Amplify CLI:
amplify 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: