The serverless.yml file is a powerful tool that allows for dynamic property values. But what about the need to implement if then logic in serverless.yml? The serverless-plugin-ifelse npm module saves the day.
How to Implement If Then Logic in serverless.yml
The serverless-plugin-ifelse npm module is a life-saver. It allows you to implement if then logic in serverless.yml and conditionally determine which portions of that file are set or excluded. In this article, I will walk through a few basic examples that demonstrate how to implement if then logic in serverless.yml.
The serverless framework alleviates virtually all of the pain associated with provisioning AWS CloudFormation stacks. One of the most well thought out aspects of this framework is serverless.yml. This file provides a straightforward way to configure your CloudFormation stack in code. It also supports dynamic properties (e.g. “stage”). But it will not be long before you find yourself wishing to include or exclude certain portions of your serverless.yml file, or set specific values based on a dynamic context such as stage. This is when it pays to know how to implement if then logic in serverless.yml.
Example 1 – package.json
https://gist.github.com/kevinchisholm/265e28c5f2b23e1db673b9eb6dc1ef4c
In Example # 1, we have the package.json file needed by our service. We set the serverless-plugin-ifelse npm module as a dependency, so just be aware that you will need to run the command npm install in the root of your service folder before deploying your service.
Example 2 – Exclude and ElseExclude
https://gist.github.com/kevinchisholm/38ba7fe30ed2a0238f6d1e018b8a9a3e
In Example # 2, we have leveraged the Exclude and ElseExclude features of the serverless-plugin-ifelse npm module in order to implement if then logic in serverless.yml. Let’s dig into the details:
- We have two functions: fooFunction and barFunction.
- If the stage IS “prod” when we deploy, then exclude fooFunction
- If the stage is NOT “prod” when we deploy, then exclude barFunction
The end result is: you will wind up with two functions deployed.
Here are the endpoints you should see:
- https://XXX.execute-api.us-east-1.amazonaws.com/dev/foo
- https://XXX.execute-api.us-east-1.amazonaws.com/prod/bar
Example 3 – Set and ElseSet
For Examples 3-A, 3-B and 3-C we will have one function. That function shows a message. The message is a combination of a static value and a dynamic value. The dynamic value is determined by our stage when we deploy. In this example, we will use Set and ElseSet in order to implement if then logic in serverless.yml.
Example 3-A
https://gist.github.com/kevinchisholm/65d6566a4d01d3971b0a367c0b899538
In Example 3-A we have our serverless.yml file. Starting on line 15, here is the logic:
- If the stage IS “prod” when we deploy, the dynamicMessage value is: “this is a dynamic PROD message”
- If the stage is NOT “prod” when we deploy, the dynamicMessage value is: “this is a dynamic DEV/TEST message”
Example 3-B
https://gist.github.com/kevinchisholm/45347779e2ee0572595234bdabc13390
In Example 3-B we have our lambda. This handler returns an HTTP response and the message property is a combination of the staticMessage and the dynamicMessage.
Example 3-C
https://gist.github.com/kevinchisholm/0f49c52632e6bf3ffd325d26e0d1ff24
In Example 3-C we have two messages that you see in the browser, depending on which endpoint you hit. As expected, the dynamic message differs, based on the deployment stage.
Summary
One of the biggest challenges of serverless.yml is that it is not a context that supports programming logic. The serverless-plugin-ifelse npm module provides a way to implement if then logic in serverless.yml. This provides a great deal of power with regard to dynamically including, excluding or setting values based on the deployment stage.
In this article, we saw that it’s not difficult to implement if then logic in serverless.yml, thereby conditionally setting serverless framework properties. We hope that you have enjoyed reading about this valuable capability and that you’ve learned something new. If you have and it has made your coding easier and more efficient, please share this article on Facebook and Twitter.