Skip to content

Commit bd8cf4d

Browse files
authored
Update opensearch pod policy module name (#15)
1 parent cbb003b commit bd8cf4d

File tree

4 files changed

+47
-29
lines changed

4 files changed

+47
-29
lines changed

main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ locals {
7777
module.postgres_admin_login[*],
7878
module.redis_token[*],
7979
module.secret_key[*],
80-
module.opensearch[0].secret_details,
80+
module.opensearch[0][*],
8181
values(module.developer_managed_secrets),
8282
)
8383
}

modules/opensearch/main.tf

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,15 +447,16 @@ module "elasticsearch_secret" {
447447
source = "github.com/thoughtbot/terraform-aws-secrets//secret?ref=v0.4.0"
448448

449449
admin_principals = var.admin_principals
450-
description = "Elastisearch secrets for: ${local.name}"
451-
name = "${local.name}-secret"
450+
description = "Elastisearch secrets for: ${var.application_name}"
451+
name = "${var.application_name}-secret"
452452
read_principals = var.read_principals
453453
resource_tags = var.tags
454454

455455
initial_value = jsonencode({
456-
ES_ENDPOINT = try(aws_opensearch_domain.this[0].endpoint, null)
457-
ES_DASHBOARD_ENDPOINT = try(aws_opensearch_domain.this[0].dashboard_endpoint, null)
458-
ES_DOMAIN_ID = try(aws_opensearch_domain.this[0].domain_id, null)
459-
ES_PASSWORD = var.advanced_security_options.master_user_options.master_user_password
456+
AWS_SEARCH_ENDPOINT = try(aws_opensearch_domain.this[0].endpoint, null)
457+
AWS_SEARCH_DASHBOARD_ENDPOINT = try(aws_opensearch_domain.this[0].dashboard_endpoint, null)
458+
AWS_SEARCH_DOMAIN_ID = try(aws_opensearch_domain.this[0].domain_id, null)
459+
AWS_SEARCH_PASSWORD = var.advanced_security_options.master_user_options.master_user_password
460+
AWS_SEARCH_USER_NAME = var.advanced_security_options.master_user_options.master_user_name
460461
})
461462
}

modules/opensearch/outputs.tf

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,27 @@ output "security_group_id" {
5858
# Secret details
5959
################################################################################
6060

61-
output "secret_details" {
62-
description = "Map containing secret details for opensearch credentials"
63-
value = [
64-
{
65-
name = module.secret.secret_name
66-
environment_variables = ["ES_ENDPOINT", "ES_DASHBOARD_ENDPOINT", "ES_DOMAIN_ID", "ES_PASSWORD"]
67-
policy_json = module.elasticsearch_secret.policy_json
68-
kms_key_arn = module.elasticsearch_secret.kms_key_arn
69-
secret_arn = module.elasticsearch_secret.arn
70-
}
71-
]
72-
}
61+
output "environment_variables" {
62+
description = "Environment variables set by this rotation function"
63+
value = ["AWS_SEARCH_ENDPOINT", "AWS_SEARCH_DASHBOARD_ENDPOINT", "AWS_SEARCH_DOMAIN_ID", "AWS_SEARCH_PASSWORD", "AWS_SEARCH_USER_NAME"]
64+
}
65+
66+
output "secret_name" {
67+
description = "Name of the secrets manager secret containing credentials"
68+
value = module.elasticsearch_secret.name
69+
}
70+
71+
output "policy_json" {
72+
description = "Required IAM policies"
73+
value = module.elasticsearch_secret.policy_json
74+
}
75+
76+
output "kms_key_arn" {
77+
description = "ID of the KMS key used to encrypt the secret"
78+
value = module.elasticsearch_secret.kms_key_arn
79+
}
80+
81+
output "secret_arn" {
82+
description = "ARN of the secrets manager secret containing credentials"
83+
value = module.elasticsearch_secret.arn
84+
}

opensearch.tf

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module "opensearch" {
9090
{ log_type = "SEARCH_SLOW_LOGS" },
9191
]
9292

93-
application_name = var.es_application_name
93+
application_name = local.name
9494

9595
admin_principals = var.es_admin_principals
9696

@@ -146,21 +146,26 @@ resource "random_password" "es" {
146146
special = false
147147
}
148148

149-
resource "aws_iam_role_policy_attachment" "test-attach" {
150-
count = var.elasticsearch_enabled ? 1 : 0
151-
152-
role = module.pod_role.name
153-
policy_arn = "arn:aws:iam::aws:policy/aws-service-role/AmazonElasticsearchServiceRolePolicy"
154-
155-
depends_on = [module.pod_policy]
149+
data "aws_iam_policy_document" "ecs_osis_access" {
150+
statement {
151+
sid = "AllowOpensearchAccess"
152+
resources = ["*"]
153+
actions = [
154+
"ec2:*",
155+
"osis:*",
156+
]
157+
}
156158
}
157159

158160
module "es_pod_policy" {
159161
count = var.elasticsearch_enabled ? 1 : 0
160162
source = "github.com/thoughtbot/flightdeck//aws/service-account-policy?ref=v0.9.0"
161163

162-
name = "es-${var.es_application_name}-pods"
163-
policy_documents = module.opensearch[*].secret_details.policy_json
164+
name = "es-${var.es_application_name}-pods"
165+
policy_documents = concat(
166+
module.opensearch[0][*].policy_json,
167+
[data.aws_iam_policy_document.ecs_osis_access.json]
168+
)
164169

165170
role_names = [module.pod_role.name]
166171
}

0 commit comments

Comments
 (0)