TERRAFORM WITH VARIABLES

Write a terraform script using AWS ec2 instance and Github and apply following kind of variables

  • Types of Terraform variable – Number
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "3.49.0"
    }
  }
}
provider "aws" {
  region  = "us-west-2"
  access_key = ""
  secret_key = ""
}


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

resource "aws_instance" "anubhab" {
  ami            = "ami-03d5c68bab01f3496"
  instance_type  = "t2.micro"

  tags  = {
    Name = "anubhab"
  }
}
provider "github" {
  token        = "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"
 
}
resource "github_repository" "anubhab" {
  name        = "pwc-anubhab-page"
  description = "My awesome codebase"

  private = false
}
  • Types of Terraform variable – String
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "3.49.0"
    }
  }
}
provider "aws" {
  region  = "us-west-2"
  access_key = ""
  secret_key = ""
}


variable "reponame" {
  type = string
  description = "This is for demo of string variable"
  default = "day3-broad"
}
resource "aws_instance" "anubhab" {
  ami            = "ami-03d5c68bab01f3496"
  instance_type  = "t2.micro"

  tags  = {
    Name = "anubhab"
  }
}
provider "github" {
  token        = "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"
 
}
resource "github_repository" "anubhab" {
  name        = "pwc-anubhab-page"
  description = "My awesome codebase"

  private = false
}
  • 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 = ""
  secret_key = ""
}


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

resource "aws_instance" "anubhab" {
  ami           = "ami-03d5c68bab01f3496"
  instance_type = "t2.micro"
  tags = {
    Name = "anubhab"
  } 
  • Types of Terraform variable – Map
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "3.49.0"
    }
  }
}
provider "aws" {
  region  = "us-west-2"
  access_key = ""
  secret_key = ""
}


variable "amis" {
  type = map
  default = {
    
    "us-west-2" = "ami-03d5c68bab01f3496"
  }
}

resource "aws_instance" "anubhab" {
  ami            = "ami-03d5c68bab01f3496"
  instance_type  = "t2.micro"

  tags  = {
    Name = "anubhab"
  }
}
provider "github" {
  token        = "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"
 
}
resource "github_repository" "example" {
  name        = "pwc-anubhab-page"
  description = "My awesome codebase"

  private = false
}
  • Types of Terraform variable – Boolean
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "3.49.0"
    }
  }
}
provider "aws" {
  region  = "us-west-2"
  access_key = ""
  secret_key = ""
}


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" "anubhab" {
  ami            = "ami-03d5c68bab01f3496"
  instance_type  = "t2.micro"

  tags  = {
    Name = "anubhab"
  }
}
provider "github" {
  token        = "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"
 
}
resource "github_repository" "anubhab" {
  name        = "pwc-anubhab-page"
  description = "My awesome codebase"

  private = false
}