[Terraform] Conditionally Add Key/Value to Map

Image for post
Image for post

Recently, I was setting up a Kubernetes ingress using Terraform, and faced the case when based on an input variable I have to add a new annotation to the ingress configuration or not.

In my use case, I had to specify a security group to be attached to the Load Balancer created by the ingress controller. The thing was that I also wanted an easy way to remove the security group, without changing the Terraform code.

Searching the web, I came across this Reddit post, but the solution proposed by the user Bitflight was not what I really wanted, so I came with a similar approach that I will present next.

Let’s say we have the following local variable named ingress_annotations_required that contains required annotations for the Kubernetes ingress, and another optional variable named security_group_id that can be passed as input variable at apply time.

If the variable security_group_id is set at apply time, we want to add to the ingress annotations the key alb.ingress.kubernetes.io/security-groups with the value provided by the variable security_group_id. To do so, we can set a new local based on a conditional expression that checks if the variable security_group_id holds the default value (which is the empty string "”) or not. If it holds the default value, we set an empty map, if not, we will create a key-value map with our desired information.

Finally, we can merge the two local variables, using the merge function from Terraform.

Then, we can use the formed merged map in the kubernetes_ingreess resource.

Now, when we apply the Terraform script, if we pass the variable security_group_id the security group annotation is added to the map, and implicitly to the Kubernetes ingress. If we don’t pass the variable, the annotation is not set (or it is removed if it was previously set).

Thank you for reading this post, I hope that you found it useful. Here you can find the Gist with the full code from the example presented above.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store