terraform repl request: How to define a variable within terraform console?
Trying to define variables or modules or data resources or templates or anything within the terraform console? Is this not supported or planned to be supported? or am I doing something wrong?
$ echo 'variable "count" {default = 2}' | terraform console
parse error at 1:12: expected "}" but found opening quote
Edit: This is the only one I could find online https://www.katacoda.com/courses/terraform/playground but you have to sign up for it...
Hi @nitrocode!
I'm sorry you ran into this unexpected behavior. The terraform console is intended to be used with existing terraform configuration files, or as an interactive console for interpolation functions. It doesn't support defining resources, as you discovered. You could put the variable declaration in a file and then open the console to work with it.
If you share some of the details of your use case we could label this as an enhancement request.
Hi @mildwonkey. I expected terraform console to be more of a repl than interfacing with existing configuration files.
My use case is to explore terraform's functions in a sandbox repl environment before configuring files. It would be nice to declare variables, resources, templates, data, etc directly from the console so I can play with functions faster in order to implement them into .tf files. It makes a huge difference in my productivity in languages like python, csharp, ruby, nodejs, etc.
Thanks for the extra info! I'll make sure this gets labeled appropriately.
Any movement on this? Would be very useful.
@illegalnumbers if someone wants to make a PR to make the console a better REPL, I think we're open to that functionality, but it's not a short-term priority for the core team. @illegalnumbers or @nitrocode is this something you're interested in building and contributing to the codebase?
If so we should work out the details of expected behavior, e.g. by writing up some sample interactions, and then one of you should self-assign this and propose an implementation prior to putting up a PR so we can give engineering feedback early on. I think making the console better would be a great improvement.
I was doing some testing with terraform functions and was trying to use Variables within terraform console but didn't find a way to do it. it would be nice if we can add variables and be able to use them within the terraform console
I'm a terraform instructor and I find when teaching Terraform to students that the console is usually a head scratcher for a lot of folks. It is expected to work as a normal REPL like most other languages have, but instead it has very little utility. I've started bringing up less and less in courses that I'm teaching.
I'd love to see this worked on and moved forward as part of a v1.0 for Terraform. I think it's easy to think about this type of thing as low priority, but in reality it would likely improve the lives of many folks banging on their terraform codebases.
Writing and debugging advanced Terraform is exceedingly difficult and slow. A REPL is really needed. Terraform is way passed basic declarative syntaxes at this point.
Kept running into multiple errors when trying to define a variable in the terraform console.
> school = "my school"
> ╷
│ Error: Extra characters after expression
│
│ on <console-input> line 1:
│ (source code not available)
│
│ An expression was successfully parsed, but extra characters were found after
│ it.
Thought I was doing things wrongly on the terraform console, only to drop in here and realize that the terraform console isn't a REPL. Feels really disappointing as I hoped to test out to some terraform functions on some variables before adding them to my project.
This is really essential for the terraform so that one can try out some functions/expressions in the command-line before adding to their project.
What I've been doing is creating a temporary file so we can take advantage.
$ echo 'variable "school" { default = "my school" }' > temp_vars.tf
$ terraform console
> var.school
my school
> exit
@nitrocode When I try that it just says "known after apply" :( Any ideas but I'm using TF Cloud and trying to do this locally with the same repo used.
Kept running into multiple errors when trying to define a variable in the terraform console.
> school = "my school" > ╷ │ Error: Extra characters after expression │ │ on <console-input> line 1: │ (source code not available) │ │ An expression was successfully parsed, but extra characters were found after │ it.Thought I was doing things wrongly on the terraform console, only to drop in here and realize that the terraform console isn't a REPL. Feels really disappointing as I hoped to test out to some terraform functions on some variables before adding them to my project.
This is really essential for the terraform so that one can try out some functions/expressions in the command-line before adding to their project.
^^ This. SO much this. At least maybe just a fixtured/emulated playground where you can assign some vars & types, then operate on them safely...instead of losing hours on successive "try this, run this, wait for plan, try again..."
locals workaround with a for expression:
#!/usr/bin/env bash
set -e; rm -rf /tmp/tf-console-test/; mkdir -p $_; cd $_
terraform console <<EOF
[for local in [{toast="rye",jam="blueberry"}] : join(" ", [local.toast, "toast", "and", local.jam, "jam"])][0]
EOF
tmp file equivalent:
#!/usr/bin/env bash
set -e; rm -rf /tmp/tf-console-test/; mkdir -p $_; cd $_
cat <<EOF >test.tf
locals {
toast = "rye"
jam = "blueberry"
}
EOF
terraform console <<EOF
join(" ", [local.toast, "toast", "and", local.jam, "jam"])
EOF
Any moment on this? I might try and create something like my puppet-debugger if nobody else does.
I'm stuck with the same issue as some have pointed out.
I have
variable "project_name" {
type = string
description = "This project's name"
default = "expenv"
}
variable "rg_location" {
type = string
description = "Specifies the supported Azure location where the Logic App Workflow exists"
default = "North Europe"
}
but inside terraform console, var.project_name or var.rg_location outputs (known after apply)
I'm using terraform 1.3.0 at the moment, but have tried it on some 1.2.X versions, and the result is the same
I've been working on a terraform console wrapper that, among other things, allows you define variables on the fly:
https://github.com/paololazzari/terraform-repl
@paololazzari - wow
any news on this?
hmmm I though the console was to manipulate and test things but it seems one cannot set a var... interesting.