Terraform : AWS : Terraform on AWS | Section :5 Terraform Loops, Meta Arguments, Splat

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

 

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

33. Step-01: Implement Variable Lists, Maps and also Meta-Argument 

No changes to c1-version.tf

 

c2-variables.tf

We are going to define an instance type whos instance type is going to be list

# AWS EC2 Instance Type - List
variable "instance_type_list" {
  description = "EC2 Instnace Type"
  type = list(string)
  default = ["t3.micro", "t3.small"]
}


# AWS EC2 Instance Type - Map
variable "instance_type_map" {
  description = "EC2 Instnace Type"
  type = map(string)
  default = {
    "dev" = "t3.micro"
    "qa"  = "t3.small"
    "prod" = "t3.large"
  }
}

---

variable "instance_type_list" {
     description = "EC2  Instance Type"
     type = list(string)
     default = ["t2.micro", "t2.micro"]
}

variable "instance_type_map" {
     description = "EC2 instance type"
     type = map(string)
     default = {
        "dev" = "t2.micro"
        "qa"  = "t3.small"
        "prod" = "t2.micro"
     }
 
}

Step-04: c3-ec2securitygroups.tf and c4-ami-datasource.tf

  • No changes to both files
https://github.com/stacksimplify/terraform-on-aws-ec2/tree/main/05-Terraform-Loops-MetaArguments-SplatOperator/05-01-MetaArgument-Count-For-Loops-Lists-Maps

Go to ec2-instance.tf

 instance_type = var.instance_type_list[]

Here you are going to say which value you want , refer from the list variable to add it here.

variable "instance_type_list" {
     description = "EC2  Instance Type"
     type = list(string)
     default = ["t2.micro", "t2.micro"]
}

default = [0, 1]  -- here the values are going to be so as give here as if in index

Count is nothing but a Meta-Aurgument , which is not your original resource parameter , but is a resource parameter of your AWS instance  .

There are some additional Meta-Arugements present in out of which "Count" , "for_each" these can be used to change the behavior of your resource . one of them is Count

count = 5     -- which will create 5 instances of aws instances


Comments

Popular posts from this blog

Terraform : AWS : Terraform on AWS with SRE & IaC DevOps | Section 2 : Terraform basics

Terraform : AWS : Terraform on AWS | Section 4: Terraform Input Variables and Datasources

Terraform VPC - on AWS : Three tier architecture design