Reputation: 893
Is there any way to get local variables within Terraform console?
> local.name
unknown values referenced, can't compute value
Seems like Terraform console allows only to check input variables and module output variables.
> var.in
2
> module.abc.out
3
Configuration file examples:
# main.tf
locals {
name = 1
}
variable "in" {
value = 2
}
module "abc" {
source "path/to/module"
}
# path/to/module/main.tf
output "out" {
value = 3
}
Upvotes: 28
Views: 62249
Reputation: 26014
You might want to check out terraform-repl, an open source terraform console wrapper written in bash. Among other things, it provides tab completion and it also allows you to view all objects of a certain type (e.g. all locals
).
Given main.tf
:
locals {
a = "foo"
b = {"nums": [
1
]}
}
then:
$ terraform-repl
> local
[
{
"a": "foo",
"b": {
"nums": [
1
]
}
}
]
> local.<TAB>
local.a local.b.nums[0]
> local.b.<TAB>
> local.b.nums[0]
Upvotes: 1
Reputation: 54859
This should work in recent Terraform releases.
$ terraform version
Terraform v1.0.5
$ terraform console
> local.name
1
> var.in
2
And it can be scripted (non-interactive) using Bash here string, for example.
$ terraform console <<<local.name
1
This is might be really useful for custom tooling, and can even be quite sophisticated.
$ terraform console <<<terraform.workspace
"default"
$ terraform console <<<local.credentials[local.stack].username
"user1234"
The tested 'main.tf'
locals {
name = 1
stack = terraform.workspace
credentials={
default: {username:"user1234",password:"1234",endpoint:"http://localhost"}
}
}
variable in {
default = 2
}
Upvotes: 26
Reputation: 25349
There is no REPL on terraform console still as on today(December 2021)
But there is a strongly upvoted request for that, see here on github. If you feel appropriate, you may also go there and upvote.
For me I just wanted to get some items from a list.
For the first item, I could use the element function and do this.
element(["apple", "banana", "pine apple", "grape", "strawberry"], 0)
I get back "apple".
And if want the last item, I have to do the following, using length function
element(["apple", "banana", "pine apple", "grape", "strawberry"],
length(["apple", "banana", "pine apple", "grape", "strawberry"])-1)
I get back "strawberry".
Since there is code repetition here, I was look for something like this, to define a variable and use it.
var fruits = ["apple", "banana", "pine apple", "grape", "strawberry"]
element(fruits, length(fruits)-1)
This is just not working.
So I ended up defining a variable in a file called main.tf as follows.
variable fruits {
default = ["apple", "banana", "pine apple", "grape", "strawberry"]
}
Launched terraform console.
Now I can use it as follows.
element(var.fruits, length(var.fruits)-1)
Some of the relevant examples you may want to look at from my Repo
Upvotes: 2
Reputation: 2430
Unfortunately, it looks like this is not possible in Terraform v0.11.x, but will be in v0.12 as described in this issue ticket:
https://github.com/hashicorp/terraform/issues/18413
HTH!
Upvotes: 7