3

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

3nomis
  • 1,175
  • 1
  • 9
  • 30

2 Answers2

5

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.

Mike Patrick
  • 10,699
  • 1
  • 32
  • 54
  • Thanks for your kind reply, I tried with your suggestion, here is my serverlessVars file: https://paste.ubuntu.com/p/Sxzn8r7dZ8/ and here my step function yml: https://paste.ubuntu.com/p/Mk53fwhjsc/ Every time I run `sls deploy --environment dev` the following error appears: https://paste.ubuntu.com/p/GBW2v3bx4K/ – 3nomis Sep 29 '20 at 10:00
  • 2
    This may have changed since this answer, but with Serverless 2 you can have multiple fall-backs without nesting. So the stage example above becomes `stage: ${opt:stage, env:stage, 'dev'}` – Dennis J Nov 24 '21 at 16:27
0

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}}
elbik
  • 1,749
  • 2
  • 16
  • 21