From 519f041a63de31c83151ae9a234b9456a66b9928 Mon Sep 17 00:00:00 2001 From: Andrew Kuklewicz Date: Wed, 14 Aug 2024 19:14:41 -0400 Subject: [PATCH 1/5] Add optional sort key as part of primary key --- db/dynamodb-global-table.yml | 55 +++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/db/dynamodb-global-table.yml b/db/dynamodb-global-table.yml index c011983bd..654e8758c 100644 --- a/db/dynamodb-global-table.yml +++ b/db/dynamodb-global-table.yml @@ -2,7 +2,7 @@ AWSTemplateFormatVersion: "2010-09-09" Description: >- - Create a global primary-key-lookup DynamoDB table + Create a global primary-key-lookup DynamoDB table with an optional sort key Metadata: AWS::CloudFormation::Interface: @@ -11,8 +11,10 @@ Metadata: default: Table Definition Parameters: - TableName - - PrimaryKey - - PrimaryType + - PartitionKey + - PartitionType + - SortKey + - SortType - ExpirationField - Label: default: Optional Regions @@ -46,10 +48,14 @@ Metadata: ParameterLabels: TableName: default: Table name - PrimaryKey: - default: Primary key field - PrimaryType: - default: Primary key type + PartitionKey: + default: Partition key field + PartitionType: + default: Partition key type + SortKey: + default: Sort key field + SortType: + default: Sort key type ExpirationField: default: Field to enable expiration on Region2: @@ -91,12 +97,22 @@ Parameters: TableName: Type: String Description: The unique DynamoDB table name. - PrimaryKey: + PartitionKey: Type: String - Description: The name of the primary key field - PrimaryType: + Description: The name of the partition key field + PartitionType: Type: String - Description: The type of the primary key field + Description: The type of the partition key field + AllowedValues: + - String + - Numeric + - Binary + SortKey: + Type: String + Description: The name of the sort key field (optional) + SortType: + Type: String + Description: The type of the sort key field AllowedValues: - String - Numeric @@ -179,6 +195,7 @@ Conditions: HasForeignAccount: !Not [!Equals [!Ref ForeignAccountId, ""]] HasForeignRole: !Not [!Equals [!Ref ForeignRoleName, ""]] HasProvisionedBilling: !Equals [!Ref BillingMode, "PROVISIONED"] + HasSortKey: !Not [!Equals [!Ref SortKey, ""]] Mappings: DataTypesMap: @@ -194,12 +211,22 @@ Resources: Type: AWS::DynamoDB::GlobalTable Properties: AttributeDefinitions: - - AttributeName: !Ref PrimaryKey - AttributeType: !FindInMap [DataTypesMap, !Ref PrimaryType, Symbol] + - AttributeName: !Ref PartitionKey + AttributeType: !FindInMap [DataTypesMap, !Ref PartitionType, Symbol] + - !If + - HasSortKey + - AttributeName: !Ref SortKey + AttributeType: !FindInMap [DataTypesMap, !Ref SortType, Symbol] + - !Ref AWS::NoValue BillingMode: !Ref BillingMode KeySchema: - - AttributeName: !Ref PrimaryKey + - AttributeName: !Ref PartitionKey KeyType: HASH + - !If + - HasSortKey + - AttributeName: !Ref SortKey + KeyType: RANGE + - !Ref AWS::NoValue Replicas: - ReadProvisionedThroughputSettings: !If - HasProvisionedBilling From 0ee1a6bdf249516011063598446b79b9e798c881 Mon Sep 17 00:00:00 2001 From: Andrew Kuklewicz Date: Wed, 28 Aug 2024 14:01:27 -0400 Subject: [PATCH 2/5] Preserve parameter name --- db/dynamodb-global-table.yml | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/db/dynamodb-global-table.yml b/db/dynamodb-global-table.yml index 654e8758c..13342bf6d 100644 --- a/db/dynamodb-global-table.yml +++ b/db/dynamodb-global-table.yml @@ -11,8 +11,8 @@ Metadata: default: Table Definition Parameters: - TableName - - PartitionKey - - PartitionType + - PrimaryKey + - PrimaryType - SortKey - SortType - ExpirationField @@ -48,10 +48,10 @@ Metadata: ParameterLabels: TableName: default: Table name - PartitionKey: - default: Partition key field - PartitionType: - default: Partition key type + PrimaryKey: + default: Primary key field + PrimaryType: + default: Primary key type SortKey: default: Sort key field SortType: @@ -97,12 +97,12 @@ Parameters: TableName: Type: String Description: The unique DynamoDB table name. - PartitionKey: + PrimaryKey: Type: String - Description: The name of the partition key field - PartitionType: + Description: The name of the primary key field + PrimaryType: Type: String - Description: The type of the partition key field + Description: The type of the primary key field AllowedValues: - String - Numeric @@ -211,8 +211,8 @@ Resources: Type: AWS::DynamoDB::GlobalTable Properties: AttributeDefinitions: - - AttributeName: !Ref PartitionKey - AttributeType: !FindInMap [DataTypesMap, !Ref PartitionType, Symbol] + - AttributeName: !Ref PrimaryKey + AttributeType: !FindInMap [DataTypesMap, !Ref PrimaryType, Symbol] - !If - HasSortKey - AttributeName: !Ref SortKey @@ -220,7 +220,7 @@ Resources: - !Ref AWS::NoValue BillingMode: !Ref BillingMode KeySchema: - - AttributeName: !Ref PartitionKey + - AttributeName: !Ref PrimaryKey KeyType: HASH - !If - HasSortKey From 158d2cc25d7ac299d61977f14684b2158331248b Mon Sep 17 00:00:00 2001 From: Andrew Kuklewicz Date: Wed, 28 Aug 2024 14:06:01 -0400 Subject: [PATCH 3/5] Fix orphaned !If statement --- db/dynamodb-global-table.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/dynamodb-global-table.yml b/db/dynamodb-global-table.yml index 13342bf6d..afb825df3 100644 --- a/db/dynamodb-global-table.yml +++ b/db/dynamodb-global-table.yml @@ -213,7 +213,7 @@ Resources: AttributeDefinitions: - AttributeName: !Ref PrimaryKey AttributeType: !FindInMap [DataTypesMap, !Ref PrimaryType, Symbol] - - !If + - Fn::If: - HasSortKey - AttributeName: !Ref SortKey AttributeType: !FindInMap [DataTypesMap, !Ref SortType, Symbol] @@ -222,7 +222,7 @@ Resources: KeySchema: - AttributeName: !Ref PrimaryKey KeyType: HASH - - !If + - Fn::If: - HasSortKey - AttributeName: !Ref SortKey KeyType: RANGE From 005796af6eecda25c7c3f53d09428619dc57f3ca Mon Sep 17 00:00:00 2001 From: Andrew Kuklewicz Date: Thu, 29 Aug 2024 11:44:56 -0400 Subject: [PATCH 4/5] Fix indent, make prettier happier --- db/dynamodb-global-table.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/db/dynamodb-global-table.yml b/db/dynamodb-global-table.yml index afb825df3..c41162b61 100644 --- a/db/dynamodb-global-table.yml +++ b/db/dynamodb-global-table.yml @@ -214,19 +214,19 @@ Resources: - AttributeName: !Ref PrimaryKey AttributeType: !FindInMap [DataTypesMap, !Ref PrimaryType, Symbol] - Fn::If: - - HasSortKey - - AttributeName: !Ref SortKey - AttributeType: !FindInMap [DataTypesMap, !Ref SortType, Symbol] - - !Ref AWS::NoValue + - HasSortKey + - AttributeName: !Ref SortKey + AttributeType: !FindInMap [DataTypesMap, !Ref SortType, Symbol] + - !Ref AWS::NoValue BillingMode: !Ref BillingMode KeySchema: - AttributeName: !Ref PrimaryKey KeyType: HASH - Fn::If: - - HasSortKey - - AttributeName: !Ref SortKey - KeyType: RANGE - - !Ref AWS::NoValue + - HasSortKey + - AttributeName: !Ref SortKey + KeyType: RANGE + - !Ref AWS::NoValue Replicas: - ReadProvisionedThroughputSettings: !If - HasProvisionedBilling From 20adffc9d3668e8f53e1946f769bc0eab4e6e62a Mon Sep 17 00:00:00 2001 From: Andrew Kuklewicz Date: Thu, 29 Aug 2024 14:59:28 -0400 Subject: [PATCH 5/5] Label primary as partition key --- db/dynamodb-global-table.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/dynamodb-global-table.yml b/db/dynamodb-global-table.yml index c41162b61..0542e8302 100644 --- a/db/dynamodb-global-table.yml +++ b/db/dynamodb-global-table.yml @@ -49,9 +49,9 @@ Metadata: TableName: default: Table name PrimaryKey: - default: Primary key field + default: Partition key field PrimaryType: - default: Primary key type + default: Partition key type SortKey: default: Sort key field SortType: