如何根据Terraform中for_each中的映射值设置EC2资源实例计数 [英] How to set the EC2 resource instance count from a map value in a for_each in Terraform

查看:9
本文介绍了如何根据Terraform中for_each中的映射值设置EC2资源实例计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下Terraform代码-我希望以2x测试-Sandbox-dev实例和1x测试-沙箱-测试实例结束。我希望能够从映射值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
  }
}

如何从预定义映射指定实例计数?

推荐答案

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

这篇关于如何根据Terraform中for_each中的映射值设置EC2资源实例计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