1. Answers
  2. Building AWS DynamoDB Table Items

How do I build an AWS DynamoDB table item?

To build and manage an AWS DynamoDB table item, you need to first set up a DynamoDB table and then define the table items you want to store. Below is an example where we’ll create a DynamoDB table for storing user data and then add an item to that table. We’ll define the resources needed and explain each part along the way.

Explanation

  1. Provider Configuration: Begin by configuring the AWS provider to allow management of your AWS resources.
  2. DynamoDB Table: Create a DynamoDB table with a specified primary key.
  3. DynamoDB Table Item: Add an item to the DynamoDB table with the corresponding attributes required.

Program

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

export = async () => {
    const users = new aws.dynamodb.Table("users", {
        name: "users-table",
        billingMode: "PAY_PER_REQUEST",
        hashKey: "user_id",
        attributes: [{
            name: "user_id",
            type: "S",
        }],
        tags: {
            Environment: "dev",
            Name: "users-table",
        },
    });
    const userItem = new aws.dynamodb.TableItem("user_item", {
        tableName: users.name,
        hashKey: "user_id",
        item: `{
  "user_id": {"S": "123"},
  "name": {"S": "John Doe"},
  "email": {"S": "john.doe@example.com"}
}
`,
    });
    return {
        tableName: users.name,
        userItem: userItem.item,
    };
}

Summary

We’ve created a program to set up a DynamoDB table and add an item to it. The example includes defining the AWS provider, creating a DynamoDB table with a primary key, adding an item to that table, and outputting the table name and item. This demonstrates how to manage and work with DynamoDB tables and items effectively.

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