[英]How to use for_each and count in the same terraform resource
[英]How to set the EC2 resource instance count from a map value in a for_each in Terraform
對於以下 Terraform 代碼 - 我想以 2x testing-sandbox-dev 實例和 1x testing-sandbox-test 實例結束。 我希望能夠從 map 值instance_count
得出計數。
我試過使用count
但 Terraform 不允許for_each
的用戶這樣做。
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
required_version = ">= 0.14.9"
}
variable "instance_name" {
description = "Value of the Name tag for the EC2 instance"
type = string
default = "ChangedName"
}
variable "aws_region" {
description = "AWS Region"
type = string
default = "eu-west-2"
}
variable "instance_size_small" {
description = "Instance size small"
type = string
default = "t3.micro"
}
variable "redundant_count" {
description = "Default redundancy - base number of instances to create for redundant services"
type = number
default = 1
}
variable "ami" {
description = "Ubuntu 20.04 AMI"
type = string
default = "ami-0015a39e4b7c0966f"
}
provider "aws" {
profile = "sandbox"
region = var.aws_region
}
variable "environment_name" {
description = "Environment Name"
type = string
default = "dev"
}
variable "client_name" {
description = "Client Name"
type = string
default = "sandbox"
}
variable "instances" {
description = "Map of modules names to configuration."
type = map
default = {
testing-sandbox-dev = {
instance_count = 2,
instance_type = "t3.micro",
environment = "dev"
},
testing-sandbox-test = {
instance_count = 1,
instance_type = "t3.micro",
environment = "test"
}
}
}
resource "aws_instance" "ec2-instance" {
for_each = var.instances
ami = var.ami
instance_type = each.value.instance_type
tags = {
Name = "${each.key}.${var.client_name}"
client = var.client_name
environment = var.environment_name
}
}
如何從預定義的 map 中指定實例計數?
您必須按如下方式擴展您的var.instances
:
locals {
instances_flat = merge([
for env, val in var.instances:
{
for idx in range(val["instance_count"]):
"${env}-${idx}" => {
instance_type = val["instance_type"]
environment = val["environment"]
}
}
]...)
}
這使:
instances_flat = {
"testing-sandbox-dev-0" = {
"environment" = "dev"
"instance_type" = "t3.micro"
}
"testing-sandbox-dev-1" = {
"environment" = "dev"
"instance_type" = "t3.micro"
}
"testing-sandbox-test-0" = {
"environment" = "test"
"instance_type" = "t3.micro"
}
}
然后
resource "aws_instance" "ec2-instance" {
for_each = local.instances_flat
ami = var.ami
instance_type = each.value.instance_type
tags = {
Name = "${each.value.environment}.${var.client_name}"
client = var.client_name
environment = var.environment_name
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.