Terraform-provider-aws: Add DynamoDB Global Table Support

Created on 30 Nov 2017  ·  8Comments  ·  Source: hashicorp/terraform-provider-aws

AWS has announced support for managing globally replicating DynamoDB tables: https://aws.amazon.com/about-aws/whats-new/2017/11/aws-launches-amazon-dynamodb-global-tables/

Prerequisite: aws-sdk-go v1.12.36 (#2474)

Terraform Version

terraform 0.10+
terraform-provider-aws 1.5.0

Affected Resource(s)

  • aws_dynamodb_table
  • Potentially new: aws_dynamodb_global_table

Expected Behavior

Create, update, delete, and import DynamoDB global tables. Potentially:

provider "aws" {
  alias  = "us-east-1"
  region = "us-east-1"
}

provider "aws" {
  alias  = "us-west-2"
  region = "us-west-2"
}

resource "aws_dynamodb_table" "us-east-1" {
  provider = "aws.us-east-1"

  hash_key         = "myAttribute"
  name             = "myTable"
  stream_enabled   = true
  stream_view_type = "NEW_AND_OLD_IMAGES"
  read_capacity    = 1
  write_capacity   = 1

  attribute {
    name = "myAttribute"
    type = "S"
  }
}

resource "aws_dynamodb_table" "us-west-2" {
  provider = "aws.us-west-2"

  hash_key         = "myAttribute"
  name             = "myTable"
  stream_enabled   = true
  stream_view_type = "NEW_AND_OLD_IMAGES"
  read_capacity    = 1
  write_capacity   = 1

  attribute {
    name = "myAttribute"
    type = "S"
  }
}

# New resource
resource "aws_dynamodb_global_table" "" {
  provider = "aws.us-east-1"

  name = "myTable"

  replication_group {
    regions = [
      "us-east-1",
      "us-west-2",
    ]
  }
}

Important Factoids

From the docs:

The following is a conceptual overview of how a global table is created.

  1. Create an ordinary DynamoDB table, with DynamoDB Streams enabled, in an AWS region.
  2. Repeat step 1 for every other AWS region where you want to replicate your data.
  3. Define a DynamoDB global table, based upon the tables that you have created.

There are a lot of restrictions on how these are to be setup at the moment. From the docs:

If you want to add a new replica table to a global table, each of the following conditions must be true:

  • The table must have the same primary key as all of the other replicas.
  • The table must have the same name as all of the other replicas.
  • The table must have DynamoDB Streams enabled, with the stream containing both the new and the old images of the item.
  • None of the replica tables in the global table can contain any data.

I presume we'll want to follow the AWS API here and layer the global table management on top of our existing aws_dynamodb_table resource rather than trying to reinvent the wheel with managing all the replica tables consistently in a single resource.

References

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/globaltables_HowItWorks.html

new-resource servicdynamodb

Most helpful comment

I'm going to start on an implementation. More soon. 🚀

All 8 comments

I'm going to start on an implementation. More soon. 🚀

PR submitted for new resource: #2517

Hey @bflad awesome PR! Thanks! I'm not sure how long it'll take to get accepted upstream and to be added to a release, I just wanted to confirm that I can fork terraform, apply your pr and this should then just work?

Any example on usage would be great, do the old app autoscaling resources for dynamodb need to be created as well? Any examples? Thanks!

We are also eagerly anticipating this PR to be merged so we can use and test it.

Hi, everyone! This has landed in master and will ship with the v1.8.0 release of the provider, expected tomorrow. Happy Terraform'ing! 🎉

This has been released in terraform-provider-aws version 1.8.0. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

Hello,

I have tried using the same solution it gives me below error
Error: Error asking for user input: Error parsing address 'aws_dynamodb_global_table': invalid resource address "aws_dynamodb_global_table"

Also, I tried to update my provider to provider.aws v1.8.0
Still same error can you please help me

Terraform v0.11.7

  • provider.aws v1.8.0

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings