Terraform contd
Google Style Guide for Terraform
According to Terraform style guide over here https://developer.hashicorp.com/terraform/language/style. How should i name variables
Terraform input variables
- Terraform allows to define variables and use them (which is better than hardcoding)
- Syntax
variable "<LABEL>" {
type = <TYPE>
default = <DEFAULT_VALUE>
description = "<DESCRIPTION>"
sensitive = <true|false>
nullable = <true|false>
}
- Lets create a new tf file called as
inputs.tf
variable "vpc_cidr" {
type = string
description = "vpc cidr"
default = "192.168.0.0/16"
}
# -------------------
# Web Subnet Variables
# -------------------
variable "web_subnet_cidr" {
type = string
description = "web subnet cidr"
default = "192.168.0.0/24"
}
variable "web_subnet_az" {
type = string
description = "web subnet az"
default = "ap-south-1a"
}
# -------------------
# App Subnet Variables
# -------------------
variable "app_subnet_cidr" {
type = string
description = "App subnet CIDR block"
default = "192.168.1.0/24"
}
variable "app_subnet_az" {
type = string
description = "App subnet Availability Zone"
default = "ap-south-1b"
}
# -------------------
# DB Subnet Variables
# -------------------
variable "db_subnet_cidr" {
type = string
description = "DB subnet CIDR block"
default = "192.168.2.0/24"
}
variable "db_subnet_az" {
type = string
description = "DB subnet Availability Zone"
default = "ap-south-1c"
}
- default values will be picked up when we dont pass the value
- variable values can be passed by terraform commands
terraform apply -var 'vpc_cidr=10.0.0.0/16' -var 'web_subnet_cidr=10.0.0.0/24'
- When we have many variables to pass the above command line becomes difficult to manage. so terraform allows to pass a file with variables which has an extension of
.tfvars
- ideally a tfvars file per environment and execute
terraform apply -var-file='default.tfvars'
Terraform meta argument count
Terraform functions