Skip to content

Instantly share code, notes, and snippets.

@mooneygr
Created July 10, 2018 03:44
Show Gist options
  • Save mooneygr/baa8bfef56f445ff2c8105bde1000f62 to your computer and use it in GitHub Desktop.
Save mooneygr/baa8bfef56f445ff2c8105bde1000f62 to your computer and use it in GitHub Desktop.
Global Tables primary key change repro
/* call the inner module passing in region specific parameters where necessary */
module "table_rst1" {
source = "theTableSpec"
provisioned_read_capacity = "${var.provisioned_read_capacity}"
provisioned_write_capacity = "${var.provisioned_write_capacity}"
table_name = "${var.table_name}"
environment = "${var.environment}"
autoscaling_role_arn = "${var.autoscaling_role_arn}"
providers = {
aws = "aws.rst1"
}
}
module "table_sea1" {
source = "theTableSpec"
provisioned_read_capacity = "${var.provisioned_read_capacity}"
provisioned_write_capacity = "${var.provisioned_write_capacity}"
table_name = "${var.table_name}"
environment = "${var.environment}"
autoscaling_role_arn = "${var.autoscaling_role_arn}"
providers = {
aws = "aws.sea1"
}
}
module "table_dub1" {
source = "theTableSpec"
provisioned_read_capacity = "${var.provisioned_read_capacity}"
provisioned_write_capacity = "${var.provisioned_write_capacity}"
table_name = "${var.table_name}"
environment = "${var.environment}"
autoscaling_role_arn = "${var.autoscaling_role_arn}"
providers = {
aws = "aws.dub1"
}
}
module "table_fra1" {
source = "theTableSpec"
provisioned_read_capacity = "${var.provisioned_read_capacity}"
provisioned_write_capacity = "${var.provisioned_write_capacity}"
table_name = "${var.table_name}"
environment = "${var.environment}"
autoscaling_role_arn = "${var.autoscaling_role_arn}"
providers = {
aws = "aws.fra1"
}
}
# The resource that puts the existing tables together into a Global Table
resource "aws_dynamodb_global_table" "global_table" {
/* AWS Pre-requisites for a Global Table are:
* To have existing empty tables (and thus we have created them in above in this file)
* The table name of the existing tables must be the same
* The primary key of the entries in the table must be the same
* Table streams must be set to NEW_AND_OLD_IMAGES
*/
depends_on = ["module.table_sea1", "module.table_rst1", "module.table_dub1", "module.table_fra1"]
name = "${var.table_name}"
replica {
region_name = "us-west-2"
}
replica {
region_name = "us-east-1"
}
replica {
region_name = "eu-west-1"
}
replica {
region_name = "eu-central-1"
}
}%
output "table_name" {
value = "${var.table_name}"
}
output "sea1_table_arn" {
value = "${module.table_sea1.table_arn}"
}
output "rst1_table_arn" {
value = "${module.table_rst1.table_arn}"
}
output "dub1_table_arn" {
value = "${module.table_dub1.table_arn}"
}
output "fra1_table_arn" {
value = "${module.table_fra1.table_arn}"
}%
/* necessary for the module to consume providers passed in from outside */
provider "aws" {
alias = "rst1"
}
provider "aws" {
alias = "sea1"
}
provider "aws" {
alias = "dub1"
}
provider "aws" {
alias = "fra1"
}%
resource "aws_dynamodb_table" "the_table" {
name = "${var.table_name}"
read_capacity = "${var.provisioned_read_capacity}"
write_capacity = "${var.provisioned_write_capacity}"
hash_key = "newKey2"
stream_enabled = true
stream_view_type = "NEW_AND_OLD_IMAGES"
attribute {
name = "newKey2"
type = "S"
}
global_secondary_index {
hash_key = "index1"
name = "index1-index"
projection_type = "ALL"
read_capacity = "5"
write_capacity = "5"
}
global_secondary_index {
hash_key = "index2"
name = "index2-index"
projection_type = "ALL"
read_capacity = "5"
write_capacity = "5"
}
attribute {
name = "index1"
type = "S"
}
attribute {
name = "index2"
type = "S"
}
/* Cisco tagging requirements: https://cisco.jiveon.com/docs/DOC-1693254 */
tags {
Name = "${var.table_name}"
Environment = "${var.environment}"
}
lifecycle {
ignore_changes = ["read_capacity", "write_capacity"]
}
}
output "table_arn" {
value = "${aws_dynamodb_table.the_table.arn}"
}%
variable "provisioned_read_capacity" {
description = "The read capacity units the table should provision"
}
variable "provisioned_write_capacity" {
description = "The write capacity units the table should provision"
}
variable "table_name" {
description = "The name to use for the table"
}
variable "environment" {
description = "environment tag (prod, nonprod, sandbox)"
}
variable "autoscaling_role_arn" {
type = "string"
default = "" # If none provided then AWS provides an autoscaling role
description = "The ARN of the DynamoDB tables autoscaling role. If unsure leave blank."
}%
variable "provisioned_read_capacity" {
description = "The read capacity units the table should provision"
}
variable "provisioned_write_capacity" {
description = "The write capacity units the table should provision"
}
variable "table_name" {
description = "The name to use for the table"
}
variable "environment" {
description = "Environment tag (prod, nonprod, sandbox)"
}
variable "autoscaling_role_arn" {
type = "string"
default = "" # If none provided then AWS provides an autoscaling role
description = "The ARN of the DynamoDB tables autoscaling role. If unsure leave blank."
}%
module "a_table" {
source = "example-table"
provisioned_read_capacity = 5
provisioned_write_capacity = 5
providers = {
"aws.sea1" = "aws.sea1"
"aws.rst1" = "aws.rst1"
"aws.dub1" = "aws.dub1"
"aws.fra1" = "aws.fra1"
}
table_name = "ChangePartitionKeyTable"
environment = "${var.environment}"
}%
// without a default provider we get prompted so just leave it in
provider "aws" {
profile = "sc"
region = "us-west-2"
}
provider "aws" {
profile = "sc"
alias = "sea1"
region = "us-west-2"
}
provider "aws" {
profile = "sc"
alias = "rst1"
region = "us-east-1"
}
provider "aws" {
profile = "sc"
alias = "fra1"
region = "eu-central-1"
}
provider "aws" {
profile = "sc"
alias = "dub1"
region = "eu-west-1"
}
GunZipped TarBall of this structure available here: https://www.dropbox.com/s/ekvit8xhl4sedu8/repro.tar.gz?dl=0
Files in full below.
Tree:
.
├── example-table
│   ├── main.tf
│   ├── output.tf
│   ├── proxy_providers.tf
│   ├── theTableSpec
│   │   ├── main.tf
│   │   ├── output.tf
│   │   └── variables.tf
│   └── variables.tf
├── main.tf
├── provider.tf
├── terraform.tfstate
├── terraform.tfstate.backup
├── variables.tf
└── version.tf
2 directories, 13 files
// not important in repo, but here for completeness
variable "environment" {
default = "stage"
}
terraform {
required_version = "= 0.11.7"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment