serverless-plugin-ifelse icon indicating copy to clipboard operation
serverless-plugin-ifelse copied to clipboard

Set stage based on environment variable

Open hbarcelos opened this issue 5 years ago • 4 comments

I'm combining the use of this plugin with serverless-dotenv-plugin. ifelse is declared AFTER dotenv:

plugins:
  - serverless-webpack
  - serverless-dotenv-plugin
  - serverless-plugin-ifelse

Then I have the following:

  chainId: ${env:CHAIN_ID, "42"}
  serverlessIfElse:
    - If: '"${self:custom.chainId}" == "1"'
      Set:
        provider.stage: mainnet
      ElseSet:
        provider.stage: kovan

Even though the following line is logged when I run serverless deploy,

> Serverless: serverless-plugin-ifelse - Value Changed for : provider.stage to: mainnet

the stack created use kovan as the value set, as the CloudFormation template contains things like:

    "HelloLogGroup": {
      "Type": "AWS::Logs::LogGroup",
      "Properties": {
        "LogGroupName": "/aws/lambda/linguo-bot-kovan-hello"
      }
    },

So basically even though the plugin says it changed the stage value, it has no effect.

hbarcelos avatar Aug 11 '20 13:08 hbarcelos

I am observing the same issue:

custom:
  paramstage: ${self:provider.stage}

  serverlessIfElse:
    - If: '"${opt:stage, self:provider.stage}" == "local"'
      Set:
        custom.paramstage: 'dev'
      ElseSet:
        custom.paramstage: ${self:provider.stage}

which when printed resolves correctly, however, the values are not applied to custom.paramstage and the end result is that the value of the custom.paramstage is always it was initially set to

paramstage: local
 serverlessIfElse:
   - If: '"local" == "local"'
     Set:
       custom.paramstage: dev
     ElseSet:
       custom.paramstage: local

vdarevskOTF avatar Apr 15 '21 16:04 vdarevskOTF

I've found that environment variables cannot be evaluated by this plugin.

This check to see if an envar is set fails:


custom:
  serverlessIfElse:
    - If: '"${env:CI_NAME}" != ""'
      Set:
        provider.tags.CI: true

But leaning the check up against a native sls property that evaluates envars works:


provider:
  tags:
    CI_NAME: ${env:CI_NAME, 'na'}
...
  serverlessIfElse:
    - If: '"${self:provider.tags.CI_NAME}" != "na"'
      Set:
        provider.tags.CI: true

So I'm getting what I need atm anyhow, but it would be nice not to have to rely on another property to shim this functionality.

AlexAtkinson avatar Dec 02 '22 20:12 AlexAtkinson