Home > Enterprise >  How to set an escaped json for AWS DMS with Terraform?
How to set an escaped json for AWS DMS with Terraform?

Time:10-12

The Terraform's example is Resource: aws_dms_replication_task I created a test resource as

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           = jsonencode(file("${path.module}/test.json"))

  tags = {
    Name = "test"
  }

  target_endpoint_arn = aws_dms_endpoint.test-dms-target-endpoint-tf.endpoint_arn
}

The test.json file is

{
  "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"
    }
  ]
}

When run the terraform apply, got this error

Error: InvalidParameterValueException: Invalid Table Mappings document. Invalid json Invalid json object

If use the format as example maybe ok

table_mappings            = "{\"rules\":[{\"rule-type\":\"selection\",\"rule-id\":\"1\",\"rule-name\":\"1\",\"object-locator\":{\"schema-name\":\"%\",\"table-name\":\"%\"},\"rule-action\":\"include\"}]}"

How to realize the same result if use Terraform's function?

CodePudding user response:

To get your escaped json string, you can do:

table_mappings = jsonencode(jsondecode(file("${path.module}/test.json")))

This effectively removes \n from your test.json and produces single-line json escaped string.

Example:

"{\"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\"}]}"
  • Related