TERRAFORM VARIABLES WITH EXAMPLES:
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
provider.tf file:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-west-2"
access_key = ""
secret_key = ""
}
# Configure the GitHub Provider
provider "github" {
token = "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"
}
Variable number:
variable "usercount" {
type = number
description = "This is for demo of number variable"
default = 3
}
resource "aws_instance" "judith" {
count = "${var.usercount}"
ami = "ami-03d5c68bab01f3496"
instance_type = "t2.micro"
tags = {
Name = "judith.${count.index}"
}
}
Variable string:
variable "reponame" {
type = string
description = "This is for demo of string variable-judith"
default = "day3-broad"
}
resource "github_repository" "string variable" {
name = "${var.reponame}"
description = "My awesome codebase"
private = false
}
Variable list:
variable "users" {
type = list
default = ["devopsschool1", "devopsschool2", "devopsschool3"]
description = "This is for demo of list variable"
}
variable "gitrepos" {
type = list
default = ["devopsschool1", "devopsschool2", "devopsschool3"]
description = "This is for demo of list variable"
}
resource "aws_iam_user" "judith" {
name = "${var.users[0]}"
}
resource "github_repository" "repo1" {
name = "${var.gitrepos[0]}"
description = "My awesome codebase"
private = false
}
resource "github_repository" "repo2" {
name = "${var.gitrepos[1]}"
description = "My awesome codebase"
private = false
}
resource "github_repository" "repo3" {
name = "${var.gitrepos[2]}"
description = "My awesome codebase"
private = false
}
map file
variable "account_name" {
type = map
default = {
"account1" = "devops1"
"account2" = "devops2"
"account3" = "devops3"
}
}
resource "aws_iam_user" "iamuser" {
for_each = var.account_name
name = "${each.value}-iam"
}
resource "aws_instance" "judith-ec2" {
ami = "0fb0225bafc2f76c5" # us-west-2
instance_type = "t2.micro"
tags = {
Name = "judith"
}
}
boolean file: