Counts in Terraform

Main.tf

provider "aws" {
   region = "us-west-2"
}

resource "aws_resource" "servers" {
   count = "4"
   ami = "ami-0e472933a1395e172"
   instance_type = "t3.micro"
   tags = {
     Name  = "Server Number is ${count.index +1}"
     Owner = "Denis Astahov"
   }   
}

Output.tf how to print resources that is generated by counts

output "instance_ids" {
  value = aws_instance.servers[*].id
}

output "instance_public_ips" {
  value = aws_instance.servers[*].public_ip
}

What if we need to provision user list, first add lists to variables tf and

variables.tf

variable "aws_users" {
  description = "List of IAM Users to create"
  default = [
    "denis@astahov.net",
    "krisa@astahov.net",
    "kevin@astahov.net",
    "jessy@astahov.net",
    "robby@astahov.net",
    "katie@astahov.net",
    "laura@astahov.net",
    "josef@astahov.net"
  ]
}

main.tf

resource "aws_iam_user" "user" {
  count = length(var.aws_users)
  name  = element(var.aws_users, count.index)
}

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir