Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexkates/b57c3587d151824474e08c1baae3c938 to your computer and use it in GitHub Desktop.
Save alexkates/b57c3587d151824474e08c1baae3c938 to your computer and use it in GitHub Desktop.
// Import aws-cdk packages
import * as cdk from '@aws-cdk/core';
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as lambda from '@aws-cdk/aws-lambda';
import { DynamoEventSource } from '@aws-cdk/aws-lambda-event-sources';
export class HowToTriggerLambdaFromDdbStreamStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create a new DynamoDB Table
const table = new dynamodb.Table(this, 'Table', {
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
stream: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES,
tableName: 'Table',
});
// Create a new Lambda Function
const lambdaFunction = new lambda.Function(this, 'Function', {
code: lambda.Code.fromAsset('src'),
handler: 'index.handler',
functionName: 'TableStreamHandler',
runtime: lambda.Runtime.NODEJS_12_X,
});
// Add an SQS Event Source from the DynamoDB Table to the Lambda Function
lambdaFunction.addEventSource(new DynamoEventSource(table, {
startingPosition: lambda.StartingPosition.LATEST,
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment