Can AWS Lambda's environment variable be set to a boolean value?

Issue

I want to update the environment variable for a Lambda function and this the code that I am using currently.

const AWS = require("aws-sdk");

exports.handler = async (event, context) => {
    const res = await updateConfig("test");
};

async function updateConfig(funcName) {
    const lambda = new AWS.Lambda({
        region: "us-east-2"
    });
    const params = {
        FunctionName: funcName,
        Environment: {
            Variables: {
                "debug": true
            }
        }
    };
    const data = await lambda.updateFunctionConfiguration(params).promise();
    return data;
}

Currently this code doesn’t work as I am trying to set the environment variable debug to true but it can only be a string and not a boolean.

Solution

The documentation https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
is referring to the key and value pair as:

An environment variable is a pair of strings that are stored in a function’s version-specific configuration.

You can use:

    Environment: {
        Variables: {
            "debug": "true"
        }
    }

and check the variable accordingly in your function:

    if(process.env.debug === "true") {
        console.log("debug is set to true")
    }

Answered By – gekart

Answer Checked By – Marilyn (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.