# Conditional serverless.yml based on stage?

**URL:** <https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763>\
**Category:** Serverless Framework\
**Tags:** aws\
**Created:** [April 25, 2017, 7:59pm UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763 "2017-04-25T19:59:19Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![DJHoltkamp](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DJHoltkamp](https://forum.serverless.com/u/DJHoltkamp)\
**Post date:** [April 25, 2017, 7:59pm UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/1 "2017-04-25T19:59:19Z")

</div>

I need to make it so a certain scheduled events do not run unless they are running on our prod stage. Is there any syntax that cam be used in serverless.yml to make it so it automatically checks this when running. For instance something like

enabled : ${opt:stage == prod}

Thanks!

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [April 25, 2017, 11:33pm UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/2 "2017-04-25T23:33:26Z")

</div>

Conditional logic isn’t supported.

If you know all of the environments you’re deploying to then you could do something like:

```auto
custom:
  enabled:
    dev: false
    prod: true

enabled: ${self:custom.enabled.${opt:stage, self:provider.stage}}

```

---

<div class="post-metadata">

**Author:** ![DJHoltkamp](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DJHoltkamp](https://forum.serverless.com/u/DJHoltkamp)\
**Post date:** [April 26, 2017, 1:17am UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/3 "2017-04-26T01:17:52Z")

</div>

Fantastic! Not conditional, but that works great for our case as we have 3 set stage names. Thanks for the help!

---

<div class="post-metadata">

**Author:** ![ezeeetm](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/ezeeetm/32/947_2.png) [@ezeeetm](https://forum.serverless.com/u/ezeeetm)\
**Post date:** [August 30, 2017, 2:47am UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/4 "2017-08-30T02:47:37Z")

</div>

@buggy @DJHoltkamp you can totally do conditions in serverless .yml, and mappings too!

```
resources:
  Mappings:
    config:
      global:
        key: value
      dev:
        AuroraInstanceSize: db.t2.small
      test:
        AuroraInstanceSize: db.t2.small
      qa:
        AuroraInstanceSize: db.t2.small
      prod:
        AuroraInstanceSize: db.t2.medium
      

  Conditions:
    IsProd:
      Fn::Equals:
        - ${opt:stage}
        - prod  

   Resources:
      SomeIAMRole:
         Type: AWS::IAM::Role
         Condition: IsProd
         Properties:
            etc
            etc
```

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [September 4, 2017, 5:48am UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/5 "2017-09-04T05:48:55Z")

</div>

@ezeeetm You can simulate conditional logic in serverless.yml by placing the stage name into the path for a variable then defining the value for every possible stage. I’ve written about that many times including the solution I provided here. It’s good enough for most people but it’s not the same as IF x THEN y ELSE z conditional logic.

CloudFormation is more flexible but it only helps you if you need the conditional logic in the CloudFormation template generated by Serverless. If you need the conditional logic as part of running the Serverless command (for example the plugins to include) then you need to use the stage in the path trick.

---

<div class="post-metadata">

**Author:** ![alechewitt](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/alechewitt/32/964_2.png) [@alechewitt](https://forum.serverless.com/u/alechewitt)\
**Post date:** [September 14, 2017, 3:41am UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/6 "2017-09-14T03:41:05Z")

</div>

Building on the solution by @buggy. If you don’t know all the environments you will be deploying to you could do something like this:

```
custom:
  enabled:
    dev: true
    prod: true
    other: false

enabled: ${self:custom.enabled.${opt:stage}, self.enabled.other}

```

So the `other` stage become all other stages apart from `dev` and `prod`

---

<div class="post-metadata">

**Author:** ![jorgt](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/jorgt/32/955_2.png) [@jorgt](https://forum.serverless.com/u/jorgt)\
**Post date:** [October 15, 2017, 9:52pm UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/7 "2017-10-15T21:52:01Z")

</div>

Perfect, just what I was looking for 👍

---

<div class="post-metadata">

**Author:** ![ezeeetm](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/ezeeetm/32/947_2.png) [@ezeeetm](https://forum.serverless.com/u/ezeeetm)\
**Post date:** [October 16, 2017, 2:08pm UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/8 "2017-10-16T14:08:24Z")

</div>

> [@buggy](#):
>
> Conditional logic isn’t supported.

I was responding more to this. Cloudformation conditional logic is supported, and (as you say) a bit more flexible than the variable replacement provided by sls. I follow a ‘write once run everywhere’ highly parameterized pattern for my infrastructure code, so sometimes I have 30 or more params for a given stack…cfn mappings and conditions are really the best way to handle that kind of parameterizations.

to be fair, I came to sls from many years of cfn, so I view sls more as a tool for making it easier to work with cfn, while others who came to sls first might just look at cfn as ‘the poo that sls spits out’, and prefer to stay in native sls as much as possible, which makes sense.

---

<div class="post-metadata">

**Author:** ![iDVB](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/idvb/32/990_2.png) [@iDVB](https://forum.serverless.com/u/iDVB)\
**Post date:** [November 29, 2017, 4:47pm UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/9 "2017-11-29T16:47:47Z")

</div>

@alechewitt  
Not sure what is happening in your example…

Is the “self.enabled.other” part a typo?  
Should it not be…  
"${self:custom.enabled.other}"

also… I didn’t know it was legit to set vars outside of “custom”?

---

<div class="post-metadata">

**Author:** ![alechewitt](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/alechewitt/32/964_2.png) [@alechewitt](https://forum.serverless.com/u/alechewitt)\
**Post date:** [March 12, 2018, 5:27am UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/10 "2018-03-12T05:27:38Z")

</div>

@iDVB apologies for the late reply.

I was writing out the case where you want `dev` and `prod` to be enabled but all other stages to be disabled.

With your configuration looking something like this:

```auto
custom:
  enabled:
    dev: true
    prod: true
    other: false

enabled: ${self:custom.enabled.${opt:stage}, self.enabled.other}

```

If you deployed to a stage called `uat` then `self:custom.enabled.${opt:stage}` gets evaluated as `self:custom.enabled.uat` which does not exist. Therefore it will use the default value of `self.enabled.other`, which is `false`.

I wrote it like this because in some situations you might not know all the different stages you are going to be deploying to ahead of time. Therefore you can fallback onto a default value.

---

<div class="post-metadata">

**Author:** ![gpa](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gpa](https://forum.serverless.com/u/gpa)\
**Post date:** [November 4, 2019, 6:14pm UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/12 "2019-11-04T18:14:13Z")

</div>

As always with Serverless the biggest problem here is that there is zero documentation. I really wish they’d do more to document their tool, its all guess-work and trying to infer syntax from CloudFormation docs. My company chose in their wisdom to use Serverless and everything is an utter nightmare.

---

<div class="post-metadata">

**Author:** ![chris](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/chris/32/279_2.png) [@chris](https://forum.serverless.com/u/chris)\
**Post date:** [August 7, 2020, 10:45pm UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/13 "2020-08-07T22:45:37Z")

</div>

This was helpful, as I was looking for this kind of thing as well. I’d agree that for a lot of uses, the solution of having settings for each stage in the `custom` section is the way to go. But, I needed to conditional a ton of stuff in the `resources` section. I just wanted to add a small bit to what @ezeeetm’s example provides.

When you create the named condition in the `Conditions` section of `resources`, you then need to specify that named condition for each resource you want to condition. Here’s an example:

```auto
resources:
  Conditions:
    # Only create the VPC setup in the dev account/stage
    CreateVPCResources:
      Fn::Equals:
        - ${self:provider.stage}
        - dev
  Resources:
    MyVPC:
      Type: AWS::EC2::VPC
      Condition: CreateVPCResources
      Properties:
        CidrBlock: 10.0.0.0/16

```

Any resources in there that don’t specify that `Condition: ...` attribute will of course not be affected. More info can be found in the [CloudFormation docs for Conditions](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html).

---

<div class="post-metadata">

**Author:** ![limawebdev1](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/limawebdev1/32/5092_2.png) [@limawebdev1](https://forum.serverless.com/u/limawebdev1)\
**Post date:** [January 18, 2021, 3:30pm UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/14 "2021-01-18T15:30:20Z")

</div>

I think @iDVB was wondering about the syntax for the `self.enabled.other`. I think it should be `self:custom.enabled.other`, otherwise self is referring to the top-level attribute.

---

<div class="post-metadata">

**Author:** ![s1mrankaur](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/s1mrankaur/32/4850_2.png) [@s1mrankaur](https://forum.serverless.com/u/s1mrankaur)\
**Post date:** [March 2, 2021, 1:09am UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/15 "2021-03-02T01:09:36Z")

</div>

@alechewitt@ezeeetm What if I need to do logical NOT of the variable. For ex: NOT OF ${self.custom.disabled} . Please suggest

---

<div class="post-metadata">

**Author:** ![jakob.ege](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@jakob.ege](https://forum.serverless.com/u/jakob.ege)\
**Post date:** [March 12, 2021, 9:09pm UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/16 "2021-03-12T21:09:36Z")

</div>

you guys are great thank you for your replies this helped me pretty much

---

<div class="post-metadata">

**Author:** ![jakob.ege](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@jakob.ege](https://forum.serverless.com/u/jakob.ege)\
**Post date:** [March 13, 2021, 10:20pm UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/17 "2021-03-13T22:20:48Z")

</div>

you guys are great. Thank you for your replies this helped me pretty much  
[snaptube](https://snaptube) [vidmate](https://vidmate)

---

<div class="post-metadata">

**Author:** ![curzio-della-santa](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/curzio-della-santa/32/7503_2.png) [@curzio-della-santa](https://forum.serverless.com/u/curzio-della-santa)\
**Post date:** [June 5, 2023, 9:16am UTC](https://forum.serverless.com/t/conditional-serverless-yml-based-on-stage/1763/18 "2023-06-05T09:16:10Z")

</div>

You can use [serverless-plugin-utils](https://www.serverless.com/plugins/serverless-plugin-utils) fn:[ternary](https://github.com/icarus-sullivan/serverless-plugin-utils/blob/master/documentation/ternary.md)

enabled : ${ternary(opt:stage, ‘prod’, true, false)}
