Search code examples
amazon-web-servicesterraformterraform-provider-awsaws-elasticsearchcloudposse

Error: Error creating ElasticSearch domain: ValidationException: You must specify exactly two subnets because you’ve set zone count to two


I got the Error: Error creating ElasticSearch domain: ValidationException: You must specify exactly two subnets because you’ve set zone count to two. But, how to specify exactly two subnets? Here is the code:

main.tf:
module "elasticsearch" {
  source                  = "git::https://github.com/cloudposse/terraform-aws-elasticsearch.git?ref=tags/0.24.1"
  security_groups                = [data.terraform_remote_state.vpc.outputs.default_security_group_id]
  vpc_id                         = data.terraform_remote_state.vpc.outputs.vpc_id
  subnet_ids                     = data.terraform_remote_state.vpc.outputs.private_subnets
  zone_awareness_enabled         = var.zone_awareness_enabled
  elasticsearch_version          = var.elasticsearch_version
  instance_type                  = var.instance_type
  instance_count                 = var.instance_count
  encrypt_at_rest_enabled        = var.encrypt_at_rest_enabled
  dedicated_master_enabled       = var.dedicated_master_enabled
  create_iam_service_linked_role = var.create_iam_service_linked_role
  kibana_subdomain_name          = var.kibana_subdomain_name
  ebs_volume_size                = var.ebs_volume_size
  dns_zone_id                    = var.dns_zone_id
  kibana_hostname_enabled        = var.kibana_hostname_enabled
  domain_hostname_enabled        = var.domain_hostname_enabled

  advanced_options = {
    "rest.action.multi.allow_explicit_index" = "true"
  }
  context = module.this.context
}

terraform.tfvars:

enabled = true
region = "us-west-2"
namespace = "dev"
stage = "pkow"
name = "pkow"
instance_type = "m5.xlarge.elasticsearch"
elasticsearch_version = "7.7"
instance_count = 2
zone_awareness_enabled = true
encrypt_at_rest_enabled = false
dedicated_master_enabled = false
elasticsearch_subdomain_name = "pkow"
kibana_subdomain_name = "pkow"
ebs_volume_size = 250
create_iam_service_linked_role = false
dns_zone_id = "Z080ZFJGLSKFJGLJDLKFGJ"
kibana_hostname_enabled = true
domain_hostname_enabled = true

vpc:

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "2.63.0"
  name                 = var.vpc_name
  cidr                 = var.cidr_blocks_vpc
  azs                  = data.aws_availability_zones.available.names
  private_subnets      = var.private_subnets
  public_subnets       = var.public_subnets
  database_subnets     = var.database_subnets
  elasticache_subnets  = var.elasticache_subnets
  redshift_subnets     = var.redshift_subnets
......

Solution

  • If you don't have any particular preference on the subnets chosen, you can get the first two private ones using slice:

    subnet_ids = slice(data.terraform_remote_state.vpc.outputs.private_subnets, 0, 2)
    

    As long as they are in different AZs it should be enough.