I am learning lambda and currently trying to understand the environment variables. Below is a very simple code to show my question. (A nodejs function that will simply print the value of name constant).
exports.handler = async function(event, context) {
const name = process.env.NAME;
return name;
};
I have already defined a environment variable on lambda as following
Now this lambda will surely print "xyz" after pletion. But how we can overwrite/change the value of the "Name" variable while running the Lambda. so while invoking it will show the new value? For example something like --NAME = "abc" or --NAME abc
I am learning lambda and currently trying to understand the environment variables. Below is a very simple code to show my question. (A nodejs function that will simply print the value of name constant).
exports.handler = async function(event, context) {
const name = process.env.NAME;
return name;
};
I have already defined a environment variable on lambda as following
Now this lambda will surely print "xyz" after pletion. But how we can overwrite/change the value of the "Name" variable while running the Lambda. so while invoking it will show the new value? For example something like --NAME = "abc" or --NAME abc
process.env.NAME = "foobar"
but that will only work (if even) for the current invocation. If you want to change the value of the environment variable for future invocations, you need to update your lambda function. Either in the AWS console or via some aws-cli call
– derpirscher
Commented
Jan 2, 2022 at 10:37
From the original question and the ments I gathered the following requirements for your Lambda:
The easiest and probably most fitting way to achieve this is to pass the domain as "event" data on every invocation.
exports.handler = async (event) => {
const domain = event["domain"];
console.log("Domain: %s", domain);
// your Route53 code goes here...
};
To run the Lambda and pass the "domain" you have a lot of options.
You can go to the AWS console, open the Lambda, switch to the "Test" tab and just use the following input JSON:
{
"domain": "www.google."
}
From the mand line you could invoke that Lambda using the AWS CLI.
aws \
lambda \
invoke \
--cli-binary-format raw-in-base64-out \
--function-name <function-name> \
--payload '{"domain":"www.google."}' \
outfile.txt
You can also write some code in a other Lambda, in a script on your local machine or any other way that can use the AWS Lambda and invoke the Lambda like this.
The following is an example of simple NodeJS CLI "code" using the v3 of the AWS SDK.
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";
async function main() {
const client = new LambdaClient({ region: "<your-region>" });
const payload = JSON.stringify({
"domain": "www.google."
});
const input = {
"FunctionName": "<function-name>",
"Payload": payload
};
const mand = new InvokeCommand(input);
const response = await client.send(mand);
console.log(response);
}
main()
If you run node index.js
you will invoke the Lambda with the given payload.
To setup node:
npm init
npm install @aws-sdk/client-lambda
Remember to set type
to module
in the package.json
.
From AWS Knowledge Center: Can I change the environment variables in a published version of my Lambda function?:
You can't change the configuration (including environment variables) or function code in a published Lambda function version. You can only change the current, unpublished function version ($LATEST).
So, short of having your Lambda connect to the AWS API and publish a pletely new version of itself, you can't.
You seem to be trying to use environment variables as a local storage space which, fundamentally, they aren't. You'd probably be better off with a database. I'd look to use DynamoDB for this.