Selfnotes/terraform-variables

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Write a terraform script using aws ec2 instance and github and apply following kind of variables

– Types of Terraform variable – Number

provider “aws” {
region = “us-west-2”
access_key = “”
secret_key = “”
}

resource “aws_instance” “first-ec2” {
ami = “ami-03d5c68bab01f3496” # us-west-2
instance_type = “t2.micro”

tags = {
Name = “Abhishek Reddy”
}
}

variable “numofusers” {
type = number
description = “This is for demo of number variable”
default = 3
}

resource “aws_iam_user” “example” {
count = “${var.numofusers}”
name = “Abhishek Reddy.${count.index}”
}

—————————————————————————————————–

– Types of Terraform variable – String

provider “github” {
token = “”
organization = “devopsschool-sample-projects”
}

variable “reponame” {
type = string
description = “This is for demo of string variable”
default = “day3-broad”
}

resource “github_repository” “example” {
name = “${var.reponame}”
description = “My awesome codebase”
private = false
}

————————————————————————————-

– Types of Terraform variable – List

provider “aws” {
region = “us-west-2”
access_key = “”
secret_key = “”
}

provider “github” {
token = “”
organization = “pwc”
}

variable “users” {
type = “list”
default = [“devopsschool11”, “devopsschool2”, “devopsschool3”]
description = “This is for demo of list variable”
}

variable “gitrepos” {
type = “list”
default = [“devopsschool11”, “devopsschool2”, “devopsschool3”]
description = “This is for demo of list variable”
}

resource “aws_iam_user” “iamuser” {
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
}

———————————————————————————

– Types of Terraform variable – Map

provider “aws” {
region = “us-west-2”
access_key = “”
secret_key = “”
}

resource “aws_instance” “first-ec2” {
ami = “ami-03d5c68bab01f3496” # us-west-2
instance_type = “t2.micro”

tags = {
Name = “Abhishek Reddy”
}
}

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”
}

—————————————————————————-

– Types of Terraform variable – Boolean

provider “aws” {
region = “us-west-2”
access_key = “”
secret_key = “”
}

resource “aws_instance” “first-ec2” {
ami = “ami-03d5c68bab01f3496” # us-west-2
instance_type = “t2.micro”

tags = {
Name = “Abhishek Reddy”
}
}

variable “create_vm” {
description = “If set to true, it will create vm”
type = bool
}

variable “create_vmss” {
description = “If set to true, it will create vmss”
type = bool
}

resource “aws_instance” “example” {
count = var.create_vm ? 1 : 0
name = “example-machine”
resource_group_name = aws_instance.example.name
location = aws_instance.example.location
size = “Standard_F2”

resource “aws_instance” “example” {
count = var.create_vmss ? 1 : 0
name = “example-vmss”
resource_group_name = aws_instance.example.name
location = aws_instance.example.location
sku = “Standard_F2”
instances = 1
admin_username = “adminuser”

….
Then call the submodule like this,

module “vm” {
source = “./modules/vm”
create_vm = true
create_vmss = false
}