Serverless-offline: setting 'stage' in serverless.yml with serverless-offline doesn't load the proper environment variables

Created on 21 Dec 2017  路  6Comments  路  Source: dherault/serverless-offline

relevant parts of the serverless.yml below

provider:
  stage: staging
  environment: ${file(env.yml):${self:custom.stage}}

plugins:
  - serverless-offline

custom:
  stage: "${opt:stage, self:provider.stage}"
  serverless-offline:
    stage: "development"

relevant parts of the env.yml file

development:
  STAGE: "development"

staging:
  STAGE: "staging"

When I run "serverless offline start" I would expect it to load "process.env.STAGE" as "development" but when I check, it's still set to "staging."

However, this isn't a problem when I run "serverless offline start --stage development", so maybe there's something about the way that the serverless.yml file is resolved with setting the options passed in the cli vs. being set in the serverless.yml. Or perhaps when they are set in the serverless-offline custom variable, it is not overriding the stage set in the provider.

Most helpful comment

I was having a similar problem what fixed the problem was this:
provider: stage: ${opt:stage, 'dev'}

this will set the self:provider.stage variable either to given --stage parameter value or defaults to dev

All 6 comments

There is a flag in CLI called --stage which you can use. I'm not sure is can you set it like that in serverless.yml file. I checked the code briefly and I didn't find anything where it explicitly setting it, rather using the --stage argument.

Below is scripts part of package.json that I am using in my set up:

"scripts": { "postinstall": "flow-typed install", "localServer": "concurrently \"yarn sls dynamodb install\" \"yarn offline\" ", "offline": "yarn sls offline start --stage local -r us-east-1", "deployDev": "SLS_DEBUG=* sls deploy --stage dev", "sls": "node --inspect=3217 ./node_modules/.bin/sls", "build": "sls webpack --out dist" },
Hope this helps.

P.S. I'm glad that not everything is configurable in serverless.yml :)

It seems there is a bug when setting stage and region within yml file.
Any of the CLI options can be added to your serverless.yml. it seems it doesn't work...

I was having a similar problem what fixed the problem was this:
provider: stage: ${opt:stage, 'dev'}

this will set the self:provider.stage variable either to given --stage parameter value or defaults to dev

I am attempting to use niom's suggestion, but this does not seem to work for me. The following are the relevant portions of my serverless.yml file.

provider:
  name: aws
  runtime: python3.6
  region: us-east-1
  stage: ${opt:stage, 'dev'}
...
  environment:
    DBHOST: ${self:custom.myDbHost.${self:provider.stage}}
...
custom:
  stages:
    - local
    - dev
  myDbHost:
    local: 'postgres'
    dev: {"Fn::GetAtt": [ServerlessRDSCluster, Endpoint.Address]}

I am starting serverless offline with serverless offline start --host 0.0.0.0 --stage local

I have a testing endpoint that for debugging purposes just returns the value of the DBHOST environment variable, and it is responding with "[object Object]" (because that Fn::GetAtt doesn't work locally).

Any idea what I am doing wrong?

I think this needs a PR, can anyone investigate please ?

I think this needs a PR, can anyone investigate please ?

@dherault PR is up, please review.

Looks like same bug exists for region.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yareyaredesuyo picture yareyaredesuyo  路  4Comments

MEGApixel23 picture MEGApixel23  路  4Comments

FranzSkuffka picture FranzSkuffka  路  3Comments

davidroman0O picture davidroman0O  路  4Comments

Looveh picture Looveh  路  4Comments