Skip to content

Commit

Permalink
fix: Update dynamic lookup logic to support lazy evaluation (terrafor…
Browse files Browse the repository at this point in the history
  • Loading branch information
bryantbiggs authored Jun 23, 2022
1 parent c3e0b97 commit 8fb0daa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
2 changes: 0 additions & 2 deletions examples/complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
22 changes: 14 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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" {}
Expand All @@ -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"
}
}
Expand All @@ -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"
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 8fb0daa

Please sign in to comment.