terraform script with veriables
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
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â
}