Terraform Variables Tutorials – Day 2 20 Oct 2023
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
# variables.tf
variable "aws_region" {
type = string
default = "us-east-1"
}
# main.tf
resource "aws_instance" "example" {
ami = "ami-01234567890abcdef0"
instance_type = "t2.micro"
key_name = "my-key-pair"
region = var.aws_region
}
variable "instance_count" {
type = number
default = 1
}
variable "instance_name" {
type = string
default = "Rajesh"
}
resource "aws_instance" "example-number" {
count = var.instance_count
ami = "ami-0f5ee92e2d63afc18"
instance_type = "t2.micro"
tags = {
Name = var.instance_name
}
}
variable "security_groups" {
type = list(string)
default = ["sg-0718505e2e0301294"]
}
resource "aws_instance" "example-list" {
ami = "ami-0f5ee92e2d63afc18"
instance_type = "t2.micro"
vpc_security_group_ids = var.security_groups
}
variable "instance_tags" {
type = map(string)
default = {
Name = "my-instance4mMap-Rajesh"
}
}
resource "aws_instance" "example-map" {
ami = "ami-0f5ee92e2d63afc18"
instance_type = "t2.micro"
tags = var.instance_tags
}
variable "create_vm" {
description = "If set to true, it will create vm"
type = bool
}
resource "aws_instance" "example-bool" {
count = var.create_vm ? 1 : 0
ami = "ami-0f5ee92e2d63afc18"
instance_type = "t2.micro"
tags = var.instance_tags
}