terraform icon indicating copy to clipboard operation
terraform copied to clipboard

terraform repl request: How to define a variable within terraform console?

Open nitrocode opened this issue 7 years ago • 19 comments

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...

nitrocode avatar Oct 09 '18 17:10 nitrocode

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.

mildwonkey avatar Oct 10 '18 20:10 mildwonkey

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.

nitrocode avatar Oct 10 '18 20:10 nitrocode

Thanks for the extra info! I'll make sure this gets labeled appropriately.

mildwonkey avatar Oct 10 '18 22:10 mildwonkey

Any movement on this? Would be very useful.

illegalnumbers avatar Dec 03 '19 21:12 illegalnumbers

@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.

danieldreier avatar Dec 05 '19 20:12 danieldreier

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

EngZaz avatar Feb 25 '21 19:02 EngZaz

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.

Gowiem avatar Apr 23 '21 17:04 Gowiem

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.

jonassteinberg1 avatar Jul 29 '21 20:07 jonassteinberg1

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.

promisepreston avatar Oct 21 '21 23:10 promisepreston

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 avatar Oct 22 '21 17:10 nitrocode

@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.

slime-uk avatar Oct 28 '21 11:10 slime-uk

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..."

armenr avatar Apr 25 '22 13:04 armenr

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

skurhse-rage-nexient avatar Apr 27 '22 17:04 skurhse-rage-nexient

Any moment on this? I might try and create something like my puppet-debugger if nobody else does.

logicminds avatar Aug 16 '22 22:08 logicminds

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

w0rldart avatar Sep 23 '22 09:09 w0rldart

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 avatar Dec 21 '22 16:12 paololazzari

@paololazzari - wow

armenr avatar Dec 21 '22 21:12 armenr

any news on this?

KurtLehnardt avatar Mar 04 '23 02:03 KurtLehnardt

hmmm I though the console was to manipulate and test things but it seems one cannot set a var... interesting.

rightkick avatar Oct 11 '24 05:10 rightkick