diff --git a/.github/workflows/opensearch.yml b/.github/workflows/opensearch.yml new file mode 100644 index 0000000..5c93d3f --- /dev/null +++ b/.github/workflows/opensearch.yml @@ -0,0 +1,23 @@ +name: opensearch +on: + pull_request: + branches: + - main + paths: + - opensearch/** + types: + - closed + - opened + - reopened + - synchronize +jobs: + terraform: + uses: ./.github/workflows/terraform.yml + concurrency: ${{ github.workflow }} + with: + module: opensearch + permissions: + id-token: write + contents: write + checks: write + pull-requests: write diff --git a/opensearch/README.md b/opensearch/README.md new file mode 100644 index 0000000..60f511d --- /dev/null +++ b/opensearch/README.md @@ -0,0 +1,104 @@ + +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.6.2 | +| [aws](#requirement\_aws) | ~> 5.0 | + +## Providers + +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | ~> 5.0 | + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [elasticsearch\_secret](#module\_elasticsearch\_secret) | github.com/thoughtbot/terraform-aws-secrets//secret | v0.8.0 | + +## Resources + +| Name | Type | +|------|------| +| [aws_cloudwatch_log_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource | +| [aws_cloudwatch_log_resource_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_resource_policy) | resource | +| [aws_opensearch_domain.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/opensearch_domain) | resource | +| [aws_opensearch_domain_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/opensearch_domain_policy) | resource | +| [aws_opensearch_domain_saml_options.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/opensearch_domain_saml_options) | resource | +| [aws_opensearch_outbound_connection.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/opensearch_outbound_connection) | resource | +| [aws_security_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource | +| [aws_vpc_security_group_egress_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_security_group_egress_rule) | resource | +| [aws_vpc_security_group_ingress_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_security_group_ingress_rule) | resource | +| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | +| [aws_iam_policy_document.cloudwatch](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_iam_policy_document.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_iam_session_context.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_session_context) | data source | +| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source | +| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source | +| [aws_subnet.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet) | data source | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [access\_policies](#input\_access\_policies) | IAM policy document specifying the access policies for the domain. Required if `create_access_policy` is `false` | `string` | `null` | no | +| [access\_policy\_override\_policy\_documents](#input\_access\_policy\_override\_policy\_documents) | List of IAM policy documents that are merged together into the exported document. In merging, statements with non-blank `sid`s will override statements with the same `sid` | `list(string)` | `[]` | no | +| [access\_policy\_source\_policy\_documents](#input\_access\_policy\_source\_policy\_documents) | List of IAM policy documents that are merged together into the exported document. Statements must have unique `sid`s | `list(string)` | `[]` | no | +| [access\_policy\_statements](#input\_access\_policy\_statements) | A map of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) for custom permission usage | `any` | `{}` | no | +| [admin\_principals](#input\_admin\_principals) | Principals allowed to peform admin actions (default: current account) | `list(string)` | `null` | no | +| [advanced\_options](#input\_advanced\_options) | Key-value string pairs to specify advanced configuration options. Note that the values for these configuration options must be strings (wrapped in quotes) or they may be wrong and cause a perpetual diff, causing Terraform to want to recreate your Elasticsearch domain on every apply | `map(string)` | `{}` | no | +| [advanced\_security\_options](#input\_advanced\_security\_options) | Configuration block for [fine-grained access control](https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/fgac.html) | `any` |
{| no | +| [application\_name](#input\_application\_name) | Unique name for the opensearch instance | `string` | `""` | no | +| [auto\_tune\_options](#input\_auto\_tune\_options) | Configuration block for the Auto-Tune options of the domain | `any` |
"anonymous_auth_enabled": false,
"enabled": true
}
{| no | +| [cloudwatch\_log\_group\_kms\_key\_id](#input\_cloudwatch\_log\_group\_kms\_key\_id) | If a KMS Key ARN is set, this key will be used to encrypt the corresponding log group. Please be sure that the KMS Key has an appropriate key policy (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/encrypt-log-data-kms.html) | `string` | `null` | no | +| [cloudwatch\_log\_group\_retention\_in\_days](#input\_cloudwatch\_log\_group\_retention\_in\_days) | Number of days to retain log events | `number` | `60` | no | +| [cloudwatch\_log\_group\_skip\_destroy](#input\_cloudwatch\_log\_group\_skip\_destroy) | Set to true if you do not wish the log group (and any logs it may contain) to be deleted at destroy time, and instead just remove the log group from the Terraform state | `bool` | `null` | no | +| [cloudwatch\_log\_resource\_policy\_name](#input\_cloudwatch\_log\_resource\_policy\_name) | Name of the resource policy for OpenSearch to log to CloudWatch | `string` | `null` | no | +| [cluster\_config](#input\_cluster\_config) | Configuration block for the cluster of the domain | `any` |
"desired_state": "DISABLED"
}
{| no | +| [cognito\_options](#input\_cognito\_options) | Configuration block for authenticating Kibana with Cognito | `any` | `{}` | no | +| [create](#input\_create) | Determines whether resources will be created (affects all resources) | `bool` | `true` | no | +| [create\_access\_policy](#input\_create\_access\_policy) | Determines whether an access policy will be created | `bool` | `true` | no | +| [create\_cloudwatch\_log\_groups](#input\_create\_cloudwatch\_log\_groups) | Determines whether log groups are created | `bool` | `true` | no | +| [create\_cloudwatch\_log\_resource\_policy](#input\_create\_cloudwatch\_log\_resource\_policy) | Determines whether a resource policy will be created for OpenSearch to log to CloudWatch | `bool` | `true` | no | +| [create\_saml\_options](#input\_create\_saml\_options) | Determines whether SAML options will be created | `bool` | `false` | no | +| [create\_security\_group](#input\_create\_security\_group) | Determines if a security group is created | `bool` | `true` | no | +| [domain\_endpoint\_options](#input\_domain\_endpoint\_options) | Configuration block for domain endpoint HTTP(S) related options | `any` |
"dedicated_master_enabled": true
}
{| no | +| [domain\_name](#input\_domain\_name) | Name of the domain | `string` | `""` | no | +| [ebs\_options](#input\_ebs\_options) | Configuration block for EBS related options, may be required based on chosen [instance size](https://aws.amazon.com/elasticsearch-service/pricing/) | `any` |
"enforce_https": true,
"tls_security_policy": "Policy-Min-TLS-1-2-2019-07"
}
{| no | +| [enable\_access\_policy](#input\_enable\_access\_policy) | Determines whether an access policy will be applied to the domain | `bool` | `true` | no | +| [encrypt\_at\_rest](#input\_encrypt\_at\_rest) | Configuration block for encrypting at rest | `any` |
"ebs_enabled": true,
"volume_size": 64,
"volume_type": "gp3"
}
{| no | +| [engine\_version](#input\_engine\_version) | Version of the OpenSearch engine to use | `string` | `null` | no | +| [log\_publishing\_options](#input\_log\_publishing\_options) | Configuration block for publishing slow and application logs to CloudWatch Logs. This block can be declared multiple times, for each log\_type, within the same resource | `any` |
"enabled": true
}
[| no | +| [node\_to\_node\_encryption](#input\_node\_to\_node\_encryption) | Configuration block for node-to-node encryption options | `any` |
{
"log_type": "INDEX_SLOW_LOGS"
},
{
"log_type": "SEARCH_SLOW_LOGS"
}
]
{| no | +| [outbound\_connections](#input\_outbound\_connections) | Map of AWS OpenSearch outbound connections to create | `any` | `{}` | no | +| [read\_principals](#input\_read\_principals) | Principals allowed to read the secret (default: current account) | `list(string)` | `null` | no | +| [saml\_options](#input\_saml\_options) | SAML authentication options for an AWS OpenSearch Domain | `any` | `{}` | no | +| [security\_group\_description](#input\_security\_group\_description) | Description of the security group created | `string` | `null` | no | +| [security\_group\_name](#input\_security\_group\_name) | Name to use on security group created | `string` | `null` | no | +| [security\_group\_rules](#input\_security\_group\_rules) | Security group ingress and egress rules to add to the security group created | `any` | `{}` | no | +| [security\_group\_tags](#input\_security\_group\_tags) | A map of additional tags to add to the security group created | `map(string)` | `{}` | no | +| [security\_group\_use\_name\_prefix](#input\_security\_group\_use\_name\_prefix) | Determines whether the security group name (`security_group_name`) is used as a prefix | `bool` | `true` | no | +| [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no | +| [timeouts](#input\_timeouts) | Create and delete timeout configurations for the domain | `map(string)` | `{}` | no | +| [vpc\_options](#input\_vpc\_options) | Configuration block for VPC related options. Adding or removing this configuration forces a new resource ([documentation](https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-vpc.html#es-vpc-limitations)) | `any` | `{}` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| [cloudwatch\_logs](#output\_cloudwatch\_logs) | Map of CloudWatch log groups created and their attributes | +| [domain\_arn](#output\_domain\_arn) | The Amazon Resource Name (ARN) of the domain | +| [domain\_dashboard\_endpoint](#output\_domain\_dashboard\_endpoint) | Domain-specific endpoint for Dashboard without https scheme | +| [domain\_endpoint](#output\_domain\_endpoint) | Domain-specific endpoint used to submit index, search, and data upload requests | +| [domain\_id](#output\_domain\_id) | The unique identifier for the domain | +| [environment\_variables](#output\_environment\_variables) | Environment variables set by this rotation function | +| [kms\_key\_arn](#output\_kms\_key\_arn) | ID of the KMS key used to encrypt the secret | +| [outbound\_connections](#output\_outbound\_connections) | Map of outbound connections created and their attributes | +| [policy\_json](#output\_policy\_json) | Required IAM policies | +| [secret\_arn](#output\_secret\_arn) | ARN of the secrets manager secret containing credentials | +| [secret\_name](#output\_secret\_name) | Name of the secrets manager secret containing credentials | +| [security\_group\_arn](#output\_security\_group\_arn) | Amazon Resource Name (ARN) of the security group | +| [security\_group\_id](#output\_security\_group\_id) | ID of the security group | + \ No newline at end of file diff --git a/opensearch/main.tf b/opensearch/main.tf new file mode 100644 index 0000000..e812f87 --- /dev/null +++ b/opensearch/main.tf @@ -0,0 +1,460 @@ +# Based on: https://github.com/terraform-community-modules/tf_aws_elasticsearch + +data "aws_region" "current" { + count = var.create ? 1 : 0 +} +data "aws_partition" "current" { + count = var.create ? 1 : 0 +} +data "aws_caller_identity" "current" { + count = var.create ? 1 : 0 +} +data "aws_iam_session_context" "current" { + count = var.create ? 1 : 0 + arn = data.aws_caller_identity.current[0].arn +} + +locals { + account_id = try(data.aws_caller_identity.current[0].account_id, "") + 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) + create_cloudwatch_log_groups = var.create && var.create_cloudwatch_log_groups + create_security_group = var.create && var.create_security_group && length(var.vpc_options) > 0 + + partition = try(data.aws_partition.current[0].partition, "") + region = try(data.aws_region.current[0].name, "") + + security_group_name = try(coalesce(var.security_group_name, var.domain_name), "") + + static_domain_arn = "arn:${local.partition}:es:${local.region}:${local.account_id}:domain/${var.domain_name}" + + tags = merge(var.tags, { terraform-aws-modules = "opensearch" }) +} + +################################################################################ +# Domain +################################################################################ + +resource "aws_opensearch_domain" "this" { + count = var.create ? 1 : 0 + + # Controlled via aws_opensearch_domain_policy below + # access_policies = var.access_policies + advanced_options = var.advanced_options + + dynamic "advanced_security_options" { + for_each = length(var.advanced_security_options) > 0 ? [var.advanced_security_options] : [] + + content { + anonymous_auth_enabled = try(advanced_security_options.value.anonymous_auth_enabled, false) + enabled = try(advanced_security_options.value.enabled, true) + internal_user_database_enabled = try(advanced_security_options.value.internal_user_database_enabled, null) + + dynamic "master_user_options" { + for_each = try([advanced_security_options.value.master_user_options], [{}]) + + content { + master_user_arn = try(master_user_options.value.master_user_arn, null) != null ? try(master_user_options.value.master_user_arn, data.aws_iam_session_context.current[0].issuer_arn) : null + master_user_name = try(master_user_options.value.master_user_arn, null) == null ? try(master_user_options.value.master_user_name, null) : null + master_user_password = try(master_user_options.value.master_user_arn, null) == null ? try(master_user_options.value.master_user_password, null) : null + } + } + } + } + + dynamic "auto_tune_options" { + for_each = length(var.auto_tune_options) > 0 ? [var.auto_tune_options] : [] + + content { + desired_state = try(auto_tune_options.value.desired_state, "DISABLED") + + dynamic "maintenance_schedule" { + for_each = try(auto_tune_options.value.maintenance_schedule, []) + + content { + cron_expression_for_recurrence = maintenance_schedule.value.cron_expression_for_recurrence + + dynamic "duration" { + for_each = [maintenance_schedule.value.duration] + + content { + unit = duration.value.unit + value = duration.value.value + } + } + + start_at = maintenance_schedule.value.start_at + } + } + + rollback_on_disable = try(auto_tune_options.value.rollback_on_disable, null) + } + } + + dynamic "cluster_config" { + for_each = length(var.cluster_config) > 0 ? [var.cluster_config] : [] + + content { + dynamic "cold_storage_options" { + for_each = try([cluster_config.value.cold_storage_options], []) + + content { + enabled = try(cold_storage_options.value.enabled, null) + } + } + + dedicated_master_count = try(cluster_config.value.dedicated_master_count, 3) + dedicated_master_enabled = try(cluster_config.value.dedicated_master_enabled, true) + dedicated_master_type = try(cluster_config.value.dedicated_master_type, "c6g.large.search") + instance_count = try(cluster_config.value.instance_count, 3) + instance_type = try(cluster_config.value.instance_type, "r6g.large.search") + warm_count = try(cluster_config.value.warm_count, null) + warm_enabled = try(cluster_config.value.warm_enabled, null) + warm_type = try(cluster_config.value.warm_type, null) + + dynamic "zone_awareness_config" { + for_each = try([cluster_config.value.zone_awareness_config], []) + + content { + availability_zone_count = try(zone_awareness_config.value.availability_zone_count, null) + } + } + + zone_awareness_enabled = try(cluster_config.value.zone_awareness_enabled, true) + } + } + + dynamic "cognito_options" { + for_each = length(var.cognito_options) > 0 ? [var.cognito_options] : [] + + content { + enabled = try(cognito_options.value.enabled, null) + identity_pool_id = cognito_options.value.identity_pool_id + role_arn = cognito_options.value.role_arn + user_pool_id = cognito_options.value.user_pool_id + } + } + + dynamic "domain_endpoint_options" { + for_each = length(var.domain_endpoint_options) > 0 ? [var.domain_endpoint_options] : [] + + content { + custom_endpoint = try(domain_endpoint_options.value.custom_endpoint, null) + custom_endpoint_certificate_arn = try(domain_endpoint_options.value.custom_endpoint_certificate_arn, null) + custom_endpoint_enabled = try(domain_endpoint_options.value.custom_endpoint_enabled, null) + enforce_https = try(domain_endpoint_options.value.enforce_https, true) + tls_security_policy = try(domain_endpoint_options.value.tls_security_policy, "Policy-Min-TLS-1-2-2019-07") + } + } + + domain_name = var.domain_name + + dynamic "ebs_options" { + for_each = length(var.ebs_options) > 0 ? [var.ebs_options] : [] + + content { + ebs_enabled = try(ebs_options.value.ebs_enabled, true) + iops = try(ebs_options.value.iops, null) + throughput = try(ebs_options.value.throughput, null) + volume_size = try(ebs_options.value.volume_size, null) + volume_type = try(ebs_options.value.volume_type, "gp3") + } + } + + dynamic "encrypt_at_rest" { + for_each = length(var.encrypt_at_rest) > 0 ? [var.encrypt_at_rest] : [] + + content { + enabled = try(encrypt_at_rest.value.enabled, true) + kms_key_id = try(encrypt_at_rest.value.kms_key_id, null) + } + } + + engine_version = var.engine_version + + dynamic "log_publishing_options" { + for_each = { for opt in var.log_publishing_options : opt.log_type => opt } + + content { + cloudwatch_log_group_arn = try(log_publishing_options.value.cloudwatch_log_group_arn, aws_cloudwatch_log_group.this[log_publishing_options.key].arn) + enabled = try(log_publishing_options.value.enabled, true) + log_type = log_publishing_options.value.log_type + } + } + + dynamic "node_to_node_encryption" { + for_each = length(var.node_to_node_encryption) > 0 ? [var.node_to_node_encryption] : [] + + content { + enabled = try(node_to_node_encryption.value.enabled, true) + } + } + + dynamic "vpc_options" { + for_each = length(var.vpc_options) > 0 ? [var.vpc_options] : [] + + content { + security_group_ids = concat(try(vpc_options.value.security_group_ids, []), aws_security_group.this[*].id) + subnet_ids = try(vpc_options.value.subnet_ids, null) + } + } + + timeouts { + create = try(var.timeouts.create, null) + delete = try(var.timeouts.delete, null) + } + + tags = local.tags +} + +################################################################################ +# Access Policy +################################################################################ + +resource "aws_opensearch_domain_policy" "this" { + count = var.create && var.enable_access_policy && (local.create_access_policy || var.access_policies != null) ? 1 : 0 + + domain_name = aws_opensearch_domain.this[0].domain_name + access_policies = local.create_access_policy ? data.aws_iam_policy_document.this[0].json : var.access_policies +} + +data "aws_iam_policy_document" "this" { + count = local.create_access_policy ? 1 : 0 + + source_policy_documents = var.access_policy_source_policy_documents + override_policy_documents = var.access_policy_override_policy_documents + + dynamic "statement" { + for_each = var.access_policy_statements + + content { + sid = try(statement.value.sid, null) + actions = try(statement.value.actions, null) + not_actions = try(statement.value.not_actions, null) + effect = try(statement.value.effect, null) + resources = try(statement.value.resources, + [for path in try(statement.value.resource_paths, ["*"]) : "${aws_opensearch_domain.this[0].arn}/${path}"] + ) + not_resources = try(statement.value.not_resources, null) + + dynamic "principals" { + for_each = try(statement.value.principals, []) + + content { + type = principals.value.type + identifiers = principals.value.identifiers + } + } + + dynamic "not_principals" { + for_each = try(statement.value.not_principals, []) + + content { + type = not_principals.value.type + identifiers = not_principals.value.identifiers + } + } + + dynamic "condition" { + for_each = try(statement.value.conditions, []) + + content { + test = condition.value.test + values = condition.value.values + variable = condition.value.variable + } + } + } + } +} + +################################################################################ +# SAML Options +################################################################################ + +resource "aws_opensearch_domain_saml_options" "this" { + count = var.create && var.create_saml_options ? 1 : 0 + + domain_name = aws_opensearch_domain.this[0].domain_name + + dynamic "saml_options" { + for_each = length(var.saml_options) > 0 ? [var.saml_options] : [] + + content { + enabled = try(saml_options.value.enabled, null) + + dynamic "idp" { + for_each = try([saml_options.value.idp], []) + + content { + entity_id = idp.value.entity_id + metadata_content = idp.value.metadata_content + } + } + + master_backend_role = try(saml_options.value.master_backend_role, null) + master_user_name = try(saml_options.value.master_user_name, null) + roles_key = try(saml_options.value.roles_key, null) + session_timeout_minutes = try(saml_options.value.session_timeout_minutes, null) + subject_key = try(saml_options.value.subject_key, null) + } + } +} + +################################################################################ +# Outbound Connections +################################################################################ + +resource "aws_opensearch_outbound_connection" "this" { + for_each = { for k, v in var.outbound_connections : k => v if var.create } + + connection_alias = try(each.value.connection_alias, each.key) + + local_domain_info { + owner_id = try(each.value.local_domain_info.owner_id, local.account_id) + region = try(each.value.local_domain_info.region, local.region) + domain_name = try(each.value.local_domain_info.domain_name, aws_opensearch_domain.this[0].domain_name) + } + + remote_domain_info { + owner_id = each.value.remote_domain_info.owner_id + region = each.value.remote_domain_info.region + domain_name = each.value.remote_domain_info.domain_name + } +} + +################################################################################ +# Cloudwatch Log Group +################################################################################ + +resource "aws_cloudwatch_log_group" "this" { + for_each = { for opt in var.log_publishing_options : opt.log_type => opt if try(opt.enabled, true) && local.create_cloudwatch_log_groups } + + name = try(each.value.log_group_name, "/aws/opensearch/${var.domain_name}/${each.key}") + retention_in_days = try(each.value.log_group_retention_in_days, var.cloudwatch_log_group_retention_in_days) + kms_key_id = try(each.value.log_group_kms_key_id, var.cloudwatch_log_group_kms_key_id) + skip_destroy = try(each.value.log_group_skip_destroy, var.cloudwatch_log_group_skip_destroy) + + tags = merge(local.tags, try(each.value.log_group_tags, {})) +} + +data "aws_iam_policy_document" "cloudwatch" { + count = local.create_cloudwatch_log_groups && var.create_cloudwatch_log_resource_policy ? 1 : 0 + + statement { + actions = [ + "logs:CreateLogStream", + "logs:PutLogEvents", + "logs:PutLogEventsBatch", + ] + + # https://github.com/hashicorp/terraform-provider-aws/issues/14497 + # resources = coalescelist([for log in aws_cloudwatch_log_group.this : "${log.arn}:*"], ["arn:${local.partition}:logs:*"]) + resources = ["arn:${local.partition}:logs:*"] + + principals { + identifiers = ["es.amazonaws.com"] + type = "Service" + } + + condition { + test = "StringEquals" + variable = "aws:SourceAccount" + values = [local.account_id] + } + + condition { + test = "ArnLike" + variable = "aws:SourceArn" + values = [local.static_domain_arn] + } + } +} + +resource "aws_cloudwatch_log_resource_policy" "this" { + count = local.create_cloudwatch_log_groups && var.create_cloudwatch_log_resource_policy ? 1 : 0 + + policy_document = data.aws_iam_policy_document.cloudwatch[0].json + policy_name = coalesce(var.cloudwatch_log_resource_policy_name, "opensearch-${var.domain_name}") +} + +################################################################################ +# Security Group +################################################################################ + +data "aws_subnet" "this" { + count = local.create_security_group ? 1 : 0 + + id = element(var.vpc_options.subnet_ids, 0) +} + +resource "aws_security_group" "this" { + count = local.create_security_group ? 1 : 0 + + name = var.security_group_use_name_prefix ? null : local.security_group_name + name_prefix = var.security_group_use_name_prefix ? "${local.security_group_name}-" : null + description = var.security_group_description + vpc_id = data.aws_subnet.this[0].vpc_id + revoke_rules_on_delete = true + + tags = merge(local.tags, var.security_group_tags) + + lifecycle { + create_before_destroy = true + } +} + +resource "aws_vpc_security_group_ingress_rule" "this" { + for_each = { for k, v in var.security_group_rules : k => v if local.create_security_group && try(v.type, "ingress") == "ingress" } + + # Required + security_group_id = aws_security_group.this[0].id + ip_protocol = try(each.value.ip_protocol, "tcp") + + # Optional + cidr_ipv4 = lookup(each.value, "cidr_ipv4", null) + cidr_ipv6 = lookup(each.value, "cidr_ipv6", null) + description = try(each.value.description, null) + from_port = try(each.value.from_port, 443) + prefix_list_id = lookup(each.value, "prefix_list_id", null) + referenced_security_group_id = lookup(each.value, "referenced_security_group_id", null) + to_port = try(each.value.to_port, 443) + + tags = merge(local.tags, var.security_group_tags, try(each.value.tags, {})) +} + +resource "aws_vpc_security_group_egress_rule" "this" { + for_each = { for k, v in var.security_group_rules : k => v if local.create_security_group && try(v.type, "ingress") == "egress" } + + # Required + security_group_id = aws_security_group.this[0].id + ip_protocol = try(each.value.ip_protocol, "tcp") + + # Optional + cidr_ipv4 = lookup(each.value, "cidr_ipv4", null) + cidr_ipv6 = lookup(each.value, "cidr_ipv6", null) + description = try(each.value.description, null) + from_port = try(each.value.from_port, null) + prefix_list_id = lookup(each.value, "prefix_list_id", null) + referenced_security_group_id = lookup(each.value, "referenced_security_group_id", null) + to_port = try(each.value.to_port, null) + + tags = merge(local.tags, var.security_group_tags, try(each.value.tags, {})) +} + +module "elasticsearch_secret" { + source = "github.com/thoughtbot/terraform-aws-secrets//secret?ref=v0.8.0" + + admin_principals = var.admin_principals + description = "Elastisearch secrets for: ${var.application_name}" + name = "${var.application_name}-secret" + read_principals = var.read_principals + resource_tags = var.tags + + initial_value = jsonencode({ + AWS_SEARCH_ENDPOINT = try(aws_opensearch_domain.this[0].endpoint, null) + AWS_SEARCH_DASHBOARD_ENDPOINT = try(aws_opensearch_domain.this[0].dashboard_endpoint, null) + AWS_SEARCH_DOMAIN_ID = try(aws_opensearch_domain.this[0].domain_id, null) + AWS_SEARCH_PASSWORD = var.advanced_security_options.master_user_options.master_user_password + AWS_SEARCH_USER_NAME = var.advanced_security_options.master_user_options.master_user_name + }) +} diff --git a/opensearch/outputs.tf b/opensearch/outputs.tf new file mode 100644 index 0000000..0adf0ee --- /dev/null +++ b/opensearch/outputs.tf @@ -0,0 +1,84 @@ +################################################################################ +# Domain +################################################################################ + +output "domain_arn" { + description = "The Amazon Resource Name (ARN) of the domain" + value = try(aws_opensearch_domain.this[0].arn, null) +} + +output "domain_id" { + description = "The unique identifier for the domain" + value = try(aws_opensearch_domain.this[0].domain_id, null) +} + +output "domain_endpoint" { + description = "Domain-specific endpoint used to submit index, search, and data upload requests" + value = try(aws_opensearch_domain.this[0].endpoint, null) +} + +output "domain_dashboard_endpoint" { + description = "Domain-specific endpoint for Dashboard without https scheme" + value = try(aws_opensearch_domain.this[0].dashboard_endpoint, null) +} + +################################################################################ +# Outbound Connections +################################################################################ + +output "outbound_connections" { + description = "Map of outbound connections created and their attributes" + value = aws_opensearch_outbound_connection.this +} + +################################################################################ +# CloudWatch Log Groups +################################################################################ + +output "cloudwatch_logs" { + description = "Map of CloudWatch log groups created and their attributes" + value = aws_cloudwatch_log_group.this +} + +################################################################################ +# Security Group +################################################################################ + +output "security_group_arn" { + description = "Amazon Resource Name (ARN) of the security group" + value = try(aws_security_group.this[0].arn, null) +} + +output "security_group_id" { + description = "ID of the security group" + value = try(aws_security_group.this[0].id, null) +} + +################################################################################ +# Secret details +################################################################################ + +output "environment_variables" { + description = "Environment variables set by this rotation function" + value = ["AWS_SEARCH_ENDPOINT", "AWS_SEARCH_DASHBOARD_ENDPOINT", "AWS_SEARCH_DOMAIN_ID", "AWS_SEARCH_PASSWORD", "AWS_SEARCH_USER_NAME"] +} + +output "secret_name" { + description = "Name of the secrets manager secret containing credentials" + value = module.elasticsearch_secret.name +} + +output "policy_json" { + description = "Required IAM policies" + value = module.elasticsearch_secret.policy_json +} + +output "kms_key_arn" { + description = "ID of the KMS key used to encrypt the secret" + value = module.elasticsearch_secret.kms_key_arn +} + +output "secret_arn" { + description = "ARN of the secrets manager secret containing credentials" + value = module.elasticsearch_secret.arn +} diff --git a/opensearch/variables.tf b/opensearch/variables.tf new file mode 100644 index 0000000..6d46084 --- /dev/null +++ b/opensearch/variables.tf @@ -0,0 +1,284 @@ +variable "application_name" { + type = string + description = "Unique name for the opensearch instance" + default = "" +} + +variable "create" { + description = "Determines whether resources will be created (affects all resources)" + type = bool + default = true +} + +variable "tags" { + description = "A map of tags to add to all resources" + type = map(string) + default = {} +} + +################################################################################ +# Domain +################################################################################ + +variable "advanced_options" { + description = "Key-value string pairs to specify advanced configuration options. Note that the values for these configuration options must be strings (wrapped in quotes) or they may be wrong and cause a perpetual diff, causing Terraform to want to recreate your Elasticsearch domain on every apply" + type = map(string) + default = {} +} + +variable "advanced_security_options" { + description = "Configuration block for [fine-grained access control](https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/fgac.html)" + type = any + default = { + enabled = true + anonymous_auth_enabled = false + } +} + +variable "auto_tune_options" { + description = "Configuration block for the Auto-Tune options of the domain" + type = any + default = { + desired_state = "DISABLED" + } +} + +variable "cluster_config" { + description = "Configuration block for the cluster of the domain" + type = any + default = { + dedicated_master_enabled = true + } +} + +variable "cognito_options" { + description = "Configuration block for authenticating Kibana with Cognito" + type = any + default = {} +} + +variable "domain_endpoint_options" { + description = "Configuration block for domain endpoint HTTP(S) related options" + type = any + default = { + enforce_https = true + tls_security_policy = "Policy-Min-TLS-1-2-2019-07" + } +} + +variable "domain_name" { + description = "Name of the domain" + type = string + default = "" +} + +variable "ebs_options" { + description = "Configuration block for EBS related options, may be required based on chosen [instance size](https://aws.amazon.com/elasticsearch-service/pricing/)" + type = any + default = { + ebs_enabled = true + volume_size = 64 + volume_type = "gp3" + } +} + +variable "encrypt_at_rest" { + description = "Configuration block for encrypting at rest" + type = any + default = { + enabled = true + } +} + +variable "engine_version" { + description = "Version of the OpenSearch engine to use" + type = string + default = null +} + +variable "log_publishing_options" { + description = "Configuration block for publishing slow and application logs to CloudWatch Logs. This block can be declared multiple times, for each log_type, within the same resource" + type = any + default = [ + { log_type = "INDEX_SLOW_LOGS" }, + { log_type = "SEARCH_SLOW_LOGS" }, + ] +} + +variable "node_to_node_encryption" { + description = "Configuration block for node-to-node encryption options" + type = any + default = { + enabled = true + } +} + +variable "vpc_options" { + description = "Configuration block for VPC related options. Adding or removing this configuration forces a new resource ([documentation](https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-vpc.html#es-vpc-limitations))" + type = any + default = {} +} + +variable "timeouts" { + description = "Create and delete timeout configurations for the domain" + type = map(string) + default = {} +} + +################################################################################ +# Access Policy +################################################################################ + +variable "enable_access_policy" { + description = "Determines whether an access policy will be applied to the domain" + type = bool + default = true +} + +variable "create_access_policy" { + description = "Determines whether an access policy will be created" + type = bool + default = true +} + +variable "access_policies" { + description = "IAM policy document specifying the access policies for the domain. Required if `create_access_policy` is `false`" + type = string + default = null +} + +variable "access_policy_statements" { + description = "A map of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) for custom permission usage" + type = any + default = {} +} + +variable "access_policy_source_policy_documents" { + description = "List of IAM policy documents that are merged together into the exported document. Statements must have unique `sid`s" + type = list(string) + default = [] +} + +variable "access_policy_override_policy_documents" { + description = "List of IAM policy documents that are merged together into the exported document. In merging, statements with non-blank `sid`s will override statements with the same `sid`" + type = list(string) + default = [] +} + +################################################################################ +# SAML Options +################################################################################ + +variable "create_saml_options" { + description = "Determines whether SAML options will be created" + type = bool + default = false +} + +variable "saml_options" { + description = "SAML authentication options for an AWS OpenSearch Domain" + type = any + default = {} +} + +################################################################################ +# Outbound Connections +################################################################################ + +variable "outbound_connections" { + description = "Map of AWS OpenSearch outbound connections to create" + type = any + default = {} +} + +################################################################################ +# CloudWatch Log Group +################################################################################ + +variable "create_cloudwatch_log_groups" { + description = "Determines whether log groups are created" + type = bool + default = true +} + +variable "cloudwatch_log_group_retention_in_days" { + description = "Number of days to retain log events" + type = number + default = 60 +} + +variable "cloudwatch_log_group_kms_key_id" { + description = "If a KMS Key ARN is set, this key will be used to encrypt the corresponding log group. Please be sure that the KMS Key has an appropriate key policy (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/encrypt-log-data-kms.html)" + type = string + default = null +} + +variable "cloudwatch_log_group_skip_destroy" { + description = "Set to true if you do not wish the log group (and any logs it may contain) to be deleted at destroy time, and instead just remove the log group from the Terraform state" + type = bool + default = null +} + +variable "create_cloudwatch_log_resource_policy" { + description = "Determines whether a resource policy will be created for OpenSearch to log to CloudWatch" + type = bool + default = true +} + +variable "cloudwatch_log_resource_policy_name" { + description = "Name of the resource policy for OpenSearch to log to CloudWatch" + type = string + default = null +} + +################################################################################ +# Security Group +################################################################################ + +variable "create_security_group" { + description = "Determines if a security group is created" + type = bool + default = true +} + +variable "security_group_name" { + description = "Name to use on security group created" + type = string + default = null +} + +variable "security_group_use_name_prefix" { + description = "Determines whether the security group name (`security_group_name`) is used as a prefix" + type = bool + default = true +} + +variable "security_group_description" { + description = "Description of the security group created" + type = string + default = null +} + +variable "security_group_rules" { + description = "Security group ingress and egress rules to add to the security group created" + type = any + default = {} +} + +variable "security_group_tags" { + description = "A map of additional tags to add to the security group created" + type = map(string) + default = {} +} + +variable "admin_principals" { + description = "Principals allowed to peform admin actions (default: current account)" + type = list(string) + default = null +} + +variable "read_principals" { + description = "Principals allowed to read the secret (default: current account)" + type = list(string) + default = null +} diff --git a/opensearch/versions.tf b/opensearch/versions.tf new file mode 100644 index 0000000..bc31fc9 --- /dev/null +++ b/opensearch/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 1.6.2" + + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } +}
"enabled": true
}