Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!
We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!
Learn from Guru Rajesh Kumar and double your salary in just one year.
1. Types of Terraform variable - Number
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-west-2"
access_key = "xxxxxxxxxxx"
secret_key = "xxxxxxxxxxxxxxxxxxx"
}
variable "usercount" {
type = number
description = "This is for demo of number variable"
default = 3
}
resource "aws_instance" "anitha" {
count = "${var.usercount}"
ami = "ami-03d5c68bab01f3496"
instance_type = "t2.micro"
tags = {
Name = "anitha.${count.index}"
}
}
2. Types of Terraform variable - String
provider "aws" {
region = "us-west-2"
access_key = "xxxxxxxxxxxx"
secret_key = "xxxxxxxxxxxxxxx"
}
resource "aws_instance" "first-ec2" {
ami = "ami-03d5c68bab01f3496" # us-west-2
instance_type = "t2.micro"
tags = {
Name = "anitha"
}
}
3. Types of Terraform variable – List
terraform {
required_providers {
aws = {
source = ""hashicorp/aws"
version = "~> 3.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-west-2"
access_key = "xxxxxxxxxxxxx"
secret_key = "xxxxxxxxxx"
}
variable "users" {
type = list
default = ["devopsschool11", "devopsschool2", "devopsschool3"]
description = "This is for demo of list variable"
}
resource "aws_instance" "anitha" {
ami = "ami-03d5c68bab01f3496"
instance_type = "t2.micro"
tags = {
Name = "${var.users[0]}"
}
}
4. Types of Terraform variable – Map
provider “aws” {
region = “us-west-2”
access_key = “xxxxxxxxx”
secret_key = “xxxxxxxxxxxx”
}
resource “aws_instance” “first-ec2” {
ami = “ami-03d5c68bab01f3496” # us-west-2
instance_type = “t2.micro”
tags = {
Name = “anitha”
}
}
variable “amis” {
type = “map”
default = {
“us-east-1” = “ami-b374d5a5”
“us-west-2” = “ami-4b32be2b”
}
}
resource “aws_instance” “example” {
ami = var.amis[var.region]
instance_type = “t2.micro”
}