A construct that simplifies the creation and configuration of a DynamoDB table with Lambda integration.

The DynamoTable construct creates a DynamoDB table with sensible defaults and automatically grants the necessary permissions to specified Lambda functions to read from and/or write to the table.

This construct is ideal for projects where you need to quickly set up a DynamoDB table that your Lambda functions can interact with, without having to manually configure IAM permissions.

import { Stack, RemovalPolicy } from 'aws-cdk-lib';
import { lambda, dynamodb } from '@reuters-graphics/structs';
import { Construct } from 'constructs';
import * as dynamodbLib from 'aws-cdk-lib/aws-dynamodb';

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

// Create a Lambda function
const myLambda = new lambda.NodeJsLambda(this, 'my-lambda', {
entry: './src/index.ts',
handler: 'handle',
});

// Create a DynamoDB table with Lambda write permissions
const myTable = new dynamodb.DynamoTable(this, 'my-table', {
partitionKey: {
name: 'id',
type: dynamodbLib.AttributeType.STRING
},
sortKey: {
name: 'timestamp',
type: dynamodbLib.AttributeType.NUMBER
},
writeLambdas: [myLambda.lambda],
pointInTimeRecovery: true,
removalPolicy: RemovalPolicy.DESTROY,
});

// Access the underlying DynamoDB table
const table = myTable.table;

// Add environment variable to Lambda with table name
myLambda.lambda.addEnvironment('TABLE_NAME', myTable.table.tableName);
}
}

Hierarchy

  • Construct
    • DynamoTable

Constructors

  • Creates a new DynamoTable construct.

    Parameters

    • scope: Construct

      The scope in which this construct is defined.

    • id: string

      The scoped construct ID.

    • props: DynamoTableOptions

      Configuration options for the DynamoDB table.

    Returns DynamoTable

Properties

table: Table

The underlying DynamoDB table created by this construct.

node: Node

The tree node.

Methods

  • Checks if x is a construct.

    Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

    Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

    Parameters

    • x: any

      Any object

    Returns x is Construct

    true if x is an object created from a class which extends Construct.

  • Grant write permissions to a Lambda function for this table.

    Parameters

    • lambdaFunction: Function

      The Lambda function to grant write permissions to.

    Returns void

  • Grant read permissions to a Lambda function for this table.

    Parameters

    • lambdaFunction: Function

      The Lambda function to grant read permissions to.

    Returns void

  • Grant full permissions (read and write) to a Lambda function for this table.

    Parameters

    • lambdaFunction: Function

      The Lambda function to grant full permissions to.

    Returns void

  • Returns a string representation of this construct.

    Returns string