The Problem

While writing code for Lambda functions in the AWS Cloud9 IDE, I had the need to provide separate configurations for test and production and the first step was to figure out within the code, that in which environment , the Lambda function is running in; on Cloud9(or any other IDE locally) or in the cloud.

Normally when running nodejs apps, you would provide environment variables for this purpose when you start the app, so you know the code is being run in which environment currently. But that is not possible when the Lambda functions run in the cloud, as the procedure is controlled by AWS itself. There is the option for providing environment variables in Lambda, but the option is to provide a single value for a single variable whereas what we want is to have separate values for a single variable name in different environments(running locally and on the cloud). Also, there is the option of ‘Stages’ in API Gateway, but I am not able to figure out if there is way to pass that on to the Lambda function regarding the current environment.

Solution: How to figure the current environment in Lambda functions

Luckily, I noticed that there is a property in the context variable that is passed as a parameter when the Lambda function is invoked. The property name is context.invokedFunctionArn. This contains the ARN of the Lambda function but at the end there is a postfix which contains test when we run Lambda function in Cloud9 test environment and has the value prod when the function is run inside the cloud.

Screenshot 2019-09-12 at 6.12.57 PM

The following snapshot is taken when running the function inside Cloud9:

Notice the :test at the end. But this value changes to prod when the function is run in the cloud by AWS. Using the finding we can figure the current environment and load the corresponding configuration.

Loading the correct configuration for the current environment

Now in order to load the correct configuration, we first need to write a configuration file. Create a new file, in this case it isconfig.json in the root of the project and paste the following content inside it:

    "dev": {
        "database_host": "db-host",
        "database_password": "db-password"
    "prod": {
        "database_host": "db-host-supreme",
        "database_password": "db-password-supreme"

Now at the start of your Lambda function, paste the following code:

let alias = context.invokedFunctionArn.split(':').pop();
let configEnvironment = '';
if (alias === 'test') {
    configEnvironment = 'dev';
} else {
    configEnvironment = 'prod';
console.log(functionName + ": Config Environment => " + configEnvironment);
const config = require("../config")[configEnvironment];

What we are doing above is:

  1. Splitting the string based on semicolon and picking up the last element, which would be either test or prod.
  2. Declare a variable configEnvironment which is assigned one of the environments’ names that we are using in our config.json file.
  3. Use the configEnvironment variable to load the required configuration from the config.json file and assign it to a constant variable config.

Now you can use this config variable in your code to get the required elements’ values. For example:

let dbHost = config.database_host

This configuration is not only feasible when run in Cloud9, but also when running the code in any other IDE, and when running tests. Simply create the property invokedFunctionArn in the context parameter, provide a dummy value that ends with :test and pass it on as the context parameter to the Lambda function so you can load the test environment.