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 completion. 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
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 completely 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.
From the original question and the comments 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.com"
}
From the command 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.com"}' \
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.com"
});
const input = {
"FunctionName": "<function-name>",
"Payload": payload
};
const command = new InvokeCommand(input);
const response = await client.send(command);
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.