Difference between revisions of "Terraform meta argument: count"

From wikieduonline
Jump to navigation Jump to search
 
(37 intermediate revisions by 7 users not shown)
Line 1: Line 1:
* https://www.terraform.io/docs/language/meta-arguments/count.html
+
* <code>count</code> <ref>https://www.terraform.io/docs/language/meta-arguments/count.html</ref> [[meta argument]]
  
 +
* count.index
  
 +
== Examples ==
 +
* <code> count = [[length]](var.[[subnet_ids]])</code>
 +
* <code> [[count]]                = var.[[vpc_cidr_block]] != null ? 1 : 0</code>
 +
* <code>count = [[local.]]create_node_sg ? 1 : 0</code>
 +
* <code> count = var.istest == true ? 3 : 0</code>
  
  count = length(var.[[subnet_ids]])
+
* <code> count = 0 </code>
  
  [[count]]               = var.[[vpc_cidr_block]] != null ? 1 : 0
+
Terraform does not allow to use count inside [[module block]].<ref>https://registry.terraform.io/modules/terraform-aws-modules/s3-bucket/aws/latest</ref>
 +
 
 +
  resource "[[aws_instance]]" "dev" {
 +
    ami = "ami-082b5a644766e0e6f"
 +
    instance_type = "t2.micro"
 +
    count = 2
 +
}
 +
 
 +
resource "aws_instance" "your_new_instance_in_test" {
 +
    ami = "ami-082b5a644766e0e6f"
 +
    instance_type = "t2.micro"
 +
    count = [[var]].istest == true ? 3 : 0
 +
}
 +
 
 +
resource "aws_instance" "your_new_conditional_instance {
 +
    count = var.your_env == "PRE" ? 1 : 0
 +
    ami = "ami-082b5a644766e0e6f"
 +
    instance_type = "t2.micro"
 +
}
 +
 
 +
 
 +
variable "elb_names" {
 +
  type = list
 +
  default = ["dev-loadbalancer", "stage-loadbalanacer","prod-loadbalancer"]
 +
}
 +
 
 +
resource "aws_iam_user" "lb" {
 +
  name = var.elb_names[count.index]
 +
  count = 2
 +
  path = "/system/"
 +
}
 +
 
 +
 
 +
resource "aws_ecs_service" "your_service" {
 +
count = [[var.]]your_env_name == "PROD" ? 1 : 0
 +
.../...
 +
[[task_definition]] = "${aws_ecs_task_definition.dashboards_task[0].family}:${aws_ecs_task_definition.dashboards_task[0].revision}"
 +
.../...
 +
}
 +
 
 +
 
 +
== Error ==
 +
* <code>[[Error: Unexpected resource instance key]]</code>
 +
* <code>[[Error: Missing resource instance key]]</code>
 +
* [[Error: Invalid index]]
 +
 
 +
== Related ==
 +
* <code>[[for_each]]</code>
 +
* <code># [[aws_instance]].your-instance-name has moved to aws_instance.your-instance-name[0]</code>
 +
* [[Terraform conditional expressions]]
 +
* <code>[[count.index]]</code>
  
 
== See also ==
 
== See also ==
* {{Terraform}}
+
* {{count}}
 +
* {{terraform functions}}
 +
* {{Terraform meta-arguments}}
  
 
[[Category:Terraform]]
 
[[Category:Terraform]]

Latest revision as of 10:13, 26 June 2023

  • count.index

Examples[edit]

  • count = 0

Terraform does not allow to use count inside module block.[2]

resource "aws_instance" "dev" {
   ami = "ami-082b5a644766e0e6f"
   instance_type = "t2.micro"
   count = 2
}
resource "aws_instance" "your_new_instance_in_test" {
   ami = "ami-082b5a644766e0e6f"
   instance_type = "t2.micro"
   count = var.istest == true ? 3 : 0
}
resource "aws_instance" "your_new_conditional_instance {
   count = var.your_env == "PRE" ? 1 : 0
   ami = "ami-082b5a644766e0e6f"
   instance_type = "t2.micro"
}


variable "elb_names" {
  type = list
  default = ["dev-loadbalancer", "stage-loadbalanacer","prod-loadbalancer"]
}
 
resource "aws_iam_user" "lb" {
  name = var.elb_names[count.index]
  count = 2
  path = "/system/"
}


resource "aws_ecs_service" "your_service" {
count = var.your_env_name == "PROD" ? 1 : 0
.../...
task_definition = "${aws_ecs_task_definition.dashboards_task[0].family}:${aws_ecs_task_definition.dashboards_task[0].revision}"
.../...
}


Error[edit]

Related[edit]

See also[edit]

  • https://www.terraform.io/docs/language/meta-arguments/count.html
  • https://registry.terraform.io/modules/terraform-aws-modules/s3-bucket/aws/latest
  • Advertising: