尝试使用 terraform 创建多个具有单独路由 53 记录的 EC2 实例

Trying to use terraform to create multiple EC2 instances with separate route 53 records

所以我有一个项目,我正在尝试做一些简单的事情,比如创建一个可以创建以下内容的可重用项目:

EC2 SG - 每个工作负载 1 个 EC2 实例 - 可以是 1 个或多个 Route 53 记录 - 每个创建的 EC2 实例 1 条记录

这个项目工作正常,只使用 1 个实例没有任何问题,但是当我将计数增加到 1 以外的任何值时,我得到以下信息:

Error: Invalid index
│ 
│   on .terraform/modules/ec2_servers_dns_name/main.tf line 18, in resource "aws_route53_record" "this":
│   18:   records = split(",", var.records[count.index])
│     ├────────────────
│     │ count.index is 1
│     │ var.records is list of string with 1 element
│ 
│ The given key does not identify an element in this collection value: the
│ given index is greater than or equal to the length of the collection.

在我的核心 main.tf 中,EC2 和 Route53 的模块如下所示:

# EC2 Instances
module "ec2_servers" {
   ami                         = data.aws_ami.server.id
   associate_public_ip_address = var.ec2_associate_public_ip_address
   disable_api_termination     = var.ec2_disable_api_termination
   ebs_optimized               = var.ec2_ebs_optimized
   instance_count              = var.ec2_servers_instance_count
   instance_dns_names          = var.ec2_servers_dns_name_for_tags
   instance_type               = var.ec2_servers_instance_type
   key_name                    = var.ec2_key_name
   monitoring                  = var.ec2_enhanced_monitoring
   name                        = format("ec2-%s-%s-server",local.tags["application"],local.tags["environment"])
   rbd_encrypted               = var.ec2_rbd_encrypted
   rbd_volume_size             = var.ec2_rbd_volume_size
   rbd_volume_type             = var.ec2_rbd_volume_type
   subnet_id                   = concat(data.terraform_remote_state.current-vpc.outputs.app_private_subnets)
   user_data                   = var.ec2_user_data
   vpc_security_group_ids      = [module.ec2_security_group_servers.this_security_group_id, var.baseline_sg]   

   tags = local.tags
}

# Create DNS entry for EC2 Instances
module "ec2_servers_dns_name" {
    domain          = var.domain_name
    instance_count  = var.ec2_servers_instance_count
    name            = var.ec2_servers_dns_name
    private_zone    = "true"
    records         = module.ec2_servers.private_ip
    ttl             = var.ttl
    type            = var.record_type

    providers = {
      aws = aws.network
    }
}

我们核心模块仓库中的资源(EC2/Route53)如下所示:

EC2

locals {
  is_t_instance_type = replace(var.instance_type, "/^t[23]{1}\..*$/", "1") == "1" ? "1" : "0"
}

resource "aws_instance" "this" {
  count = var.instance_count

  ami                                  = var.ami
  associate_public_ip_address          = var.associate_public_ip_address

  credit_specification {
    cpu_credits = local.is_t_instance_type ? var.cpu_credits : null
  }

  disable_api_termination              = var.disable_api_termination
  ebs_optimized                        = var.ebs_optimized
  iam_instance_profile                 = var.iam_instance_profile  
  instance_initiated_shutdown_behavior = var.instance_initiated_shutdown_behavior
  instance_type                        = var.instance_type
  ipv6_addresses                       = var.ipv6_addresses
  ipv6_address_count                   = var.ipv6_address_count
  key_name                             = var.key_name

  lifecycle {
    ignore_changes = [private_ip, root_block_device, ebs_block_device, volume_tags, user_data, ami]
  }

  monitoring      = var.monitoring
  placement_group = var.placement_group
  private_ip      = var.private_ip

  root_block_device {
    encrypted   = var.rbd_encrypted
    volume_size = var.rbd_volume_size
    volume_type = var.rbd_volume_type
  }

  secondary_private_ips  = var.secondary_private_ips
  source_dest_check      = var.source_dest_check
  subnet_id              = element(var.subnet_id, count.index)
  tags                   = merge(tomap({"Name"= var.name}), var.tags, var.instance_dns_names[count.index])
  tenancy                = var.tenancy  
  user_data              = var.user_data[count.index]
  volume_tags            = var.volume_tags
  vpc_security_group_ids = var.vpc_security_group_ids
}

Route53

data "aws_route53_zone" "this" {
  name         = var.domain
  private_zone = var.private_zone
}

terraform {
  required_providers {
    aws = {
        source  = "hashicorp/aws"
        version = ">= 2.7.0"
    }
  }
}

resource "aws_route53_record" "this" {
  count   = var.instance_count
  name    = var.name[count.index]
  records = split(",", var.records[count.index])
  type    = var.type
  ttl     = var.ttl
  zone_id = data.aws_route53_zone.this.zone_id
}

似乎它可能与 EC2 的私有 IP 的输出有关,但我不确定。这是 EC2 资源的私有 IP 的输出

output "private_ip" {
  description = "The private IP address assigned to the instance."
  value      = [aws_instance.this[0].private_ip]
}

并将R53资源中的records变量设置为列表。

关于如何为 EC2 实例(无论是一个还是多个)提取私有 IP 的任何想法都可以在 R53 模块中动态调用每个私有 IP 的输出,以便可以毫无问题地创建 R53 记录?

尝试

output "private_ip" {
 description = "The private IP address assigned to the instance."
 value      = [aws_instance.this[*].private_ip]
}

第0个returns只有一个元素

编辑:添加这个 tostring()