package
1.204.0-devpreview
Repository: https://github.com/aws/aws-cdk-go.git
Documentation: pkg.go.dev

# README

Amazon DynamoDB Construct Library

Here is a minimal deployable DynamoDB table definition:

table := dynamodb.NewTable(this, jsii.String("Table"), &TableProps{
	PartitionKey: &Attribute{
		Name: jsii.String("id"),
		Type: dynamodb.AttributeType_STRING,
	},
})

Importing existing tables

To import an existing table into your CDK application, use the Table.fromTableName, Table.fromTableArn or Table.fromTableAttributes factory method. This method accepts table name or table ARN which describes the properties of an already existing table:

var user user

table := dynamodb.Table_FromTableArn(this, jsii.String("ImportedTable"), jsii.String("arn:aws:dynamodb:us-east-1:111111111:table/my-table"))
// now you can just call methods on the table
table.GrantReadWriteData(user)

If you intend to use the tableStreamArn (including indirectly, for example by creating an @aws-cdk/aws-lambda-event-source.DynamoEventSource on the imported table), you must use the Table.fromTableAttributes method and the tableStreamArn property must be populated.

Keys

When a table is defined, you must define it's schema using the partitionKey (required) and sortKey (optional) properties.

Billing Mode

DynamoDB supports two billing modes:

  • PROVISIONED - the default mode where the table and global secondary indexes have configured read and write capacity.
  • PAY_PER_REQUEST - on-demand pricing and scaling. You only pay for what you use and there is no read and write capacity for the table or its global secondary indexes.
table := dynamodb.NewTable(this, jsii.String("Table"), &TableProps{
	PartitionKey: &Attribute{
		Name: jsii.String("id"),
		Type: dynamodb.AttributeType_STRING,
	},
	BillingMode: dynamodb.BillingMode_PAY_PER_REQUEST,
})

Further reading: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadWriteCapacityMode.

Table Class

DynamoDB supports two table classes:

  • STANDARD - the default mode, and is recommended for the vast majority of workloads.
  • STANDARD_INFREQUENT_ACCESS - optimized for tables where storage is the dominant cost.
table := dynamodb.NewTable(this, jsii.String("Table"), &TableProps{
	PartitionKey: &Attribute{
		Name: jsii.String("id"),
		Type: dynamodb.AttributeType_STRING,
	},
	TableClass: dynamodb.TableClass_STANDARD_INFREQUENT_ACCESS,
})

Further reading: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.TableClasses.html

Configure AutoScaling for your table

You can have DynamoDB automatically raise and lower the read and write capacities of your table by setting up autoscaling. You can use this to either keep your tables at a desired utilization level, or by scaling up and down at pre-configured times of the day:

Auto-scaling is only relevant for tables with the billing mode, PROVISIONED.

readScaling := table.AutoScaleReadCapacity(&EnableScalingProps{
	MinCapacity: jsii.Number(1),
	MaxCapacity: jsii.Number(50),
})

readScaling.ScaleOnUtilization(&UtilizationScalingProps{
	TargetUtilizationPercent: jsii.Number(50),
})

readScaling.ScaleOnSchedule(jsii.String("ScaleUpInTheMorning"), &ScalingSchedule{
	Schedule: appscaling.Schedule_Cron(&CronOptions{
		Hour: jsii.String("8"),
		Minute: jsii.String("0"),
	}),
	MinCapacity: jsii.Number(20),
})

readScaling.ScaleOnSchedule(jsii.String("ScaleDownAtNight"), &ScalingSchedule{
	Schedule: appscaling.Schedule_*Cron(&CronOptions{
		Hour: jsii.String("20"),
		Minute: jsii.String("0"),
	}),
	MaxCapacity: jsii.Number(20),
})

Further reading: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/AutoScaling.html https://aws.amazon.com/blogs/database/how-to-use-aws-cloudformation-to-configure-auto-scaling-for-amazon-dynamodb-tables-and-indexes/

Amazon DynamoDB Global Tables

You can create DynamoDB Global Tables by setting the replicationRegions property on a Table:

globalTable := dynamodb.NewTable(this, jsii.String("Table"), &TableProps{
	PartitionKey: &Attribute{
		Name: jsii.String("id"),
		Type: dynamodb.AttributeType_STRING,
	},
	ReplicationRegions: []*string{
		jsii.String("us-east-1"),
		jsii.String("us-east-2"),
		jsii.String("us-west-2"),
	},
})

When doing so, a CloudFormation Custom Resource will be added to the stack in order to create the replica tables in the selected regions.

