1

i'm running my serverless with serverless offline library, i'm running using sls offline and i have several. how i detect is my lambda API triggered from localhost or server? thank you

i already try like process.env.IS_LOCAL, process.env.AWS_EXECUTION_ENV but returning undefined.

4 Answers4

1

You can use LAMBDA_TASK_ROOT as in

const isAws = !!process.env.LAMBDA_TASK_ROOT
cyberwombat
  • 38,105
  • 35
  • 175
  • 251
1

You may want to add something like console.log(process.env) in your lambda handler to learn what environment variables exist in both cases called either from localhost or server, then see what you would use to detect how lambda is triggered.

for example:

exports.handler = async (event) => {
    
    const response = {
        statusCode: 200,
        body: {"env": process.env},
    };
    console.log(process.env)
    return response;
};

Would return:

{
  "statusCode": 200,
  "body": {
    "env": {
      "AWS_LAMBDA_FUNCTION_VERSION": "$LATEST",
      "AWS_SESSION_TOKEN": "TOKEN",
      "LAMBDA_TASK_ROOT": "/var/task",
      "LD_LIBRARY_PATH": "/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib",
      "AWS_LAMBDA_LOG_GROUP_NAME": "/aws/lambda/credential-exfiltration",
      "AWS_LAMBDA_RUNTIME_API": "127.0.0.1:9001",
      "AWS_LAMBDA_LOG_STREAM_NAME": "2023/07/11/[$LATEST]c50b302786f4448ba80c142eea1c94fa",
      "AWS_EXECUTION_ENV": "AWS_Lambda_nodejs16.x",
      "AWS_XRAY_DAEMON_ADDRESS": "169.254.79.129:2000",
      "AWS_LAMBDA_FUNCTION_NAME": "credential-exfiltration",
      "PATH": "/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin",
      "AWS_DEFAULT_REGION": "us-east-1",
      "PWD": "/var/task",
      "AWS_SECRET_ACCESS_KEY": "AWS_SECRET_ACCESS_KEY",
      "LANG": "en_US.UTF-8",
      "LAMBDA_RUNTIME_DIR": "/var/runtime",
      "AWS_LAMBDA_INITIALIZATION_TYPE": "on-demand",
      "NODE_PATH": "/opt/nodejs/node16/node_modules:/opt/nodejs/node_modules:/var/runtime/node_modules:/var/runtime:/var/task",
      "AWS_REGION": "us-east-1",
      "TZ": ":UTC",
      "AWS_ACCESS_KEY_ID": "AWS_ACCESS_KEY_ID",
      "SHLVL": "0",
      "_AWS_XRAY_DAEMON_ADDRESS": "169.254.79.129",
      "_AWS_XRAY_DAEMON_PORT": "2000",
      "AWS_XRAY_CONTEXT_MISSING": "LOG_ERROR",
      "_HANDLER": "index.handler",
      "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "128",
      "NODE_EXTRA_CA_CERTS": "/etc/pki/tls/certs/ca-bundle.crt",
      "_X_AMZN_TRACE_ID": "Root=1-64ad16dd-11b6c8747177397c3f4f0f2e;Parent=49939b5605c39c50;Sampled=0;Lineage=920b0c92:0"
    }
  }
}

Hope that helps!

Vasyl Herman
  • 414
  • 2
  • 11
0

If you want to determine whether the Lambda API is triggered on the localhost or server, you can use serverless offline plugins then you can simply use the IS_OFFLINE environment variable. If you invoke Lambda using sls offline process.env.IS_OFFLINE value will be true. in package.json you can add this dependency :

  "devDependencies": {
    "serverless-offline": "^8.8.0"
  },

in serverless.yml you can add this line :

# Plugins
plugins:
  - serverless-offline

This is the related documentation that you can read link

0

If you're using serverless-offline plugin, the best approach is to use process.env.IS_OFFLINE variable documented here.

pgrzesik
  • 1,869
  • 13
  • 14