From 8fb0daa718b2b346d14c314f0865b8f26bedebe0 Mon Sep 17 00:00:00 2001 From: Bryant Biggs Date: Thu, 23 Jun 2022 13:41:00 -0400 Subject: [PATCH] fix: Update dynamic lookup logic to support lazy evaluation (#19) --- .pre-commit-config.yaml | 4 ++-- examples/complete/README.md | 2 -- main.tf | 22 ++++++++++++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a0925b0..4ab192b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/antonbabenko/pre-commit-terraform - rev: v1.68.1 + rev: v1.72.2 hooks: - id: terraform_fmt - id: terraform_validate @@ -23,7 +23,7 @@ repos: - '--args=--only=terraform_standard_module_structure' - '--args=--only=terraform_workspace_remote' - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.2.0 + rev: v4.3.0 hooks: - id: check-merge-conflict - id: end-of-file-fixer diff --git a/examples/complete/README.md b/examples/complete/README.md index 5691eed..13ee272 100644 --- a/examples/complete/README.md +++ b/examples/complete/README.md @@ -17,8 +17,6 @@ To run this example you need to execute: ```bash $ terraform init $ terraform plan -# due to https://github.com/hashicorp/terraform/issues/4149 -$ terraform apply --target module.msk_cluster $ terraform apply ``` diff --git a/main.tf b/main.tf index e09cdee..e97e099 100644 --- a/main.tf +++ b/main.tf @@ -1,7 +1,8 @@ locals { subnet_group_id = var.create && var.create_repl_subnet_group ? aws_dms_replication_subnet_group.this[0].id : var.repl_instance_subnet_group_id - partition = data.aws_partition.current.partition + partition = data.aws_partition.current.partition + dns_suffix = data.aws_partition.current.dns_suffix } data "aws_partition" "current" {} @@ -19,7 +20,7 @@ data "aws_iam_policy_document" "dms_assume_role" { actions = ["sts:AssumeRole"] principals { - identifiers = ["dms.amazonaws.com"] + identifiers = ["dms.${local.dns_suffix}"] type = "Service" } } @@ -34,7 +35,7 @@ data "aws_iam_policy_document" "dms_assume_role_redshift" { actions = ["sts:AssumeRole"] principals { - identifiers = ["redshift.amazonaws.com"] + identifiers = ["redshift.${local.dns_suffix}"] type = "Service" } } @@ -176,7 +177,8 @@ resource "aws_dms_endpoint" "this" { # https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Target.Elasticsearch.html dynamic "elasticsearch_settings" { - for_each = try([each.value.elasticsearch_settings], []) + for_each = length(lookup(each.value, "elasticsearch_settings", {})) == 0 ? [] : [each.value.elasticsearch_settings] + content { endpoint_uri = elasticsearch_settings.value.endpoint_uri error_retry_duration = lookup(elasticsearch_settings.value, "error_retry_duration", null) @@ -187,7 +189,8 @@ resource "aws_dms_endpoint" "this" { # https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Target.Kafka.html dynamic "kafka_settings" { - for_each = try([each.value.kafka_settings], []) + for_each = length(lookup(each.value, "kafka_settings", {})) == 0 ? [] : [each.value.kafka_settings] + content { broker = kafka_settings.value.broker include_control_details = lookup(kafka_settings.value, "include_control_details", null) @@ -212,7 +215,8 @@ resource "aws_dms_endpoint" "this" { # https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Target.Kinesis.html dynamic "kinesis_settings" { - for_each = try([each.value.kinesis_settings], []) + for_each = length(lookup(each.value, "kinesis_settings", {})) == 0 ? [] : [each.value.kinesis_settings] + content { include_control_details = lookup(kinesis_settings.value, "include_control_details", null) include_null_and_empty = lookup(kinesis_settings.value, "include_null_and_empty", null) @@ -228,7 +232,8 @@ resource "aws_dms_endpoint" "this" { # https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Source.MongoDB.html dynamic "mongodb_settings" { - for_each = can(each.value["mongodb_settings"]) ? [each.value.mongodb_settings] : [] + for_each = length(lookup(each.value, "mongodb_settings", {})) == 0 ? [] : [each.value.mongodb_settings] + content { auth_mechanism = lookup(mongodb_settings.value, "auth_mechanism", null) auth_source = lookup(mongodb_settings.value, "auth_source", null) @@ -242,7 +247,8 @@ resource "aws_dms_endpoint" "this" { # https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Source.S3.html # https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Target.S3.html dynamic "s3_settings" { - for_each = can(each.value["s3_settings"]) ? [each.value.s3_settings] : [] + for_each = length(lookup(each.value, "s3_settings", {})) == 0 ? [] : [each.value.s3_settings] + content { add_column_name = lookup(s3_settings.value, "add_column_name", null) bucket_folder = lookup(s3_settings.value, "bucket_folder", null)