javascript - Change value of environment variable while invoking Lambda function - Stack Overflow

admin2025-04-20  0

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

Share Improve this question asked Jan 2, 2022 at 10:34 Faisal ShaniFaisal Shani 8201 gold badge23 silver badges52 bronze badges 6
  • Why would you want to do that? You can probably do 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
  • Does this answer your question? How can I set an environmental variable in node.js? – kiner_shah Commented Jan 2, 2022 at 10:37
  • @kiner_shah — The context of AWS Lambda's means that isn't a good duplicate target here. – Quentin Commented Jan 2, 2022 at 10:39
  • @derpirscher i need to do this on another script where i am creating some route53 records using the lambda function. so everytime I run the lambda function I need to pass the value of variable "domain" – Faisal Shani Commented Jan 2, 2022 at 10:39
  • 1 @FaisalShani That's not what environment variables for and you cannot use them in such a way. If you need to pass parameters to a lambda function, use one of the offical ways, ie either via query parameters or in the event's body ... – derpirscher Commented Jan 2, 2022 at 10:44
 |  Show 1 more ment

2 Answers 2

Reset to default 2

From the original question and the ments I gathered the following requirements for your Lambda:

  1. You want to talk to the Route53 API.
  2. You want to parameterise the Lambda to allow passing a domain name to the Lambda on every invocation.

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.

Option 1: AWS Console

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."
}

Option 2: AWS CLI v2

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

Option 3: AWS SDK

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745110190a285535.html

最新回复(0)