Terraform – Meta参数Count的使用
Meta参数中的count可以批量创建ec2服务器,但是需要注意的是,有些东西是无法批量创建的,比如AWS IAM User 同样的名字是无法重复创建的,所以名字需要加上count.index
使用案例:
terraform {
  required_version = ">= 1.0.0" # Ensure that the Terraform version is 1.0.0 or higher
  required_providers {
    aws = {
      source = "hashicorp/aws" # Specify the source of the AWS provider
      version = "~> 5.0"        # Use a version of the AWS provider that is compatible with version
    }
  }
}
provider "aws" {
  region = "us-west-2"
}
resource "aws_instance" "aws_example" {
  instance_type = "t4g.nano"
  ami           = "ami-005e54dee72cc1d00"
  tags = {
    Name = "ExampleInstance_${count.index}"
  }
  count = 3
}
terraform plan
会创建3个ec2资源,分别是以下
– aws_instance.aws_example[0]
– aws_instance.aws_example[1]
– aws_instance.aws_example[2]

使用 count.index 可以为每个资源创建独立的名字

 ![]()
		Facebook评论