Functions

Configuring 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 we 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": { ... "Runtime": "nodejs14.x", // Runtime now set to 14.x "Layers": [], ... } }, }

"Resources": { "LambdaFunction": { ... "Runtime": "nodejs14.x", // Runtime now set to 14.x "Layers": [], ... } }, }

Next, deploy the updates using the Amplify CLI:

amplify push

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": { ... "Runtime": "nodejs14.x", "MemorySize": "1024", // Memory size now set to 1024 mb "Layers": [], ... } }, }

"Resources": { "LambdaFunction": { ... "Runtime": "nodejs14.x", "MemorySize": "1024", // Memory size now set to 1024 mb "Layers": [], ... } }, }

Next, deploy the updates using the Amplify CLI:

amplify push

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:

  • Secret values (example: access keys, API keys etc.)
  • Non-secret values (example: endpoint information, locale information etc.)

1. Configuring secret values

If your value is secret, you can use Secrets Manager.

To do so, you first need to create a secret in the secrets manager console.

Next, add a statement to the PolicyDocument in amplify/backend/function/function-name/function-name-cloudformation-template.json to give the Lambda function permission to use the secret:

{ "Effect": "Allow", "Action": [ "secretsmanager:GetSecretValue" ], "Resource": { "Fn::Sub": [ "arn:aws:secretsmanager:${region}:${account}:secret:key_id", { "region": { "Ref": "AWS::Region" }, "account": { "Ref": "AWS::AccountId" } } ] } }

{ "Effect": "Allow", "Action": [ "secretsmanager:GetSecretValue" ], "Resource": { "Fn::Sub": [ "arn:aws:secretsmanager:${region}:${account}:secret:key_id", { "region": { "Ref": "AWS::Region" }, "account": { "Ref": "AWS::AccountId" } } ] } }

Next, access the token in your function:

const AWS = require('aws-sdk') const secretsManager = new AWS.SecretsManager() const secret = await secretsManager.getSecretValue({ SecretId: 'YOUR_KEY' }).promise() console.log(secret.SecretString)

const AWS = require('aws-sdk') const secretsManager = new AWS.SecretsManager() const secret = await secretsManager.getSecretValue({ SecretId: 'YOUR_KEY' }).promise() console.log(secret.SecretString)

2. Configuring non-secret values

If your value is just a configuration value, you can configure the CloudFormation configuration locally to set the value - in amplify/backend/function/function-name/function-name-cloudformation-template.json

For this purpose, there is a section in the template - Parameters - that you can set.

"Parameters" : { "MyKey" : { "Type" : "String", "Default" : "my-environment-variable" } }

"Parameters" : { "MyKey" : { "Type" : "String", "Default" : "my-environment-variable" } }

And then use these parameters in Environment declaration:

"Environment":{ "Variables":{ "MY_ENV_VAR":{ "Ref":"MyKey" } } }

"Environment":{ "Variables":{ "MY_ENV_VAR":{ "Ref":"MyKey" } } }

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

Previous Page
previous

Calling DynamoDB using AWS Cognito triggers

Discord Logo
Amplify open source, documentation and community are supported by Amazon Web Services © 2020, Amazon Web Services, Inc. and its affiliates. All rights reserved. View the site terms and privacy policy.
    Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC.