You can use Terraform templates to include or omit lines of configuration (or scripts) based on some condition:

this is always present
%{ if condition }
this is present
%{ endif }
this is also always present

If condition, this will print

this is always present

this is present

this is also always present

otherwise, it will print

this is always present

this is also always present

You can avoid the blank lines by carefully setting up your template

this is always present%{ if condition }
this is present%{ endif }
this is also always present

or you can keep the nice formatting by using a strip marker at the start

this is always present
%{~ if condition }
this is present
%{~ endif }
this is also always present