Skip to content

Commit cbb003b

Browse files
authored
Use vpc subnet count to set availability zone count for opensearch (#14)
1 parent 00213d9 commit cbb003b

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

modules/opensearch/main.tf

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ data "aws_iam_session_context" "current" {
1515
}
1616

1717
locals {
18-
account_id = try(data.aws_caller_identity.current[0].account_id, "")
19-
partition = try(data.aws_partition.current[0].partition, "")
20-
region = try(data.aws_region.current[0].name, "")
18+
account_id = try(data.aws_caller_identity.current[0].account_id, "")
19+
create_access_policy = var.create && var.create_access_policy && (length(var.access_policy_statements) > 0 || length(var.access_policy_source_policy_documents) > 0 || length(var.access_policy_override_policy_documents) > 0)
20+
create_cloudwatch_log_groups = var.create && var.create_cloudwatch_log_groups
21+
create_security_group = var.create && var.create_security_group && length(var.vpc_options) > 0
22+
23+
partition = try(data.aws_partition.current[0].partition, "")
24+
region = try(data.aws_region.current[0].name, "")
25+
26+
security_group_name = try(coalesce(var.security_group_name, var.domain_name), "")
2127

2228
static_domain_arn = "arn:${local.partition}:es:${local.region}:${local.account_id}:domain/${var.domain_name}"
2329

2430
tags = merge(var.tags, { terraform-aws-modules = "opensearch" })
2531

26-
name = "es-${var.application_name}"
32+
name = "es-${var.application_name}"
2733
}
2834

2935
################################################################################
@@ -206,10 +212,6 @@ resource "aws_opensearch_domain" "this" {
206212
# Access Policy
207213
################################################################################
208214

209-
locals {
210-
create_access_policy = var.create && var.create_access_policy && (length(var.access_policy_statements) > 0 || length(var.access_policy_source_policy_documents) > 0 || length(var.access_policy_override_policy_documents) > 0)
211-
}
212-
213215
resource "aws_opensearch_domain_policy" "this" {
214216
count = var.create && var.enable_access_policy && (local.create_access_policy || var.access_policies != null) ? 1 : 0
215217

@@ -326,10 +328,6 @@ resource "aws_opensearch_outbound_connection" "this" {
326328
# Cloudwatch Log Group
327329
################################################################################
328330

329-
locals {
330-
create_cloudwatch_log_groups = var.create && var.create_cloudwatch_log_groups
331-
}
332-
333331
resource "aws_cloudwatch_log_group" "this" {
334332
for_each = { for opt in var.log_publishing_options : opt.log_type => opt if try(opt.enabled, true) && local.create_cloudwatch_log_groups }
335333

@@ -385,11 +383,6 @@ resource "aws_cloudwatch_log_resource_policy" "this" {
385383
# Security Group
386384
################################################################################
387385

388-
locals {
389-
create_security_group = var.create && var.create_security_group && length(var.vpc_options) > 0
390-
security_group_name = try(coalesce(var.security_group_name, var.domain_name), "")
391-
}
392-
393386
data "aws_subnet" "this" {
394387
count = local.create_security_group ? 1 : 0
395388

opensearch.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ module "opensearch" {
5858
instance_type = coalesce(var.es_instance_type, var.es_dedicated_master_type)
5959

6060
zone_awareness_config = {
61-
availability_zone_count = 2
61+
availability_zone_count = length(module.network.private_subnet_ids) > 3 ? 3 : length(module.network.private_subnet_ids)
6262
}
6363

6464
zone_awareness_enabled = true
@@ -101,7 +101,7 @@ module "opensearch" {
101101
}
102102

103103
vpc_options = {
104-
subnet_ids = module.network.private_subnet_ids
104+
subnet_ids = length(module.network.private_subnet_ids) > 3 ? slice(module.network.private_subnet_ids, 0, 3) : module.network.private_subnet_ids
105105
}
106106

107107
# Security Group rule example

0 commit comments

Comments
 (0)