0
votes

I am creating aws global table with terraform with multi-region replication.

    resource "aws_dynamodb_global_table" "test_table" {
  depends_on = ["aws_dynamodb_table.us-east-1", "aws_dynamodb_table.us-west-2"]
  provider   = "aws.us-east-1"

  name = "test"

  replica {
    region_name = "us-east-1"
  }

  replica {
    region_name = "us-west-2"
  }
}

Next I am creating both the regional tables as modules with count interpolation and returning ARN for each table.

How do I create Global Table as module where I add reference to both tables? depends_on and replica in this case.

Updated with tried approach:

//Templ1 
{ region_name = "${region_name}" }

//Templ2
  ${value} 

data "template_file" "replica_region" {
  template = "${file("${path.module}/replica-region.json.tmpl")}" 
  count = "${length(var.regions)}"
  vars {
    region_name = "${element(var.regions, count.index)}"
  }
}

data "template_file" "replica_regional" {
  template = "${file("${path.module}/replica-regional.json.tmpl")}" 
  vars {
    value = "${join(",", data.template_file.replica_region.*.rendered)}"
  }
}

I am passing below to global table

replica = [ "${data.template_file.replica_regional.rendered}" ] 

but getting error replica.0.region_name : required field is not set where as when I do output for "${data.template_file.replica_regional.rendered}" I get below output.

 { region_name = "us-east-1" },{ region_name = "us-west-2" }

And If I hardcode above terraform recipe works just fine. What Am i missing? I am referring to this article Link

1

1 Answers

1
votes

I ended up setting it as a variable as below in variables.tf and using it directly in global db resource.

    replica = [
        {
            region_name = "us-east-1"
        },
        {
            region_name = "us-west-1"
        }
    ]