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
@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.
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.
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.
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.
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:
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.
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.