PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 22, 2022

[FIXED] How do I inject environment variables to Lambda running under sam local?

 August 22, 2022     amazon-web-services, aws-lambda, aws-sam, environment-variables, node.js     No comments   

Issue

I am developing an aws lambda locally using AWS SAM.
I am trying to inject environment variables, with no success.
The Lambda code is:

exports.lambdaHandler = async (event, context) => {
    console.log('INJECTED ENVIRONMENT:',JSON.stringify(process.env));
    response = {
        'statusCode': 200,
        'body': "booo"
    }
    return response;
};

The environment file test.json is:

{
  "parameters": {
    "PPPPPPPPPPPPPPPPPPPPP1": "PPPPPPPPPPPPPPPPPPPPP1",
    "PPPPPPPPPPPPPPPPPPPPP2": "PPPPPPPPPPPPPPPPPPPPP2",
    "PPPPPPPPPPPPPPPPPPPPP3": "PPPPPPPPPPPPPPPPPPPPP3"
  }
}

I am running the lambda. It runs ok, shows me the expected output, Except the env variables I tried defining.
sam local invoke -e events/event.json -n test.json

What am I missing?


Solution

The environment variables file has a specific format it needs to adhere to that depends on the logical id of the lambda function you want to test:

Test.json:

{
  "LogicalIdOfLambdaHere":{
    "ENV_VAR_KEY": "value"
  }
}

Make sure the logical id matches the one defined in your sam template. Alternatively you can use Parameters instead of parameters.

Note you also need to define the same variables in the actual SAM template. This method is for overriding the values specified in the template and not for adding new ones not defined in the template. Variables are also case sensitive:)



Answered By - LRutten
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing