1
votes

serverless offers a nice workflow for using variables in both the runtime function code as well as in the AWS configuration of Lambda functions and API Gateway routing. This is described at http://docs.serverless.com/docs/templates-variables and How to define variables in a DRY way.

I am having trouble figuring out how to assign variables when I am testing my Lambda functions locally using sls function run -s stage. The function runs fine when I run the test on Lambda by including the -d option, but it fails without it because a necessary variable hasn't been assigned. serverless provides these messages:

Serverless: WARNING: This variable is not defined: region  
Serverless: WARNING: This variable is not defined: appEnv

I am using DRY variables as described in link 2. Specifically:

s-variables-aguldman-useast1.json:

{
  "appEnv": "qa"
}

functions/fn1/s-function.json:

{
...
  "environment": {
    "NODE_ENV": "${appEnv}"
  }
...
}

functions/fn1/handler.js:

...
var foo = process.env.NODE_ENV;
...

Any advice?

Thanks!

3

3 Answers

0
votes

I have to do a bit of guessing here because I'm lacking some info from your question. So I will assume that:

  • that you are trying to output a variable called region
  • that you have a s-function.json file that has this in it:

    "environment": { "myEnvironmentVar":"${myEnvironmentVar}" }

  • that you are trying to access the content of the region variable using this code in you lambda function: process.env.myEnvironmentVar;

if this is the warning you are getting: Serverless: WARNING: This variable is not defined: region

If this is the case then the problem is that you haven't defined this variable any of the these two files:

  • _meta/variables/s-variables-common.json
  • _meta/variables/s-variables-stage.json

Also if you add the value in s-variables-common.json and then also add it to s-variables-stage.json then s-variables-stage.json will override the value of s-variables-common.json

0
votes

If I add the -r us-east-1 option to serverless function run then it works fine. Interestingly, serverless doesn't need the -r option when it is run remotely (if there is a single region in the project) but does when run locally.

To summarize:

serverless function run -s stage -r us-east-1

successfully reads variables from the appropriate s-variables file, but

serverless function run -s stage

does not. In contrast,

serverless function run -s stage -d

works fine without a region being explicitly specified, presumably because I only have one region in my project.

0
votes

First of all run following command to create your stage/regions's variables json file in _meta folder

sls stage create

then provide your stage name and the region(you have to provide your amazon access key and the secret key). Then the following json file will be created.

_meta/variables/s-variables-{your_region}-{your_stage}.json
ie. _meta/variables/s-variables-us-east-1-dev.json

If you want to add the variable manually you can do it as follows. Those variables will be added to above s-variables-{your_region}-{your_stage}.json file

sls variables set

and then give the key and the value or in one line as follows

sls variables set -k appEnv -v dev

Hope this helps.