Is there a way to add a new environment variable to an AWS Lambda function without removing the ones already there?
(With the command line tools, that is.)
Is there a way to add a new environment variable to an AWS Lambda function without removing the ones already there?
(With the command line tools, that is.)
Using the Lambda Console you can just append new Environmental variables:
Doing it using the CLI is harder- aws lambda update-function-configuration allows you to selectively update aspects of a lambda, but does not have helper methods to append enviornment variables. You can use aws lambda get-function-configuration to get the current list of enviornment variables. Which could be used in tandem with some bash/powershell scripting (or language of your choice using the matching SDK functions).
For example:
const AWS = require('aws-sdk');
const lambda = new AWS.lambda();
const FunctionName = 'FUNCTION_NAME';
const AppendVars = { key: value };
async function appendVars() {
const { Environment: { Variables } } = await lambda.getFunctionConfiguration({ FunctionName }).promise();
await lambda.updateFunctionConfiguration({
FunctionName,
Environment: { Variables: { ...Variables, ...AppendVars } },
}).promise();
}
appendVars();
I successfully used this as a bash script.
The name of the lambda function is coming in from a parameter. My goal was to simple change a variable so to restart the lambda function.
LAMBDA=$1
CURRENTVARIABLES=$(aws lambda get-function-configuration --function-name $LAMBDA | jq '.Environment.Variables')
NEWVARIABLES=$(echo $CURRENTVARIABLES | jq '. += {"kick":"'$(getrandom 8)'"}')
COMMAND="aws lambda update-function-configuration --function-name $LAMBDA --environment '{\"Variables\":$NEWVARIABLES}'"
eval $COMMAND