import * as cdk from '@aws-cdk/core' ; import { Table , BillingMode , AttributeType , StreamViewType } from '@aws-cdk/aws-dynamodb' ; import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs' ; import * as lambda from '@aws-cdk/aws-lambda' ; import * as path from 'path' ; import { DynamoEventSource } from '@aws-cdk/aws-lambda-event-sources' ; export class DynamoToLambdaCdkStack extends cdk . Stack { constructor ( scope , id , props ? ) { super ( scope , id , props ) ; const dynamoTable = new Table ( this , 'DynamoTable' , { partitionKey : { name : 'pk' , type : AttributeType . STRING } , billingMode : BillingMode . PAY_PER_REQUEST , stream : StreamViewType . NEW_IMAGE } ) ; const lambdaReadStream = new NodejsFunction ( this , 'readStream' , { entry : 'lambda-fns/readStream/handler.js' , handler : 'handler' } ) ; dynamoTable . grantReadWriteData ( lambdaReadStream ) ; lambdaReadStream . addEventSource ( new DynamoEventSource ( dynamoTable , { startingPosition : lambda . StartingPosition . TRIM_HORIZON , batchSize : 10 , retryAttempts : 0 } ) ) ; new cdk . CfnOutput ( this , 'DynamoDbTableName' , { value : dynamoTable . tableName } ) ; new cdk . CfnOutput ( this , 'LambdaFunctionArn' , { value : lambdaReadStream . functionArn } ) ; } }