Reputation: 83
In terraform, is there any way to conditionally execute some lines of code in template file? For example:
I have a test.sh.tpl file defined as below called from main.tf:
#!/bin/bash
echo ${test_key} > /opt/test_key.properties
In the above value to "test_key" is assigned from variable
Now I want to execute the above code only if ${test_key} value is not empty. I tried this, but not working even the value exists
{ if ${test_key} != "" }
echo ${test_key} > /opt/test_key.properties
{ endif }
Upvotes: 8
Views: 17906
Reputation: 247
probably because you are missing % before you brackets.
%{ if test_key != "" }
echo ${test_key} > /opt/test_key.properties
%{ endif }
https://www.terraform.io/docs/language/expressions/strings.html
NOTE: The test_key
is not in ${}
since we are already evaluating expressions as code.
Upvotes: 12