Terraform Series - Variables
All variables start with the keyword variable.
All the variables are in a file awsvars.tf. You can call the file whatever you want.
1) Simple Variables.
2) List.
3) Maps.
A simple variable is like a key-value and can be declared as shown below.
variable "ec2_image" {
description = "Image ID"
type = string
default = "ami-0b5eea76982371e91"
}
variable "aws_region" {
description = "Region in which AWS resources to be created"
type = string
default = "us-east-1"
}
variable "ec2_instance_type" {
description = "EC2 Instance Type"
type = string
default = "t2.micro"
}
variable "ec2_instance_name" {
description = "EC2 Instance Name"
type = string
default = "webserver"
}
Line 1 – Declares the name of the variable – ec2_image. Pattern – variable “Variable_Name”
Line 2 – Declares the type of variable. Optional.
Line 3 – Declares the default value of the variable in case no other value is assigned.
Line 4 – Provide a description of the variable. Optional.
When I use "default" variable its called the value to the variable automatically. No need to mention like var.ec2_instance_name.default.
Call can be like "var.ec2_instance_name".
Lets create ec2_intance.tf and calling the variables:
# Terraform Settings Block
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
#version = "~> 3.21" # Optional but recommended in production
}
}
}
# Provider Block
provider "aws" {
profile = "default" # AWS Credentials Profile configured on your local desktop terminal $HOME/.aws/credentials
region = var.aws_region
}
# Resource Block
resource "aws_instance" "ec2demo" {
ami = var.ec2_image
instance_type = var.ec2_instance_type
tags = { # Using tags to create a VM by the name myfirstvm
"Name" = var.ec2_instance_name
}
}
Terraform CLI defines the following optional arguments for variable declarations:
default - A default value which then makes the variable optional.
type - This argument specifies what value types are accepted for the variable.
description - This specifies the input variable's documentation.
validation - A block to define validation rules, usually in addition to type constraints.
sensitive - Limits Terraform UI output when the variable is used in configuration.
nullable - Specify if the variable can be null within the module.
Maps are a set of key-value pairs. Similar to map data-structure in various languages.
variable "ec2_spec" {
type = map
default = {
"ec2image": "ami-0b5eea76982371e91"
"ec2region": "us-east-1"
"e2type": "t2.micro"
"ec2name": "webserver"
}
description = "Map of Ec2 Spec"
}
# Terraform Settings Block
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
#version = "~> 3.21" # Optional but recommended in production
}
}
}
# Provider Block
provider "aws" {
profile = "default" # AWS Credentials Profile configured on your local desktop terminal $HOME/.aws/credentials
region = lookup(var.ec2_spec,"ec2region")
}
# Resource Block
resource "aws_instance" "ec2demo" {
ami = lookup(var.ec2_spec,"ec2image")
instance_type = lookup(var.ec2_spec,"e2type")
tags = { # Using tags to create a VM by the name myfirstvm
"Name" = lookup(var.ec2_spec,"ec2name")
}
}
We use the "lookup" function to get the map key value.
Comments
Post a Comment