skip to Main Content

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.

2

Answers


  1. You can use LAMBDA_TASK_ROOT as in

    const isAws = !!process.env.LAMBDA_TASK_ROOT
    
    Login or Signup to reply.
  2. 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!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search