Passing Boolean values from CLI into serverless.yml


#1

Hi everyone,
I have a question regarding passing variables from the CLI. As far as I can see so far they seem to be interpreted as strings, is there any way a variable could be interpreted as a boolean?
my serverless.yml looks something like like this:

...
functions:

  poll_queue:
    ....
    events:
      - schedule:
          rate: rate(1 minute)
          enabled: ${opt:poll_enabled}

...

and I want to be able to do this:
serverless deploy --poll_enabled false
and
serverless deploy --poll_enabled true
however I think the variable is interpreted as a string and is therefore always enables the schedule


#2

I just ran into the exact same issue. @alechewitt did you (or anyone else) manage to find a solution for this?


#3

I’m in the same situation.
There is a closed-without-fix issue from v0.5 about this: https://github.com/serverless/serverless/issues/672


#4

My situation is this:
I need to set a boolean variable so that in our CI/CD builds, custom.dockerizePip is set to false.

The following does not work because the environment variable value is interpreted as a string, so any value will be interpreted as true.

custom:
  pythonRequirements:
    dockerizePip: ${env:DOCKERIZE_PIP}

and if the variable is not set, serverless complains thus:
A valid environment variable to satisfy the declaration 'env:DOCKERIZE_PIP' could not be found.

The following also does not work, because a boolean literal cannot appear as a default value:

custom:
  pythonRequirements:
    dockerizePip: ${env:DOCKERIZE_PIP, false}

Invalid variable reference syntax for variable false. You can only reference env vars, options, & files. You can check our docs for more info.

So here is my workaround. Externally, the environment variable DOCKERIZE_PIP is either set (for true) or not set for false. If it is not set, we default to a special custom variable ‘false’ to work around the inability to use a boolean literal as a default.

custom:
  false: false
  pythonRequirements:
    dockerizePip: ${env:DOCKERIZE_PIP, self:custom.false}