anantab / serverless-plugin-ifelse

A Serverless Plugin to write If Else conditions in serverless YAML file
69 stars 19 forks source link

Set stage based on environment variable #21

Open hbarcelos opened 3 years ago

hbarcelos commented 3 years ago

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.

vdarevskOTF commented 3 years ago

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
AlexAtkinson commented 1 year ago

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.