Skip to main content

DATA-TYPE IN VARIABLES

CONDITIONAL STATEMENT EXAMPLE


provider "aws" {

  region     = "us-east-1"

  access_key = "AUGQ"

  secret_key = "KMdfVkt1aurid"

}


variable "image" {

  type    = list

  default = ["ami-02e136e904f3da870", "ami-02e136e904f3da870", "ami-02e136e904f3da870"]


}


variable "instancetype" {

  type    = map

  default = {

    "dev" = "t2.small",

    "test" = "t2.medium",

    "prod" = "t2.large"


}

}



variable input {}


resource "aws_instance" "dev" {

  instance_type = var.instancetype["dev"]

  ami = var.image[0]

  count = var.input == "dev" ? 1 : 0


  tags = {

   Name = "dev-dep"


}


}


resource "aws_instance" "test" {

  instance_type = var.instancetype["test"]

  ami = var.image[1]

  count = var.input == "test" ? 2 : 0


  tags = {

   Name = "test-dep"

}

}




resource "aws_instance" "prod" {

  instance_type = var.instancetype["prod"]

  ami = var.image[2]

  count = var.input == "prod" ? 3 : 0


  tags = {

   Name = "dev-dep"

}

}

EQUAL TO



provider "aws" {
  region     = "us-east-1"
  access_key = "AUGQ"
  secret_key = "KfTMdfVkt1aurid"
}

variable "image" {
  type    = list
  default = ["ami-02e136e904f3da870", "ami-02e136e904f3da870", "ami-02e136e904f3da870"]

}

variable "instancetype" {
  type    = map
  default = {
    "dev" = "t2.small",
    "test" = "t2.medium",
    "prod" = "t2.large"

}
}


variable input {}

resource "aws_instance" "dev" {
  instance_type = var.instancetype["dev"]
  ami = var.image[0]
  count = var.input == "dev" ? 1 : 0                              #if input is equal to dev create 1 if not then 0

  tags = {
   Name = "dev-dep"



 GREATER THAN EQUAL TO



provider "aws" {

  region     = "us-east-1"

  access_key = "AKGQ"

  secret_key = "Kkt1aurid"

}


variable "image" {

  type    = list

  default = ["ami-02e136e904f3da870", "ami-02e136e904f3da870", "ami-02e136e904f3da870"]


}


variable "instancetype" {

  type    = map

  default = {

    "dev" = "t2.small",

    "test" = "t2.medium",

    "prod" = "t2.large"


}

}



variable input {}


resource "aws_instance" "dev" {

  instance_type = var.instancetype["dev"]

  ami = var.image[0]

  count = var.input >= "2" ? 1 : 0


  tags = {

   Name = "dev-dep"


}

}


NOT EQUAL TO 

provider "aws" {
  region     = "us-east-1"
  access_key = "AUGQ"
  secret_key = "KVkt1aurid"
}

variable "image" {
  type    = list
  default = ["ami-02e136e904f3da870", "ami-02e136e904f3da870", "ami-02e136e904f3da870"]

}

variable "instancetype" {
  type    = map
  default = {
    "dev" = "t2.small",
    "test" = "t2.medium",
    "prod" = "t2.large"

}
}


variable input {}

resource "aws_instance" "dev" {
  instance_type = var.instancetype["dev"]
  ami = var.image[0]
  count = var.input != "2" ? 2 : 0     

  tags = {
   Name = "dev-dep"



Count Parameter

 provider "aws" {

  region     = "us-east-1"

  access_key = "AKQ"

  secret_key = "KfdfVkt1aurid"

}


variable "instancetags" {

  type    = list

  default = ["root", "user1", "user2"]

}


variable "instancetype" {

  type    = list

  default = ["t2.nano", "t2.micro", "t2.medium"]

}






resource "aws_instance" "cloud-instan" {

  ami           = "ami-02e136e904f3da870"

  instance_type = var.instancetype[count.index]

  count         = 3

  tags          = {

    Name        = var.instancetags[count.index]


Comments

Popular posts from this blog

Packer Environment Variable

Note:  1011  export AWS_ACCESS=AKGQ                         do this on console not on editor  1012  export AWS_SECRET=KfVkt1aurid  1013  echo $AWS_ACCESS  1014  AKVUGQ   vim moon.json {     "variables": {       "aws_access_key": "{{env `AWS_ACCESS`}}",       "aws_secret_key": "{{env `AWS_SECRET`}}"     },        "builders": [            {            "type": "amazon-ebs",            "region": "us-east-1",            "access_key": "{{user `aws_access_key`}}",            "secret_key": "{{user `aws_secret_key`}}",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870", ...

copying snap from one REGION to another

 while copying snap from one to another we need to change region provider "aws" {   region     = "us-west-1"   access_key = "AGQ"   secret_key = "Kurid" } resource "aws_ebs_snapshot_copy" "example_copy" {   source_snapshot_id = "snap-08bc5c27dad8e82b3"   source_region      = "us-east-1"   tags = {     Name = "HelloWorld_copy_snap"   } }

File Provisioner

 Note: file provisioner is used to copy file we have created one file vim clod now how to checck its been copied  go to ami and launch image and create instance  vi pro.json {        "builders": [            {            "type": "amazon-ebs",            "region": "us-east-1",            "access_key": "AUGQ",            "secret_key": "Kurid",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870",            "ssh_username": "ec2-user",            "ami_name": "moon-amiii"            }        ],        "provisioners": [                {   ...