Skip to content

Commit

Permalink
chore(dynamodb): make examples compile (#17313)
Browse files Browse the repository at this point in the history
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
kaizencc authored Nov 4, 2021
1 parent d24e067 commit a91cc05
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
47 changes: 19 additions & 28 deletions packages/@aws-cdk/aws-dynamodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
Here is a minimal deployable DynamoDB table definition:

```ts
import * as dynamodb from '@aws-cdk/aws-dynamodb';

const table = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING }
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
});
```

Expand All @@ -28,7 +26,8 @@ factory method. This method accepts table name or table ARN which describes the
existing table:

```ts
const table = Table.fromTableArn(this, 'ImportedTable', 'arn:aws:dynamodb:us-east-1:111111111:table/my-table');
declare const user: iam.User;
const table = dynamodb.Table.fromTableArn(this, 'ImportedTable', 'arn:aws:dynamodb:us-east-1:111111111:table/my-table');
// now you can just call methods on the table
table.grantReadWriteData(user);
```
Expand All @@ -50,11 +49,9 @@ DynamoDB supports two billing modes:
* 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.

```ts
import * as dynamodb from '@aws-cdk/aws-dynamodb';

const table = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
});
```

Expand All @@ -81,8 +78,6 @@ https://aws.amazon.com/blogs/database/how-to-use-aws-cloudformation-to-configure
You can create DynamoDB Global Tables by setting the `replicationRegions` property on a `Table`:

```ts
import * as dynamodb from '@aws-cdk/aws-dynamodb';

const globalTable = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
replicationRegions: ['us-east-1', 'us-east-2', 'us-west-2'],
Expand All @@ -100,7 +95,7 @@ you have to make sure write auto-scaling is enabled for that Table:
const globalTable = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
replicationRegions: ['us-east-1', 'us-east-2', 'us-west-2'],
billingMode: BillingMode.PROVISIONED,
billingMode: dynamodb.BillingMode.PROVISIONED,
});

globalTable.autoScaleWriteCapacity({
Expand Down Expand Up @@ -131,11 +126,9 @@ All user data stored in Amazon DynamoDB is fully encrypted at rest. When creatin
Creating a Table encrypted with a customer managed CMK:

```ts
import dynamodb = require('@aws-cdk/aws-dynamodb');

const table = new dynamodb.Table(stack, 'MyTable', {
const table = new dynamodb.Table(this, 'MyTable', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
encryption: TableEncryption.CUSTOMER_MANAGED,
encryption: dynamodb.TableEncryption.CUSTOMER_MANAGED,
});

// You can access the CMK that was added to the stack on your behalf by the Table construct via:
Expand All @@ -145,27 +138,24 @@ const tableEncryptionKey = table.encryptionKey;
You can also supply your own key:

```ts
import dynamodb = require('@aws-cdk/aws-dynamodb');
import kms = require('@aws-cdk/aws-kms');
import * as kms from '@aws-cdk/aws-kms';

const encryptionKey = new kms.Key(stack, 'Key', {
enableKeyRotation: true
const encryptionKey = new kms.Key(this, 'Key', {
enableKeyRotation: true,
});
const table = new dynamodb.Table(stack, 'MyTable', {
const table = new dynamodb.Table(this, 'MyTable', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
encryption: TableEncryption.CUSTOMER_MANAGED,
encryption: dynamodb.TableEncryption.CUSTOMER_MANAGED,
encryptionKey, // This will be exposed as table.encryptionKey
});
```

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

```ts
import dynamodb = require('@aws-cdk/aws-dynamodb');

const table = new dynamodb.Table(stack, 'MyTable', {
const table = new dynamodb.Table(this, 'MyTable', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
encryption: TableEncryption.AWS_MANAGED,
encryption: dynamodb.TableEncryption.AWS_MANAGED,
});

// In this case, the CMK _cannot_ be accessed through table.encryptionKey.
Expand All @@ -176,19 +166,20 @@ const table = new dynamodb.Table(stack, 'MyTable', {
To get the partition key and sort key of the table or indexes you have configured:

```ts
const { partitionKey, sortKey } = table.schema();
declare const table: dynamodb.Table;
const schema = table.schema();
const partitionKey = schema.partitionKey;
const sortKey = schema.sortKey;

// In case you want to get schema details for any secondary index

const { partitionKey, sortKey } = table.schema(INDEX_NAME);
// const { partitionKey, sortKey } = table.schema(INDEX_NAME);
```

## Kinesis Stream

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

```ts
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as kinesis from '@aws-cdk/aws-kinesis';

const stream = new kinesis.Stream(this, 'Stream');
Expand Down
13 changes: 13 additions & 0 deletions packages/@aws-cdk/aws-dynamodb/rosetta/default.ts-fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Fixture with packages imported, but nothing else
import { Construct } from 'constructs';
import { Duration, Stack } from '@aws-cdk/core';
import dynamodb = require('@aws-cdk/aws-dynamodb');
import iam = require('@aws-cdk/aws-iam');

class Fixture extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id);

/// here
}
}

0 comments on commit a91cc05

Please sign in to comment.