The default billing mode for Global Tables is PAY_PER_REQUEST. If you want to use PROVISIONED, you have to make sure write auto-scaling is enabled for that Table:

globalTable := dynamodb.NewTable(this, jsii.String("Table"), &TableProps{
	PartitionKey: &Attribute{
		Name: jsii.String("id"),
		Type: dynamodb.AttributeType_STRING,
	},
	ReplicationRegions: []*string{
		jsii.String("us-east-1"),
		jsii.String("us-east-2"),
		jsii.String("us-west-2"),
	},
	BillingMode: dynamodb.BillingMode_PROVISIONED,
})

globalTable.AutoScaleWriteCapacity(&EnableScalingProps{
	MinCapacity: jsii.Number(1),
	MaxCapacity: jsii.Number(10),
}).ScaleOnUtilization(&UtilizationScalingProps{
	TargetUtilizationPercent: jsii.Number(75),
})

When adding a replica region for a large table, you might want to increase the timeout for the replication operation:

globalTable := dynamodb.NewTable(this, jsii.String("Table"), &TableProps{
	PartitionKey: &Attribute{
		Name: jsii.String("id"),
		Type: dynamodb.AttributeType_STRING,
	},
	ReplicationRegions: []*string{
		jsii.String("us-east-1"),
		jsii.String("us-east-2"),
		jsii.String("us-west-2"),
	},
	ReplicationTimeout: awscdk.Duration_Hours(jsii.Number(2)),
})

Encryption

All user data stored in Amazon DynamoDB is fully encrypted at rest. When creating a new table, you can choose to encrypt using the following customer master keys (CMK) to encrypt your table:

  • AWS owned CMK - By default, all tables are encrypted under an AWS owned customer master key (CMK) in the DynamoDB service account (no additional charges apply).
  • AWS managed CMK - AWS KMS keys (one per region) are created in your account, managed, and used on your behalf by AWS DynamoDB (AWS KMS charges apply).
  • Customer managed CMK - You have full control over the KMS key used to encrypt the DynamoDB Table (AWS KMS charges apply).

Creating a Table encrypted with a customer managed CMK:

table := dynamodb.NewTable(this, jsii.String("MyTable"), &TableProps{
	PartitionKey: &Attribute{
		Name: jsii.String("id"),
		Type: dynamodb.AttributeType_STRING,
	},
	Encryption: dynamodb.TableEncryption_CUSTOMER_MANAGED,
})

// You can access the CMK that was added to the stack on your behalf by the Table construct via:
tableEncryptionKey := table.EncryptionKey

You can also supply your own key:

import kms "github.com/aws/aws-cdk-go/awscdk"


encryptionKey := kms.NewKey(this, jsii.String("Key"), &KeyProps{
	EnableKeyRotation: jsii.Boolean(true),
})
table := dynamodb.NewTable(this, jsii.String("MyTable"), &TableProps{
	PartitionKey: &Attribute{
		Name: jsii.String("id"),
		Type: dynamodb.AttributeType_STRING,
	},
	Encryption: dynamodb.TableEncryption_CUSTOMER_MANAGED,
	EncryptionKey: EncryptionKey,
})

In order to use the AWS managed CMK instead, change the code to:

table := dynamodb.NewTable(this, jsii.String("MyTable"), &TableProps{
	PartitionKey: &Attribute{
		Name: jsii.String("id"),
		Type: dynamodb.AttributeType_STRING,
	},
	Encryption: dynamodb.TableEncryption_AWS_MANAGED,
})

Get schema of table or secondary indexes

To get the partition key and sort key of the table or indexes you have configured:

var table table

schema := table.Schema()
partitionKey := schema.PartitionKey
sortKey := schema.SortKey

Kinesis Stream

A Kinesis Data Stream can be configured on the DynamoDB table to capture item-level changes.

import kinesis "github.com/aws/aws-cdk-go/awscdk"


stream := kinesis.NewStream(this, jsii.String("Stream"))

table := dynamodb.NewTable(this, jsii.String("Table"), &TableProps{
	PartitionKey: &Attribute{
		Name: jsii.String("id"),
		Type: dynamodb.AttributeType_STRING,
	},
	KinesisStream: stream,
})

# Functions

No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given construct is a CfnResource.
Return whether the given object is a Construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given construct is a CfnResource.
Return whether the given object is a Construct.
Create a new `AWS::DynamoDB::GlobalTable`.
Create a new `AWS::DynamoDB::GlobalTable`.
Create a new `AWS::DynamoDB::Table`.
Create a new `AWS::DynamoDB::Table`.
Experimental.
Experimental.
Creates a Table construct that represents an external table via table arn.
Creates a Table construct that represents an external table.
Creates a Table construct that represents an external table via table name.
Permits an IAM Principal to list all DynamoDB Streams.
Return whether the given object is a Construct.
Check whether the given construct is a Resource.

# Constants

Up to 400KiB of binary data (which must be encoded as base64 before sending to DynamoDB).
Numeric values made of up to 38 digits (positive, negative or zero).
Up to 400KiB of UTF-8 encoded text.
Pay only for what you use.
Explicitly specified Read/Write capacity units.
BatchExecuteStatement.
BatchGetItem.
BatchWriteItem.
DeleteItem.
ExecuteStatement.
ExecuteTransaction.
GetItem.
GetRecords.
PutItem.
Query.
Scan.
TransactGetItems.
TransactWriteItems.
UpdateItem.
All of the table attributes are projected into the index.
Only the specified table attributes are projected into the index.
Only the index and primary keys are projected into the index.
Only the key attributes of the modified item are written to the stream.
Both the new and the old item images of the item are written to the stream.
The entire item, as it appears after it was modified, is written to the stream.
The entire item, as it appeared before it was modified, is written to the stream.
Default table class for DynamoDB.
Table class for DynamoDB that reduces storage costs compared to existing DynamoDB Standard tables.
Server-side KMS encryption with a master key managed by AWS.
Server-side KMS encryption with a customer master key managed by customer.
Server-side KMS encryption with a master key owned by AWS.

# Structs

Represents an attribute for describing the key schema for the table and indexes.
Represents an attribute for describing the key schema for the table and indexes.
Configures a scalable target and an autoscaling policy for a table or global secondary index's read or write capacity.
Configures contributor insights settings for a replica or one of its indexes.
Allows you to specify a global secondary index for the global table.
Represents *a single element* of a key schema.
The Kinesis Data Streams configuration for the specified global table replica.
Represents the properties of a local secondary index.
Represents the settings used to enable point in time recovery.
Represents attributes that are copied (projected) from the table into an index.
Allows you to specify the read capacity settings for a replica table or a replica global secondary index when the `BillingMode` is set to `PROVISIONED` .
Represents the properties of a global secondary index that can be set on a per-replica basis.
Defines settings specific to a single replica of a global table.
Allows you to specify a KMS key identifier to be used for server-side encryption.
Represents the settings used to enable server-side encryption.
Represents the DynamoDB Streams configuration for a table in DynamoDB.
Defines a target tracking scaling policy.
Represents the settings used to enable or disable Time to Live (TTL) for the specified table.
Specifies an auto scaling policy for write capacity.
Properties for defining a `CfnGlobalTable`.
Represents an attribute for describing the key schema for the table and indexes.
The settings used to enable or disable CloudWatch Contributor Insights.
The options for imported source files in CSV format.
Represents the properties of a global secondary index.
Specifies the properties of data being imported from the S3 bucket source to the table.
The format options for the data that was imported into the target table.
Represents *a single element* of a key schema.
The Kinesis Data Streams configuration for the specified table.
Represents the properties of a local secondary index.
The settings used to enable point in time recovery.
Represents attributes that are copied (projected) from the table into an index.
Throughput for the specified table, which consists of values for `ReadCapacityUnits` and `WriteCapacityUnits` .
The S3 bucket that is being imported from.
Represents the settings used to enable server-side encryption.
Represents the DynamoDB Streams configuration for a table in DynamoDB.
Represents the settings used to enable or disable Time to Live (TTL) for the specified table.
Properties for defining a `CfnTable`.
Properties for enabling DynamoDB capacity scaling.
Properties for a global secondary index.
Properties for a local secondary index.
Represents the table schema attributes.
Properties for a secondary index.
Options for configuring a system errors metric that considers multiple operations.
Reference to a dynamodb table.
Properties of a DynamoDB Table.
Properties for a DynamoDB Table.
Properties for enabling DynamoDB utilization tracking.

# Interfaces

A CloudFormation `AWS::DynamoDB::GlobalTable`.
A CloudFormation `AWS::DynamoDB::Table`.
Interface for scalable attributes.
An interface that represents a DynamoDB Table - either created with the CDK, or an existing one.
Provides a DynamoDB table.

# Type aliases

Data types for attributes within a table.
DynamoDB's Read/Write capacity modes.
Supported DynamoDB table operations.
The set of attributes that are projected into the index.
When an item in the table is modified, StreamViewType determines what information is written to the stream for this table.
DynamoDB's table class.
What kind of server-side encryption to apply to this table.