1. Answers
  2. Configuring an AWS DynamoDB Table

How do I configure an AWS DynamoDB table?

To achieve the configuration of an AWS DynamoDB table, you need to define the table with its schema, provisioned throughput (read and write capacities), primary key attributes, and any other optional settings like global secondary indexes.

Here’s how you can create an AWS DynamoDB table for storing user data with a primary key and a global secondary index for accessing the table by email.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create the DynamoDB Table with partition key (UserId) and sort key (Email)
const users = new aws.dynamodb.Table("users", {
    name: "Users",
    billingMode: "PROVISIONED",
    hashKey: "UserId",
    rangeKey: "Email",
    readCapacity: 10,
    writeCapacity: 10,
    attributes: [
        {
            name: "UserId",
            type: "S",
        },
        {
            name: "Email",
            type: "S",
        },
    ],
    globalSecondaryIndexes: [{
        name: "EmailIndex",
        hashKey: "Email",
        projectionType: "ALL",
        writeCapacity: 10,
        readCapacity: 10,
    }],
    tags: {
        Name: "UsersTable",
        Environment: "production",
    },
});
export const tableName = users.name;
export const tableArn = users.arn;
export const gsiName = users.globalSecondaryIndexes.apply(globalSecondaryIndexes => globalSecondaryIndexes?.[0]?.name);

In this configuration:

  • The provider block sets up the AWS provider and specifies the region us-west-2.
  • The aws_dynamodb_table resource defines a DynamoDB table named Users with provisioned throughput.
  • The table has a primary key composed of UserId (partition key) and Email (sort key).
  • The global_secondary_index block adds a secondary index to allow querying the table by Email.
  • output blocks are used to export the table name, ARN, and global secondary index name for easy reference.

This configuration enables the creation and management of a DynamoDB table in AWS with specified primary and secondary indexes, making it efficient to query and manage user data.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up