使用Terraform无法为ElasticSearch添加2个子网

12

我正在尝试使用Terraform构建ElasticSearch集群,但我无法分配多个子网!这真的很奇怪,因为文档中有这个内容:

https://www.terraform.io/docs/providers/aws/r/elasticsearch_domain.html#subnet_ids

subnet_ids-(必填)要在其中创建Elasticsearch域端点的VPC子网ID列表。

但是,当我尝试执行时,出现以下错误:

Error: ValidationException:您必须指定一个子网

这是我的代码:

resource "aws_elasticsearch_domain" "es" {
  domain_name           = "${var.es_domain}-${var.environment}"
  elasticsearch_version = "${var.es_version}"

  cluster_config {
    instance_type  = "${var.es_instance_type}"
    instance_count = "${var.es_instance_count}"
  }
  vpc_options {

    subnet_ids = ["${data.aws_subnet.private_1.id}", "${data.aws_subnet.private_2.id}"]

    security_group_ids = ["${aws_security_group.es.id}"]
  }

  snapshot_options { automated_snapshot_start_hour = "${var.es_automated_spanshot_start_hour}" }

  ebs_options {
    ebs_enabled = true
    volume_type = "standard"
    volume_size = "20"
  }


  access_policies = <<CONFIG
    {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": "es:*",
        "Principal": "*",
        "Effect": "Allow",
        "Resource": "arn:aws:es:${var.aws_region}:${data.aws_caller_identity.current.account_id}:domain/${var.es_domain}/*"
      }
    ]
}
CONFIG


}

我正在使用 terraform v0.12.2

感谢您的帮助。


你能分享一下完整的aws_elasticsearch_domain资源代码吗? 我猜你漏了zone_awareness_enabled,但是以你现有的例子很难看出来。总的来说,你应该尽量提供一个 [mcve] 使人们可以真正复制你的问题。 - ydaetskcoR
好的,我已经编辑了帖子并包含了所有的代码。 - user1297406
2个回答

15

谢谢,我已经添加了它,现在它可以工作了。但是我还必须添加dedicated_master_count,因为它是必需的。 - user1297406
2
所以在 Terraform 官网的示例 https://www.terraform.io/docs/providers/aws/r/elasticsearch_domain.html#vpc-based-es 中存在问题。 - user1297406
它使用了2个子网,但他们没有提供zone_awareness_enable参数。 - user1297406
1
dedicated_master_count 不是必需的字段,除非您还指定了有关专用主节点的其他字段。完全可以在没有专用主节点的情况下运行ES集群(实际上我运行了几个)。 - ydaetskcoR
我向terraform官方网站提交了一个PR,以更新示例以包括zone_awareness_enabled参数。您可以点赞以提高其优先级:https://github.com/hashicorp/terraform-provider-aws/pull/20627 - Yann Stoneman

2
感谢@ydaetskcoR指出的方法。
我将分享我在配置availability_zone_countsubnet_ids时遇到的问题 - 希望这能为其他人节省一些时间。
以下是问题的背景:
A)我尝试创建一个多可用区ES集群。
B)我有4个子网用于数据层(还包含其他类型的数据库),并希望集群在当前区域中的可用AZ之间分割 - 因此其中一个AZ将具有2个子网和2个ES实例。
请注意以下几点:
1:在zone_awareness_config块下的availability_zone_count字段应该与可用的AZ数量完全一致。
2:在vpc_options块下的subnet_ids字段应该包含您在availability_zone_count下指定的同样数量的AZ。
因此,简而言之:availability_zone_count == (available AZs) == length( subnet_ids) 以下是相关部分的代码片段(还请遵循注释 - 这可能也可以为您节省一些时间):
resource "aws_elasticsearch_domain" "staging" {
    domain_name  = ...
    vpc_options{
       subnet_ids = "${local.subnet_ids}"  # Instead of: [for s in aws_subnet.data_tier : s.id] which will lead to: Error creating ElasticSearch domain: ValidationException: You must specify exactly three subnets because you’ve set zone count to three.

    }
    cluster_config {
       zone_awareness_enabled = true #If you ignore it you'll get: Error creating ElasticSearch domain: ValidationException: You must specify exactly one subnet
       #Notice that there is no "=" Below - or you'll visit this thread: https://github.com/terraform-providers/terraform-provider-aws/issues/12365
       zone_awareness_config {
         availability_zone_count = "${length(var.region_azs)}"
       }
    }
    .
    . 
}

#Take only X number of subnets where X is the number of available AZs)
locals {
  subnet_ids = "${slice(aws_subnet.data_tier.*.id, 0, length(var.region_azs))}"
}  


# Added this also due to: Error creating ElasticSearch domain: ValidationException: Before you can proceed, you must enable a service-linked role to give Amazon ES permissions to access your VPC.
# Solved with: https://dev59.com/VVYN5IYBdhLWcg3wlI7G (Terraform related Answer)
resource "aws_iam_service_linked_role" "es" {
  aws_service_name = "es.amazonaws.com"
  description      = "Allows Amazon ES to manage AWS resources for a domain on your behalf."
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接