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:
Let’s look at how to update all of these things.
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
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
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:
If your value is secret, you can use
To do so, you first need to create a secret in the
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)
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"
}
}
}