In this cloud era, every other resources existence is dependent on another resource. Which is nothing but conditional existence. In this blog, we will discuss about the same, How to create resource conditionally in terraform using conditional expressions.
We will discuss about the same by going through examples with two different use cases. Firstly using conditionals to decide arguments and then using conditionals to create resources.
Let’s understand what is an expression. An expression is literally an algebraic equation which evaluates to either true or false only. Then we will have two expressions one will get invoked when the expression is true and the other when it is false.
expression ? true_invocation : false_invocation
The above shown is the syntax of conditional expression which finally execute either true invocation or false based on expression. The expression and invocations are seperated by question mark (?) While the two invocations are seperated by a colon. Major thing you should understand is that the invocations can’t be more than one line.
Here firstly, the condition is environment_type == “prod”, which means it will invoke true invocation if it is prod environment, otherwise false. True invocation is a string with vm size set to f4s_v2 while the false statement set the vm size to DS1_v2. Since the variable value is dev, hence the condition deduces to false and the size of our vm becomes DS1_v2.
creating resource conditionally:
By conditional resource, it means that the resource will be created only if some condition is true. Let’s say, In azure terms, only premium ACR can have a private endpoint. Here the private endpoint can only be created if ACR is premium otherwise not.
This can be achieved using the count and expression. As we discussed here in other blog about count. count decides how many times a resource gets created. So if you give zero, the resource won’t get created. That zero or one, we will deduce via an expression based on premium or standard. The below example shows the same.
Hope you understand, how you can create a resource conditionally. Please comment you thoughts. Thanks for reading, happy infra.
My name is sherika from ForeMedia and I help website owners with great websites to maximize the revenue they make from their website.
I would love to buy Ad Spaces on your website and pay you per each impression of the ads on your site. We are Google Ad Exchange Premium partners and we work with more than 100 Ad Network & thousands of premium direct advertisers who would LOVE to buy Ads directly from your site and we can gurantee to increase your revenue by at least 45% compared to other advertising solutions you are currently using.
You deserve to earn more for your content and maximize your earning potential and I will help you unlock that potential!
There is no cost, you can register to our monetization platform for free on this link: bit.ly/3Vj7rly
I will be available for you on the Chat if you need any help if you have any questions.
I have at least 17 Premium Advertisers that have asked me to approach you because they would like to advertise on your website on Premium eCPM [cost per impressions] basis.
Have a good day & feel free to ping me for any question!
Click the link here for 1 minute registration to our platform [it’s free!]: bit.ly/3Vj7rly
Will talk over mail.