From 799b541135d0fb9cea31ddf29a8dacc1a94cb0fc Mon Sep 17 00:00:00 2001 From: Matsuda Date: Thu, 17 Oct 2024 08:12:55 +0900 Subject: [PATCH] feat(dynamodb): enable contributor insights for global secondary index (#30560) ### Issue # (if applicable) Closes #15671 . ### Reason for this change To enable contributor insights for a specific global secondary index. This feature is supported in [CFn](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-dynamodb-table-globalsecondaryindex.html#cfn-dynamodb-contributorinsightsspecification-enabled). ### Description of changes Add `contributorInsightsEnabled` property to the `GlobalSecondaryIndexProps` in `Table`. In [AWS::DynamoDB::GlobalTable GlobalSecondaryIndex](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-dynamodb-globaltable-globalsecondaryindex.html), `ContributorInsightsSpecification` does not exist. So I didn't change `TableV2`. ### Description of how you validated changes Add unit test and integ tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- ...b-contributor-insights-for-gsi.assets.json | 19 ++ ...contributor-insights-for-gsi.template.json | 105 ++++++++++ ...efaultTestDeployAssert9D8EC318.assets.json | 19 ++ ...aultTestDeployAssert9D8EC318.template.json | 36 ++++ .../cdk.out | 1 + .../integ.json | 12 ++ .../manifest.json | 113 +++++++++++ .../tree.json | 192 ++++++++++++++++++ ...g.dynamodb.contirubtor-insights-for-gsi.ts | 39 ++++ packages/aws-cdk-lib/aws-dynamodb/README.md | 19 +- .../aws-cdk-lib/aws-dynamodb/lib/table.ts | 8 + .../aws-dynamodb/test/dynamodb.test.ts | 47 ++++- 12 files changed, 605 insertions(+), 5 deletions(-) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/aws-cdk-dynamodb-contributor-insights-for-gsi.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/aws-cdk-dynamodb-contributor-insights-for-gsi.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/aws-cdk-dynamodb-contributor-insights-for-gsi.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/aws-cdk-dynamodb-contributor-insights-for-gsi.assets.json new file mode 100644 index 0000000000000..e62b42408dc2f --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/aws-cdk-dynamodb-contributor-insights-for-gsi.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.24", + "files": { + "f9b6221d41c7d1dec71841a066339cb8de39d9cc4b599c8bd8af7b741044e64c": { + "source": { + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "f9b6221d41c7d1dec71841a066339cb8de39d9cc4b599c8bd8af7b741044e64c.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/aws-cdk-dynamodb-contributor-insights-for-gsi.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/aws-cdk-dynamodb-contributor-insights-for-gsi.template.json new file mode 100644 index 0000000000000..567ea9b452573 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/aws-cdk-dynamodb-contributor-insights-for-gsi.template.json @@ -0,0 +1,105 @@ +{ + "Resources": { + "TableWithGlobalSecondaryIndexCC8E841E": { + "Type": "AWS::DynamoDB::Table", + "Properties": { + "AttributeDefinitions": [ + { + "AttributeName": "hashKey", + "AttributeType": "S" + }, + { + "AttributeName": "gsiHashKey", + "AttributeType": "S" + } + ], + "GlobalSecondaryIndexes": [ + { + "ContributorInsightsSpecification": { + "Enabled": true + }, + "IndexName": "GSI-ContributorInsightsEnabled", + "KeySchema": [ + { + "AttributeName": "gsiHashKey", + "KeyType": "HASH" + } + ], + "Projection": { + "ProjectionType": "ALL" + }, + "ProvisionedThroughput": { + "ReadCapacityUnits": 5, + "WriteCapacityUnits": 5 + } + }, + { + "ContributorInsightsSpecification": { + "Enabled": false + }, + "IndexName": "GSI-ContributorInsightsDisabled", + "KeySchema": [ + { + "AttributeName": "gsiHashKey", + "KeyType": "HASH" + } + ], + "Projection": { + "ProjectionType": "ALL" + }, + "ProvisionedThroughput": { + "ReadCapacityUnits": 5, + "WriteCapacityUnits": 5 + } + } + ], + "KeySchema": [ + { + "AttributeName": "hashKey", + "KeyType": "HASH" + } + ], + "ProvisionedThroughput": { + "ReadCapacityUnits": 5, + "WriteCapacityUnits": 5 + } + }, + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Delete" + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.assets.json new file mode 100644 index 0000000000000..165991a4ee766 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.24", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/cdk.out new file mode 100644 index 0000000000000..4efaa16f29af9 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"36.0.24"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/integ.json new file mode 100644 index 0000000000000..e57a4eaa2d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "36.0.24", + "testCases": { + "aws-cdk-dynamodb-contributor-insights-for-gsi-test/DefaultTest": { + "stacks": [ + "aws-cdk-dynamodb-contributor-insights-for-gsi" + ], + "assertionStack": "aws-cdk-dynamodb-contributor-insights-for-gsi-test/DefaultTest/DeployAssert", + "assertionStackName": "awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/manifest.json new file mode 100644 index 0000000000000..221a00e60b0cc --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/manifest.json @@ -0,0 +1,113 @@ +{ + "version": "36.0.24", + "artifacts": { + "aws-cdk-dynamodb-contributor-insights-for-gsi.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "aws-cdk-dynamodb-contributor-insights-for-gsi.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "aws-cdk-dynamodb-contributor-insights-for-gsi": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "aws-cdk-dynamodb-contributor-insights-for-gsi.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/f9b6221d41c7d1dec71841a066339cb8de39d9cc4b599c8bd8af7b741044e64c.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "aws-cdk-dynamodb-contributor-insights-for-gsi.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "aws-cdk-dynamodb-contributor-insights-for-gsi.assets" + ], + "metadata": { + "/aws-cdk-dynamodb-contributor-insights-for-gsi/TableWithGlobalSecondaryIndex/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "TableWithGlobalSecondaryIndexCC8E841E" + } + ], + "/aws-cdk-dynamodb-contributor-insights-for-gsi/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/aws-cdk-dynamodb-contributor-insights-for-gsi/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "aws-cdk-dynamodb-contributor-insights-for-gsi" + }, + "awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "awscdkdynamodbcontributorinsightsforgsitestDefaultTestDeployAssert9D8EC318.assets" + ], + "metadata": { + "/aws-cdk-dynamodb-contributor-insights-for-gsi-test/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/aws-cdk-dynamodb-contributor-insights-for-gsi-test/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "aws-cdk-dynamodb-contributor-insights-for-gsi-test/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/tree.json new file mode 100644 index 0000000000000..2f7c91f42507f --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.js.snapshot/tree.json @@ -0,0 +1,192 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "aws-cdk-dynamodb-contributor-insights-for-gsi": { + "id": "aws-cdk-dynamodb-contributor-insights-for-gsi", + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi", + "children": { + "TableWithGlobalSecondaryIndex": { + "id": "TableWithGlobalSecondaryIndex", + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi/TableWithGlobalSecondaryIndex", + "children": { + "Resource": { + "id": "Resource", + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi/TableWithGlobalSecondaryIndex/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::DynamoDB::Table", + "aws:cdk:cloudformation:props": { + "attributeDefinitions": [ + { + "attributeName": "hashKey", + "attributeType": "S" + }, + { + "attributeName": "gsiHashKey", + "attributeType": "S" + } + ], + "globalSecondaryIndexes": [ + { + "contributorInsightsSpecification": { + "enabled": true + }, + "indexName": "GSI-ContributorInsightsEnabled", + "keySchema": [ + { + "attributeName": "gsiHashKey", + "keyType": "HASH" + } + ], + "projection": { + "projectionType": "ALL" + }, + "provisionedThroughput": { + "readCapacityUnits": 5, + "writeCapacityUnits": 5 + } + }, + { + "contributorInsightsSpecification": { + "enabled": false + }, + "indexName": "GSI-ContributorInsightsDisabled", + "keySchema": [ + { + "attributeName": "gsiHashKey", + "keyType": "HASH" + } + ], + "projection": { + "projectionType": "ALL" + }, + "provisionedThroughput": { + "readCapacityUnits": 5, + "writeCapacityUnits": 5 + } + } + ], + "keySchema": [ + { + "attributeName": "hashKey", + "keyType": "HASH" + } + ], + "provisionedThroughput": { + "readCapacityUnits": 5, + "writeCapacityUnits": 5 + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_dynamodb.CfnTable", + "version": "0.0.0" + } + }, + "ScalingRole": { + "id": "ScalingRole", + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi/TableWithGlobalSecondaryIndex/ScalingRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_dynamodb.Table", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "aws-cdk-dynamodb-contributor-insights-for-gsi-test": { + "id": "aws-cdk-dynamodb-contributor-insights-for-gsi-test", + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi-test", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi-test/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi-test/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi-test/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi-test/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "aws-cdk-dynamodb-contributor-insights-for-gsi-test/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.ts new file mode 100644 index 0000000000000..7e1968ba12fc4 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.contirubtor-insights-for-gsi.ts @@ -0,0 +1,39 @@ +import { App, RemovalPolicy, Stack } from 'aws-cdk-lib'; +import { Attribute, AttributeType, Table } from 'aws-cdk-lib/aws-dynamodb'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; + +// CDK parameters +const STACK_NAME = 'aws-cdk-dynamodb-contributor-insights-for-gsi'; + +// DynamoDB table parameters +const TABLE = 'TableWithGlobalSecondaryIndex'; +const TABLE_PARTITION_KEY: Attribute = { name: 'hashKey', type: AttributeType.STRING }; + +// DynamoDB global secondary index parameters +const GSI_TEST_CASE_1 = 'GSI-ContributorInsightsEnabled'; +const GSI_TEST_CASE_2 = 'GSI-ContributorInsightsDisabled'; +const GSI_PARTITION_KEY: Attribute = { name: 'gsiHashKey', type: AttributeType.STRING }; + +const app = new App(); + +const stack = new Stack(app, STACK_NAME); + +const table = new Table(stack, TABLE, { + partitionKey: TABLE_PARTITION_KEY, + removalPolicy: RemovalPolicy.DESTROY, +}); + +table.addGlobalSecondaryIndex({ + contributorInsightsEnabled: true, + indexName: GSI_TEST_CASE_1, + partitionKey: GSI_PARTITION_KEY, +}); +table.addGlobalSecondaryIndex({ + contributorInsightsEnabled: false, + indexName: GSI_TEST_CASE_2, + partitionKey: GSI_PARTITION_KEY, +}); + +new IntegTest(app, 'aws-cdk-dynamodb-contributor-insights-for-gsi-test', { + testCases: [stack], +}); diff --git a/packages/aws-cdk-lib/aws-dynamodb/README.md b/packages/aws-cdk-lib/aws-dynamodb/README.md index 24e09c418b6ae..41244dec60ea9 100644 --- a/packages/aws-cdk-lib/aws-dynamodb/README.md +++ b/packages/aws-cdk-lib/aws-dynamodb/README.md @@ -136,7 +136,7 @@ class BarStack extends cdk.Stack { const app = new cdk.App(); const fooStack = new FooStack(app, 'FooStack', { env: { region: 'us-west-2' } }); -const barStack = new BarStack(app, 'BarStack', { +const barStack = new BarStack(app, 'BarStack', { replicaTable: fooStack.globalTable.replica('us-east-1'), env: { region: 'us-east-1' }, }); @@ -527,6 +527,21 @@ const table = new dynamodb.TableV2(this, 'Table', { }); ``` +When you use `Table`, you can enable contributor insights for a table or specific global secondary index by setting `contributorInsightsEnabled` to `true`. + +```ts +const table = new dynamodb.Table(this, 'Table', { + partitionKey: { name: 'pk', type: dynamodb.AttributeType.STRING }, + contributorInsightsEnabled: true, // for a table +}); + +table.addGlobalSecondaryIndex({ + contributorInsightsEnabled: true, // for a specific global secondary index + indexName: 'gsi', + partitionKey: { name: 'pk', type: dynamodb.AttributeType.STRING }, +}); +``` + Further reading: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/contributorinsights_HowItWorks.html @@ -615,7 +630,7 @@ https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.Tabl ## Tags -You can add tags to a `TableV2` in several ways. By adding the tags to the construct itself it will apply the tags to the +You can add tags to a `TableV2` in several ways. By adding the tags to the construct itself it will apply the tags to the primary table. ```ts const table = new dynamodb.TableV2(this, 'Table', { diff --git a/packages/aws-cdk-lib/aws-dynamodb/lib/table.ts b/packages/aws-cdk-lib/aws-dynamodb/lib/table.ts index 8a37d443cb97f..03d2681382544 100644 --- a/packages/aws-cdk-lib/aws-dynamodb/lib/table.ts +++ b/packages/aws-cdk-lib/aws-dynamodb/lib/table.ts @@ -456,6 +456,13 @@ export interface GlobalSecondaryIndexProps extends SecondaryIndexProps, SchemaOp * @default - on-demand throughput is disabled */ readonly maxWriteRequestUnits?: number; + + /** + * Whether CloudWatch contributor insights is enabled for the specified global secondary index. + * + * @default false + */ + readonly contributorInsightsEnabled?: boolean; } /** @@ -1227,6 +1234,7 @@ export class Table extends TableBase { const gsiProjection = this.buildIndexProjection(props); this.globalSecondaryIndexes.push({ + contributorInsightsSpecification: props.contributorInsightsEnabled !== undefined ? { enabled: props.contributorInsightsEnabled } : undefined, indexName: props.indexName, keySchema: gsiKeySchema, projection: gsiProjection, diff --git a/packages/aws-cdk-lib/aws-dynamodb/test/dynamodb.test.ts b/packages/aws-cdk-lib/aws-dynamodb/test/dynamodb.test.ts index 4be16524b2ca3..420d8ce7a8290 100644 --- a/packages/aws-cdk-lib/aws-dynamodb/test/dynamodb.test.ts +++ b/packages/aws-cdk-lib/aws-dynamodb/test/dynamodb.test.ts @@ -52,7 +52,7 @@ const GSI_NAME = 'MyGSI'; const GSI_PARTITION_KEY: Attribute = { name: 'gsiHashKey', type: AttributeType.STRING }; const GSI_SORT_KEY: Attribute = { name: 'gsiSortKey', type: AttributeType.BINARY }; const GSI_NON_KEY = 'gsiNonKey'; -function * GSI_GENERATOR(): Generator { +function* GSI_GENERATOR(): Generator { let n = 0; while (true) { const globalSecondaryIndexProps: GlobalSecondaryIndexProps = { @@ -63,7 +63,7 @@ function * GSI_GENERATOR(): Generator { n++; } } -function * NON_KEY_ATTRIBUTE_GENERATOR(nonKeyPrefix: string): Generator { +function* NON_KEY_ATTRIBUTE_GENERATOR(nonKeyPrefix: string): Generator { let n = 0; while (true) { yield `${nonKeyPrefix}${n}`; @@ -75,7 +75,7 @@ function * NON_KEY_ATTRIBUTE_GENERATOR(nonKeyPrefix: string): Generator { +function* LSI_GENERATOR(): Generator { let n = 0; while (true) { const localSecondaryIndexProps: LocalSecondaryIndexProps = { @@ -1323,6 +1323,47 @@ test('when adding a global secondary index without specifying read and write cap ); }); +test.each([true, false])('when adding a global secondary index with contributoreIngishtsEnabled %s', (contributorInsightsEnabled: boolean) => { + const stack = new Stack(); + const table = new Table(stack, CONSTRUCT_NAME, { + partitionKey: TABLE_PARTITION_KEY, + sortKey: TABLE_SORT_KEY, + }); + + table.addGlobalSecondaryIndex({ + contributorInsightsEnabled, + indexName: GSI_NAME, + partitionKey: GSI_PARTITION_KEY, + }); + + Template.fromStack(stack).hasResourceProperties('AWS::DynamoDB::Table', + { + AttributeDefinitions: [ + { AttributeName: 'hashKey', AttributeType: 'S' }, + { AttributeName: 'sortKey', AttributeType: 'N' }, + { AttributeName: 'gsiHashKey', AttributeType: 'S' }, + ], + KeySchema: [ + { AttributeName: 'hashKey', KeyType: 'HASH' }, + { AttributeName: 'sortKey', KeyType: 'RANGE' }, + ], + ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }, + GlobalSecondaryIndexes: [ + { + ContributorInsightsSpecification: { + Enabled: contributorInsightsEnabled, + }, + IndexName: 'MyGSI', + KeySchema: [ + { AttributeName: 'gsiHashKey', KeyType: 'HASH' }, + ], + Projection: { ProjectionType: 'ALL' }, + }, + ], + }, + ); +}); + test('when adding a local secondary index with hash + range key', () => { const stack = new Stack(); const table = new Table(stack, CONSTRUCT_NAME, { partitionKey: TABLE_PARTITION_KEY, sortKey: TABLE_SORT_KEY });