如何使用 Terraform 在 AWS DMS 上为 table 映射加入多个 JSON 文件?
How do I join multiple JSON files for table mapping on AWS DMS using Terraform?
为了创建DMS的复制任务,我使用了这个资源:
resource "aws_dms_replication_task" "test" {
migration_type = "full-load"
replication_instance_arn = aws_dms_replication_instance.test-dms-replication-instance-tf.replication_instance_arn
replication_task_id = "test-dms-replication-task-tf"
source_endpoint_arn = aws_dms_endpoint.test-dms-source-endpoint-tf.endpoint_arn
table_mappings = file("${path.module}/db1/test1.json")
tags = {
Name = "test"
}
target_endpoint_arn = aws_dms_endpoint.test-dms-target-endpoint-tf.endpoint_arn
}
schema生成的文件很多JSON,例如:
./db1/test1.json
{
"rules": [
{
"rule-type": "transformation",
"rule-id": "1",
"rule-name": "Rule name 1",
"rule-action": "rename",
"rule-target": "schema",
"object-locator": {
"schema-name": "SCHEMA_1",
"table-name": "TABLE_1"
},
"value": "main"
}
]
}
./db1/test2.json
{
"rules": [
{
"rule-type": "transformation",
"rule-id": "2",
"rule-name": "Rule name 2",
"rule-action": "rename",
"rule-target": "schema",
"object-locator": {
"schema-name": "SCHEMA_2",
"table-name": "TABLE_2"
},
"value": "main"
}
]
}
最后,我想得到一个完整的 rules
JSON 输出像
{
"rules": [
{
"rule-type": "transformation",
"rule-id": "1",
"rule-name": "Rule name 1",
"rule-action": "rename",
"rule-target": "schema",
"object-locator": {
"schema-name": "SCHEMA_1",
"table-name": "TABLE_1"
},
"value": "main"
},
{
"rule-type": "transformation",
"rule-id": "2",
"rule-name": "Rule name 2",
"rule-action": "rename",
"rule-target": "schema",
"object-locator": {
"schema-name": "SCHEMA_2",
"table-name": "TABLE_2"
},
"value": "main"
}
]
}
看来我需要合并子元素
如何在不使用 Terraform 手动将它们放入 1 个 JSON 文件的情况下将这些文件连接在一起?
可能有几种方法可以做到这一点。一个是这样的:
locals {
rule_files = ["${path.module}/test1.json", "${path.module}/test2.json"]
rules_joined = {
rules = flatten([for fname in local.rule_files: jsondecode(file(fname))["rules"]])
}
}
给出 local.rules_joined
为:
{
"rules" = [
{
"object-locator" = {
"schema-name" = "SCHEMA_1"
"table-name" = "TABLE_1"
}
"rule-action" = "rename"
"rule-id" = "1"
"rule-name" = "Rule name 1"
"rule-target" = "schema"
"rule-type" = "transformation"
"value" = "main"
},
{
"object-locator" = {
"schema-name" = "SCHEMA_2"
"table-name" = "TABLE_2"
}
"rule-action" = "rename"
"rule-id" = "2"
"rule-name" = "Rule name 2"
"rule-target" = "schema"
"rule-type" = "transformation"
"value" = "main"
},
]
}
要将其变成字符串,请使用jsonencode(local.rules_joined)
。
为了创建DMS的复制任务,我使用了这个资源:
resource "aws_dms_replication_task" "test" {
migration_type = "full-load"
replication_instance_arn = aws_dms_replication_instance.test-dms-replication-instance-tf.replication_instance_arn
replication_task_id = "test-dms-replication-task-tf"
source_endpoint_arn = aws_dms_endpoint.test-dms-source-endpoint-tf.endpoint_arn
table_mappings = file("${path.module}/db1/test1.json")
tags = {
Name = "test"
}
target_endpoint_arn = aws_dms_endpoint.test-dms-target-endpoint-tf.endpoint_arn
}
schema生成的文件很多JSON,例如:
./db1/test1.json
{
"rules": [
{
"rule-type": "transformation",
"rule-id": "1",
"rule-name": "Rule name 1",
"rule-action": "rename",
"rule-target": "schema",
"object-locator": {
"schema-name": "SCHEMA_1",
"table-name": "TABLE_1"
},
"value": "main"
}
]
}
./db1/test2.json
{
"rules": [
{
"rule-type": "transformation",
"rule-id": "2",
"rule-name": "Rule name 2",
"rule-action": "rename",
"rule-target": "schema",
"object-locator": {
"schema-name": "SCHEMA_2",
"table-name": "TABLE_2"
},
"value": "main"
}
]
}
最后,我想得到一个完整的 rules
JSON 输出像
{
"rules": [
{
"rule-type": "transformation",
"rule-id": "1",
"rule-name": "Rule name 1",
"rule-action": "rename",
"rule-target": "schema",
"object-locator": {
"schema-name": "SCHEMA_1",
"table-name": "TABLE_1"
},
"value": "main"
},
{
"rule-type": "transformation",
"rule-id": "2",
"rule-name": "Rule name 2",
"rule-action": "rename",
"rule-target": "schema",
"object-locator": {
"schema-name": "SCHEMA_2",
"table-name": "TABLE_2"
},
"value": "main"
}
]
}
看来我需要合并子元素
如何在不使用 Terraform 手动将它们放入 1 个 JSON 文件的情况下将这些文件连接在一起?
可能有几种方法可以做到这一点。一个是这样的:
locals {
rule_files = ["${path.module}/test1.json", "${path.module}/test2.json"]
rules_joined = {
rules = flatten([for fname in local.rule_files: jsondecode(file(fname))["rules"]])
}
}
给出 local.rules_joined
为:
{
"rules" = [
{
"object-locator" = {
"schema-name" = "SCHEMA_1"
"table-name" = "TABLE_1"
}
"rule-action" = "rename"
"rule-id" = "1"
"rule-name" = "Rule name 1"
"rule-target" = "schema"
"rule-type" = "transformation"
"value" = "main"
},
{
"object-locator" = {
"schema-name" = "SCHEMA_2"
"table-name" = "TABLE_2"
}
"rule-action" = "rename"
"rule-id" = "2"
"rule-name" = "Rule name 2"
"rule-target" = "schema"
"rule-type" = "transformation"
"value" = "main"
},
]
}
要将其变成字符串,请使用jsonencode(local.rules_joined)
。