CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-pulumi--aws

A Pulumi package for creating and managing Amazon Web Services (AWS) cloud resources with infrastructure-as-code.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

dynamodb.mddocs/database/

DynamoDB - NoSQL Database

Amazon DynamoDB is a fully managed NoSQL database service providing fast and predictable performance with seamless scalability.

Common Tasks

Create a simple key-value table

const table = new aws.dynamodb.Table("users", {
    attributes: [{ name: "userId", type: "S" }],
    hashKey: "userId",
    billingMode: "PAY_PER_REQUEST",
});

Create a table with sort key and GSI

const ordersTable = new aws.dynamodb.Table("orders", {
    attributes: [
        { name: "customerId", type: "S" },
        { name: "orderDate", type: "N" },
        { name: "status", type: "S" },
    ],
    hashKey: "customerId",
    rangeKey: "orderDate",
    billingMode: "PAY_PER_REQUEST",
    globalSecondaryIndexes: [{
        name: "StatusIndex",
        hashKey: "status",
        projectionType: "ALL",
    }],
});

Enable DynamoDB Streams

const streamTable = new aws.dynamodb.Table("events", {
    attributes: [{ name: "id", type: "S" }],
    hashKey: "id",
    billingMode: "PAY_PER_REQUEST",
    streamEnabled: true,
    streamViewType: "NEW_AND_OLD_IMAGES",
});

Core Resources

Table

Create DynamoDB tables.

class Table extends pulumi.CustomResource {
    constructor(name: string, args: TableArgs, opts?: pulumi.CustomResourceOptions);

    readonly arn: pulumi.Output<string>;
    readonly name: pulumi.Output<string>;
    readonly streamArn: pulumi.Output<string>;
}

interface TableArgs {
    attributes: pulumi.Input<pulumi.Input<TableAttribute>[]>;
    hashKey: pulumi.Input<string>;
    rangeKey?: pulumi.Input<string>;
    billingMode?: pulumi.Input<"PROVISIONED" | "PAY_PER_REQUEST">;
    readCapacity?: pulumi.Input<number>;
    writeCapacity?: pulumi.Input<number>;
    streamEnabled?: pulumi.Input<boolean>;
    streamViewType?: pulumi.Input<"NEW_IMAGE" | "OLD_IMAGE" | "NEW_AND_OLD_IMAGES" | "KEYS_ONLY">;
    globalSecondaryIndexes?: pulumi.Input<pulumi.Input<TableGlobalSecondaryIndex>[]>;
    localSecondaryIndexes?: pulumi.Input<pulumi.Input<TableLocalSecondaryIndex>[]>;
    ttl?: pulumi.Input<TableTtl>;
    tags?: pulumi.Input<{[key: string]: pulumi.Input<string>}>;
}

interface TableAttribute {
    name: pulumi.Input<string>;
    type: pulumi.Input<"S" | "N" | "B">;
}

interface TableGlobalSecondaryIndex {
    name: pulumi.Input<string>;
    hashKey: pulumi.Input<string>;
    rangeKey?: pulumi.Input<string>;
    projectionType: pulumi.Input<"ALL" | "KEYS_ONLY" | "INCLUDE">;
    nonKeyAttributes?: pulumi.Input<pulumi.Input<string>[]>;
    readCapacity?: pulumi.Input<number>;
    writeCapacity?: pulumi.Input<number>;
}

interface TableLocalSecondaryIndex {
    name: pulumi.Input<string>;
    rangeKey: pulumi.Input<string>;
    projectionType: pulumi.Input<"ALL" | "KEYS_ONLY" | "INCLUDE">;
    nonKeyAttributes?: pulumi.Input<pulumi.Input<string>[]>;
}

interface TableTtl {
    attributeName: pulumi.Input<string>;
    enabled?: pulumi.Input<boolean>;
}

Example: Complete table with indexes and TTL

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

const table = new aws.dynamodb.Table("users", {
    attributes: [
        { name: "userId", type: "S" },
        { name: "email", type: "S" },
    ],
    hashKey: "userId",
    billingMode: "PAY_PER_REQUEST",
    streamEnabled: true,
    streamViewType: "NEW_AND_OLD_IMAGES",
    globalSecondaryIndexes: [{
        name: "EmailIndex",
        hashKey: "email",
        projectionType: "ALL",
    }],
    ttl: {
        attributeName: "expiresAt",
        enabled: true,
    },
    tags: { Environment: "production" },
});

export const tableName = table.name;
export const tableArn = table.arn;
export const streamArn = table.streamArn;

Example: Provisioned capacity with auto-scaling

const provisionedTable = new aws.dynamodb.Table("high-traffic", {
    attributes: [
        { name: "pk", type: "S" },
        { name: "sk", type: "S" },
    ],
    hashKey: "pk",
    rangeKey: "sk",
    billingMode: "PROVISIONED",
    readCapacity: 5,
    writeCapacity: 5,
    tags: { Application: "web" },
});

// Auto-scaling for read capacity
const readTarget = new aws.appautoscaling.Target("read-target", {
    maxCapacity: 100,
    minCapacity: 5,
    resourceId: pulumi.interpolate`table/${provisionedTable.name}`,
    scalableDimension: "dynamodb:table:ReadCapacityUnits",
    serviceNamespace: "dynamodb",
});

const readPolicy = new aws.appautoscaling.Policy("read-policy", {
    policyType: "TargetTrackingScaling",
    resourceId: readTarget.resourceId,
    scalableDimension: readTarget.scalableDimension,
    serviceNamespace: readTarget.serviceNamespace,
    targetTrackingScalingPolicyConfiguration: {
        targetValue: 70,
        predefinedMetricSpecification: {
            predefinedMetricType: "DynamoDBReadCapacityUtilization",
        },
    },
});

Example: Global and local secondary indexes

const complexTable = new aws.dynamodb.Table("orders", {
    attributes: [
        { name: "customerId", type: "S" },
        { name: "orderDate", type: "N" },
        { name: "status", type: "S" },
        { name: "totalAmount", type: "N" },
    ],
    hashKey: "customerId",
    rangeKey: "orderDate",
    billingMode: "PAY_PER_REQUEST",

    // Query orders by status
    globalSecondaryIndexes: [{
        name: "StatusIndex",
        hashKey: "status",
        rangeKey: "orderDate",
        projectionType: "ALL",
    }],

    // Query orders by amount for the same customer
    localSecondaryIndexes: [{
        name: "AmountIndex",
        rangeKey: "totalAmount",
        projectionType: "KEYS_ONLY",
    }],
});

Related Services

  • Lambda - Process DynamoDB Streams with Lambda functions
  • Kinesis - Stream DynamoDB changes to Kinesis Data Streams
  • IAM - Control fine-grained access to tables and items
  • CloudWatch - Monitor table metrics and set alarms
  • Backup - Automated backups and point-in-time recovery
  • DynamoDB Accelerator (DAX) - In-memory cache for DynamoDB

Install with Tessl CLI

npx tessl i tessl/npm-pulumi--aws@7.16.0

docs

index.md

quickstart.md

README.md

tile.json