Skip to content

paololazzari/terraform-repl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

terraform-repl

terraform-repl is a terraform console wrapper that aims at providing a better interactive console for evaluating Terraform language expressions.

Motivation

The standard terraform console has several limitations, including:

  • No tab completion
  • No ability to view all objects
  • No ability to define variables
  • No ability to view command history
  • No ability to clear screen
  • No ability to run system commands

terraform-repl provides a solution to these problems.

How it works

By default, terraform-repl evaluates each expression against a new terraform console process.

You can, however, choose to use a hashicorp/terraform docker container as a single background process to evaluate the expressions entered. This can be faster if working with terraform projects of a certain size. You can enable the container backend by using the -docker-container-backend option.

Usage

Starting the REPL

To start the REPL:

$ terraform-repl
>

or with the container backend:

$ terraform-repl -docker-container-backend
Starting terraform console docker container...

or starting the terraform-repl docker container:

$ docker run --rm -it -v "$(pwd)":/data plazzari/terraform-repl
>

Using the REPL

Given the following main.tf file:

locals {
  a = "foo"
  b = {"nums": [
    1,
    2
  ]}
}

To inspect the locals:

> local
[
  {
    "a": "foo",
    "b": {
      "nums": [
        1,
        2
      ]
    }
  }
]

To inspect one value:

> local.b.nums[0]
1

To create new variables:

> local.c="example"
> local.c
"example"
> local.d={"nums": \
[ 3, 4 ] \
}
> local.d
{
  "nums" = [
    3,
    4,
  ]
}

To view the command history:

> history
    1  local
    2  local.b.nums[0]
    3  local.c="example"
    4  local.c
    5  local.d={"nums": [ 3, 4 ] }
    6  local.d
    7  history

All terraform functions can be run as usual:

> element(local.d.nums,0)
3

To run an external command, prepend the command with !:

> !grep "locals" main.tf
locals {

To clear the screen:

> clear

To exit:

> exit

Tab completion

You can use the TAB key to get autocompletion for terraform functions:

> e<TAB>
endswith element
> el<TAB>
> element

as well as for local variables:

> local.<TAB>
local.a  local.b.nums[0]  local.b.nums[1]

this can be useful when you are working with many local variables.

Transcript

When you start the REPL you may also specify the -transcript option. This will generate a transcript of all inputs and outputs of the session. For the commands demonstrated above, the transcript file would be as follows:

Sample transcript
> local
[
  {
    "a": "foo",
    "b": {
      "nums": [
        1,
        2
      ]
    }
  }
]
> local.b.nums[0]
1
> local.c="example"
> local.c
"example"
> local.d={"nums": [ 3, 4 ] }
> local.d
{
  "nums" = [
    3,
    4,
  ]
}
> element(local.d.nums,0)
3

Prerequisites

* optional, although highly recommended

Installation

$ curl -O https://raw.githubusercontent.com/paololazzari/terraform-repl/master/terraform-repl
$ cp terraform-repl /usr/local/bin/
$ chmod +x /usr/local/bin/terraform-repl

About

A terraform console wrapper for a better REPL experience

Topics

Resources

License

Stars

Watchers

Forks