Reputation: 949
Does terraform provide such functionality to override variable values? Let suppose I have declared two variables given below.
variable "foo" {}
variable "bar" { default = "false"}
foo
is mandatory and bar
assigned with default value false. Is there any resoruce
available in terraform in which I can reassign or override the bar
value? I'm asking this in resource
perspective. I know I could do this using terraform-modules
.
I've tried this use null_resource
but didn't get an expected results. It still returns the default value.
resource "null_resource" "this" {
provisioner "local-exec" {
command = "echo ${var.env} > ${var.newvar}"
}
}
Also I wanted to run curl
in command attribute. Do I need to use an `interpreter. if so then what would be its vaule?
interpreter = ["shell","?"]
what sete of values should I pass to execute curl
command in local-exec
provisioner.
bash script
function check_efs() {
curl -ls https://elasticfilesystem.us-east-1.amazonsaws.com
if [ $? -eq 0 ]; then
output=1
else:
output=0
}
function produce_output() {
value=$(output)
jq -n \
--arg is_efs_exist "$value" \
'{"is_efs_exist":$is_efs_exist}'
}
check_efs
produce_output
Upvotes: 5
Views: 18145
Reputation: 2430
[Answering the 1st question about overrides...]
There are a few ways you could tackle this. I don't know your exact use case, so your mileage may vary.
You could export an environment variable that has the value you'd like to use. For example, you could set the bar
var to a new value with export TF_VAR_bar=newvalue
and then run terraform
in that session. Or combine them on the same line: TF_VAR_bar=newvalue terraform apply
Ref: https://www.terraform.io/docs/configuration/environment-variables.html
Use an override file. E.g.:
override.tf
could contain variable "bar" { default = "newvalue"}
or any other TF code. It's loaded last.
Ref: https://www.terraform.io/docs/configuration/override.html
Put your code into a TF module, then you could call the module and pass along the value of bar
that you'd like. This is particularly useful if you keep re-using the same code and wanting to provision different instances of some set of resources with varying parameters.
Ref: https://www.terraform.io/docs/configuration/modules.html
HTH!
Upvotes: 5