-1

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

enter image description here

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

Faisal Shani
  • 698
  • 1
  • 13
  • 37
  • 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 Jan 02 '22 at 10:37
  • Does this answer your question? [How can I set an environmental variable in node.js?](https://stackoverflow.com/questions/10829433/how-can-i-set-an-environmental-variable-in-node-js) – kiner_shah Jan 02 '22 at 10:37
  • @kiner_shah — The context of AWS Lambda's means that isn't a good duplicate target here. – Quentin Jan 02 '22 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 Jan 02 '22 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 Jan 02 '22 at 10:44
  • @FaisalShani You can't permanently change it at runtime, but you can override it for that lambda execution of course. For example send the new value in the event, and your lambda code needs to look in the event for whatever field you've set and use the new value if supplied. – 404 Jan 02 '22 at 11:23

2 Answers2

2

From the original question and the comments 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.com"
}

Invoke Lambda in AWS Console

Option 2: AWS CLI v2

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

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

Jens
  • 20,533
  • 11
  • 60
  • 86
1

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • thanks for the reply. Just a bit more information about my case is that I have a nodejs function locally that creates some route53 records when it runs. for this I have to pass the value of a "DOMAIN" variable by something like --AWS_PROFILE=qa DOMAIN=abc.ss.com nodejs index.js – Faisal Shani Jan 02 '22 at 10:42
  • anyway I can replicate the same on AWS lambda side? – Faisal Shani Jan 02 '22 at 10:43
  • They aren't even trying to use environment variables as local storage, they are just trying to use it to pass parameters into their script. @FaisalShani you need to change your code to pull the domain from event parameters instead of environment variables. – Mark B Jan 02 '22 at 14:52