【问题标题】:Unable to add 2 subnets for an ElasticSearch with Terraform无法使用 Terraform 为 ElasticSearch 添加 2 个子网
【发布时间】:2019-10-28 21:38:49
【问题描述】:

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

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

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

但是当我尝试这样做时,我得到了这个错误:

错误: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,但您现有的示例很难看到。一般来说,您应该尝试提供minimal reproducible example,以便人们实际重现您的问题。
  • 好的,我编辑了帖子并包含了所有代码

标签: amazon-web-services elasticsearch terraform terraform0.12+


【解决方案1】:

您在使用多可用区 Elasticsearch 集群时需要 cluster_config 中缺少 zone_awareness_enabled parameter

【讨论】:

  • 谢谢你,我添加了它,现在它可以工作了。但我还必须添加 dedicated_master_count 因为它是必需的。
  • 所以terraform官网的例子有问题terraform.io/docs/providers/aws/r/…
  • 它使用了 2 个子网,但它们没有提供 zone_awareness_enable 参数
  • dedicated_master_count 不是必填字段,除非您还指定有关专用母版的其他字段。很可能有一个没有专用主节点的 ES 集群(实际上我运行了一些)。
  • 我向 terraform 官网提交了一个 PR 以更新示例以包含 zone_awareness_enabled 参数。你可以给它一个大拇指来增加它的优先级:github.com/hashicorp/terraform-provider-aws/pull/20627
【解决方案2】:

感谢@ydaetskcoR 指路。

我将分享我在配置 availability_zone_countsubnet_ids 时遇到的困难 - 希望它可以为其他人节省一些时间。

问题的一些背景:

A) 我尝试创建一个多区 ES 集群。

B) 我有 4 个用于数据层的子网(也包含其他类型的 DB),并且希望在当前区域中的可用 AZ(3 个 AZ)之间拆分集群 - 所以其中一个 AZ 将有 2 个子网和 2 个 ES 实例。

请注意:

1:zone_awareness_config 块下的 availability_zone_count 字段应该具有与可用 AZ 一样的确切数量。

2:vpc_options 块下的subnet_ids 字段应包含您在availability_zone_count 下指定的相同数量的可用区。

所以,一句话:availability_zone_count == (available AZs) == length( subnet_ids)

以下是包含相关部分的代码 sn-p(也请关注 cmets - 它也可能会为您节省一些时间):

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://stackoverflow.com/questions/47229247/validationexception-before-you-can-proceed-you-must-enable-a-service-linked-ro (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."
}

【讨论】:

    猜你喜欢
    • 2019-05-16
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 2017-11-13
    • 2018-04-02
    • 2021-12-29
    • 1970-01-01
    相关资源
    最近更新 更多