Terraform

Testing with variables in Terraform

After installation of Terraform you can practice with simple variable test.

1.As a first step you have to create main.tf file

#vi main.tf

variable “myvar” {
type = string
default = “hello terraform”
}

variable “mymap” {
type = map(string)
default = {
mykey = “myvalue”
}
}

variable “mylist” {
type = list
default = [1,2,3]
}

2.Testing variables

#terraform console

>var.myvar
“hello terraform”

> “${var.myvar}”
“hello terraform”

> var.mymap
tomap({
“mykey” = “myvalue”
})

>var.mymap[“mykey”]
“myvalue”

> “${var.mymap[“mykey”]}”
“myvalue”

>var.mylist
tolist([
1,
2,
3,
])


>var.mylist[0]
1
> element(var.mylist, 1)
2
>element(var.mylist, 0)
1
>slice(var.mylist, 0, 2)
tolist([
1,
2,
])
>exit

3. Testing with AWS variables

As part of next exercise create below files with content.

#cat sesource.tf

#cat terraform.tfvars

Now type “terraform console” , you will encounter with below error

You have to install provider plugin , here its aws . Use

#terraform init

4.Now test with variables

Tagged

Leave a Reply