How to use @aws-cdk/aws-dynamodb - 10 common examples

To help you get started, we’ve selected a few @aws-cdk/aws-dynamodb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github punchcard / punchcard / examples / lib / invoke-function.ts View on Github external
import { Build } from 'punchcard/lib/core/build';

export const app = new Core.App();
const stack = app.root.map(app => new cdk.Stack(app, 'invoke-function-example'));

const table = new DynamoDB.Table(stack, 'my-table', {
  partitionKey: 'id',
  attributes: {
    id: string(),
    count: integer({
      minimum: 0
    }),
    anyProperty: dynamic 
  },
  tableProps: Build.of({
    billingMode: BillingMode.PAY_PER_REQUEST
  })
});

// create a function that increments counts in a dynamodb table
// Function
const incrementer = new Lambda.Function(stack, 'Callable', {
  // request is a structure with a single property, 'id'
  request: struct({
    id: string()
  }),
  // response is just an integer
  response: integer(),
  depends: table.readWriteAccess(),
}, async (request, table) => {
  console.log(request);
  const item = await table.get({
github aws-samples / aws-cdk-examples / typescript / appsync-graphql-dynamodb / index.ts View on Github external
}
      type Query {
        all(limit: Int, nextToken: String): Paginated${tableName}!
        getOne(${tableName}Id: ID!): ${tableName}
      }
      type Mutation {
        save(name: String!): ${tableName}
        delete(${tableName}Id: ID!): ${tableName}
      }
      type Schema {
        query: Query
        mutation: Mutation
      }`
    });

    const itemsTable = new Table(this, 'ItemsTable', {
      tableName: tableName,
      partitionKey: {
        name: `${tableName}Id`,
        type: AttributeType.STRING
      },
      billingMode: BillingMode.PAY_PER_REQUEST,
      stream: StreamViewType.NEW_IMAGE,

      // The default removal policy is RETAIN, which means that cdk destroy will not attempt to delete
      // the new table, and it will remain in your account until manually deleted. By setting the policy to 
      // DESTROY, cdk destroy will delete the table (even if it has data in it)
      removalPolicy: cdk.RemovalPolicy.DESTROY, // NOT recommended for production code
    });

    const itemsTableRole = new Role(this, 'ItemsDynamoDBRole', {
      assumedBy: new ServicePrincipal('appsync.amazonaws.com')
github deepalert / deepalert / lib / deepalert-stack.ts View on Github external
constructor(scope: cdk.Construct, id: string, props: property) {
    super(scope, id, props);

    const lambdaRole = props.lambdaRoleARN
      ? iam.Role.fromRoleArn(this, "LambdaRole", props.lambdaRoleARN, {
          mutable: false,
        })
      : undefined;
    const sfnRole = props.sfnRoleARN
      ? iam.Role.fromRoleArn(this, "SfnRole", props.sfnRoleARN, {
          mutable: false,
        })
      : undefined;

    this.cacheTable = new dynamodb.Table(this, "cacheTable", {
      billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
      partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
      sortKey: { name: "sk", type: dynamodb.AttributeType.STRING },
      timeToLiveAttribute: "expires_at",
    });

    // ----------------------------------------------------------------
    // Messaging Channels
    this.alertTopic = new sns.Topic(this, "alertTopic");
    this.taskTopic = new sns.Topic(this, "taskTopic");
    this.contentTopic = new sns.Topic(this, "contentTopic");
    this.attributeTopic = new sns.Topic(this, "attributeTopic");
    this.reportTopic = new sns.Topic(this, "reportTopic");

    const alertQueueTimeout = cdk.Duration.seconds(30);
    this.alertQueue = new sqs.Queue(this, "alertQueue", {
github aws-samples / aws-cdk-changelogs-demo / changelogs-md.js View on Github external
const changelogsTable = new dynamodb.Table(this, 'Changelogs', {
      partitionKey: { name: 'changelog', type: dynamodb.AttributeType.STRING },
      billingMode: dynamodb.BillingMode.Provisioned
    });

    const readScaling = changelogsTable.autoScaleReadCapacity({
      minCapacity: 211,
      maxCapacity: 300
    });

    readScaling.scaleOnUtilization({
      targetUtilizationPercent: 75
    });

    // A table to store the list of feeds
    const feedsTable = new dynamodb.Table(this, 'Feeds', {
      partitionKey: { name: 'feed', type: dynamodb.AttributeType.STRING },
      billingMode: dynamodb.BillingMode.PayPerRequest
    });

    // A table which stores the auto complete search index
    const searchIndexTable = new dynamodb.Table(this, 'search-index', {
      partitionKey: { name: 'fragment', type: dynamodb.AttributeType.STRING },
      sortKey: { name: 'score', type: dynamodb.AttributeType.STRING },
      timeToLiveAttribute: 'validUntil',
      billingMode: dynamodb.BillingMode.PayPerRequest
    });

    // An S3 bucket which holds the web content
    const webBucket = new s3.Bucket(this, 'web-bucket', {
      publicReadAccess: true,
      websiteIndexDocument: 'index.html'
github nathanpeck / screenshot-service / app.3.js View on Github external
// Create an ECS cluster
    this.cluster = new ecs.Cluster(this, 'cluster', {
      vpc: this.vpc
    });

    // Create S3 bucket
    this.screenshotBucket = new s3.Bucket(this, 'screenshot-bucket', {
      publicReadAccess: true
    });

    // Create queue
    this.screenshotQueue = new sqs.Queue(this, 'screenshot-queue');

    // Create DynamoDB table
    this.screenshotTable = new dynamodb.Table(this, 'screenshots', {
      partitionKey: { name: 'id', type: dynamodb.AttributeType.String },
      billingMode: dynamodb.BillingMode.PayPerRequest
    });
  }
}
github aws-samples / aws-cdk-changelogs-demo / changelogs-md.js View on Github external
constructor(parent, id, props) {
    super(parent, id, props);

    // Network to run everything in
    const vpc = new ec2.Vpc(this, 'NpmFollowerVpc', {
      maxAZs: 2,
      natGateways: 1
    });

    // A table to store the list of changelogs and their metadata in
    const changelogsTable = new dynamodb.Table(this, 'Changelogs', {
      partitionKey: { name: 'changelog', type: dynamodb.AttributeType.STRING },
      billingMode: dynamodb.BillingMode.Provisioned
    });

    const readScaling = changelogsTable.autoScaleReadCapacity({
      minCapacity: 211,
      maxCapacity: 300
    });

    readScaling.scaleOnUtilization({
      targetUtilizationPercent: 75
    });

    // A table to store the list of feeds
    const feedsTable = new dynamodb.Table(this, 'Feeds', {
      partitionKey: { name: 'feed', type: dynamodb.AttributeType.STRING },
github aws-samples / aws-cdk-changelogs-demo / changelogs-md.js View on Github external
minCapacity: 211,
      maxCapacity: 300
    });

    readScaling.scaleOnUtilization({
      targetUtilizationPercent: 75
    });

    // A table to store the list of feeds
    const feedsTable = new dynamodb.Table(this, 'Feeds', {
      partitionKey: { name: 'feed', type: dynamodb.AttributeType.STRING },
      billingMode: dynamodb.BillingMode.PayPerRequest
    });

    // A table which stores the auto complete search index
    const searchIndexTable = new dynamodb.Table(this, 'search-index', {
      partitionKey: { name: 'fragment', type: dynamodb.AttributeType.STRING },
      sortKey: { name: 'score', type: dynamodb.AttributeType.STRING },
      timeToLiveAttribute: 'validUntil',
      billingMode: dynamodb.BillingMode.PayPerRequest
    });

    // An S3 bucket which holds the web content
    const webBucket = new s3.Bucket(this, 'web-bucket', {
      publicReadAccess: true,
      websiteIndexDocument: 'index.html'
    });

    // An S3 bucket which holds the API content
    const apiBucket = new s3.Bucket(this, 'api-bucket', {
      publicReadAccess: true,
      websiteIndexDocument: 'index.json'
github deepalert / deepalert / lib / deepalert-stack.ts View on Github external
const lambdaRole = props.lambdaRoleARN
      ? iam.Role.fromRoleArn(this, "LambdaRole", props.lambdaRoleARN, {
          mutable: false,
        })
      : undefined;
    const sfnRole = props.sfnRoleARN
      ? iam.Role.fromRoleArn(this, "SfnRole", props.sfnRoleARN, {
          mutable: false,
        })
      : undefined;

    this.cacheTable = new dynamodb.Table(this, "cacheTable", {
      billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
      partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
      sortKey: { name: "sk", type: dynamodb.AttributeType.STRING },
      timeToLiveAttribute: "expires_at",
    });

    // ----------------------------------------------------------------
    // Messaging Channels
    this.alertTopic = new sns.Topic(this, "alertTopic");
    this.taskTopic = new sns.Topic(this, "taskTopic");
    this.contentTopic = new sns.Topic(this, "contentTopic");
    this.attributeTopic = new sns.Topic(this, "attributeTopic");
    this.reportTopic = new sns.Topic(this, "reportTopic");

    const alertQueueTimeout = cdk.Duration.seconds(30);
    this.alertQueue = new sqs.Queue(this, "alertQueue", {
      visibilityTimeout: alertQueueTimeout,
    });
    this.alertTopic.addSubscription(new SqsSubscription(this.alertQueue));
github deepalert / deepalert / lib / deepalert-stack.ts View on Github external
super(scope, id, props);

    const lambdaRole = props.lambdaRoleARN
      ? iam.Role.fromRoleArn(this, "LambdaRole", props.lambdaRoleARN, {
          mutable: false,
        })
      : undefined;
    const sfnRole = props.sfnRoleARN
      ? iam.Role.fromRoleArn(this, "SfnRole", props.sfnRoleARN, {
          mutable: false,
        })
      : undefined;

    this.cacheTable = new dynamodb.Table(this, "cacheTable", {
      billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
      partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
      sortKey: { name: "sk", type: dynamodb.AttributeType.STRING },
      timeToLiveAttribute: "expires_at",
    });

    // ----------------------------------------------------------------
    // Messaging Channels
    this.alertTopic = new sns.Topic(this, "alertTopic");
    this.taskTopic = new sns.Topic(this, "taskTopic");
    this.contentTopic = new sns.Topic(this, "contentTopic");
    this.attributeTopic = new sns.Topic(this, "attributeTopic");
    this.reportTopic = new sns.Topic(this, "reportTopic");

    const alertQueueTimeout = cdk.Duration.seconds(30);
    this.alertQueue = new sqs.Queue(this, "alertQueue", {
      visibilityTimeout: alertQueueTimeout,
    });
github humank / EventStormingWorkShop / deployment / coffeeshop-cdk / lib / coffee-shop-code-pipeline.ts View on Github external
const fargateTaskRole = fargatesvc.service.taskDefinition.taskRole;
        fargateTaskRole.addToPolicy(new iam.PolicyStatement({
            resources: ['*'],
            actions: ['events:*']
        }));
        const orderTable = new dynamodb.Table(this, 'Order', {
            partitionKey: { name: 'seqNo', type: dynamodb.AttributeType.NUMBER },
            billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
            tableName: 'Order',
        });

        orderTable.grantFullAccess(fargateTaskRole);

        const coffeeTable = new dynamodb.Table(this, 'Coffee', {
            partitionKey: { name: 'seqNo', type: dynamodb.AttributeType.NUMBER },
            billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
            tableName: 'Coffee',
        });

        coffeeTable.grantFullAccess(fargateTaskRole);

        const rule = new Rule(this, 'OrderCreatedRule',{
            eventPattern:{
                source:["solid.humank.coffeeshop.order"],
                detailType:['customevent']
            },
            // eventBus: coffeeshop_eventbus,
            ruleName: 'OrderCreatedRule',
        });

        //add ssm parameter store for cloudwatchevent put usage