With terraform we can use variables for example an integer which is defined as a number or a string or a list of strings and then we can reference these variables further down in the code in order to reuse them you can also output variables to the screen and we could also ask the customer to input interactively variable values into the console
``` In this session
variables
terraform pattern consistent
in curly braces the parametesrs like cidr_block = "10.0.0.0/16"
terraform init
pull down modules
terraform plan
checks state
terraform apply
yes to continueresources created in aws!
terraform destroy
confirm
state file terraform.tfstate
variablesdefault is like the valuetype and then default for default valuemain.tf create file in that folder
```json
variable "vpcname" {
type = string (or number boolean list(string) with values ["val1", "val2"] to access 1 [0])
default = "myvpc"
}
variable "mymap" { type = map default = { key1 = "value1" key2 = "value2" } }
variable "mytuple" { type tuple([string, number, string]) default = ["mystr1", 45, "mystr2"] }
variable "myobject" { type = object({name = string, port = number}) default { name = "myval1" default = "232" } } ``` - cidr_range would get used multiple times so just put it in var
use variables
var.myvarname can also be done with string interpolationvar.mylist[0]var.mymap["Key1"]
interactive input variables
json
variable "inputname" {
type = string
description = "please enter value for input name"
}
next use this
- var.inputname as usuall
- terraform plan will now ask to input the input vars values
json
output "myoutput" {
value = aws\_vpc.myvpc.id
}
so nw we can print the dynamic id we get when we run terraform.
run terraform plan will not print the id as it's created only after the apply is run and resources actually run.
```