【问题标题】:How to set the EC2 resource instance count from a map value in a for_each in Terraform如何根据 Terraform 中 for_each 中的地图值设置 EC2 资源实例计数
【发布时间】:2022-01-19 19:52:18
【问题描述】:

对于以下 Terraform 代码 - 我希望得到 2x testing-sandbox-dev 实例和 1x testing-sandbox-test 实例。我希望能够从地图值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
  }
}

如何从预定义的地图中指定实例数?

【问题讨论】:

  • 如果我理解这个问题,你想使用for_eachcount,从 Terraform 文档中你可以看到这是不可能的:Note: A given resource or module block cannot use both count and for_each.terraform.io/language/meta-arguments/…
  • 是的。我知道它们不能一起使用。但是是否有可能以其他方式实现相同的行为?
  • 不确定,对我来说,如果可能的话,这听起来很复杂。有两个 aws_instance 块有什么问题? :)

标签: amazon-web-services terraform terraform-provider-aws


【解决方案1】:

您必须按如下方式扩展您的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
  }
}

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2020-04-22
    • 2019-12-16
    • 2023-02-08
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多