Serverless option as default value for option not working

Viewed 3485

Hi I am trying to do the following with serverless: If I am not giving an option I want it to pick another option

${opt:fb,${opt:environment,'dev'}}

However something is wrong as this error always appear as I call sls deploy:

A valid option to satisfy the declaration 'opt:fb,dev' could not be found.

I do not understand what's wrong as apparently it seem to pick the value correctly. Able to help me out? Thanks

2 Answers

To get this type of interpolation working correctly, wrap the inner variable with double quotes:

${opt:fb, "${opt:environment, 'dev'}"}

This pattern is especially useful for setting up default -> envvar -> CLI option heirarchies.

For example:

stage: ${opt:stage, "${env:stage, 'dev'}"}
region: ${opt:region, "${env:region, 'us-east-1'}"}

Stage and region each have a default, which can be overridden by an environment variable, which can in turn be overridden by a CLI argument.

According to your expression, you are trying to do a double condition. Looks like it doesn't work.

${opt:fb,${opt:environment,'dev'}}

  1. ${opt:environment,'dev'} - when the opt:environment is undefined then value is set to 'dev' (also maybe you meant opt:stage)
  2. the name of the variable is built: opt:fb,dev - which cannot be found.

Try to simplify and split your options:

custom:
    cliEnv: ${opt:environment, 'dev'}
    conditionEnv: ${opt:fb, ${self:custom.cliEnv}}
Related