github.com/mweagle/Sparta@v1.15.0/docs_source/content/reference/eventsources/dynamodb.md (about) 1 --- 2 date: 2016-03-09T19:56:50+01:00 3 title: DynamoDB 4 weight: 10 5 --- 6 7 In this section we'll walkthrough how to trigger your lambda function in response to DynamoDB stream events. This overview is based on the [SpartaApplication](https://github.com/mweagle/SpartaApplication) sample code if you'd rather jump to the end result. 8 9 # Goal 10 11 Assume that we're given a DynamoDB stream. See [below](http://localhost:1313/docs/eventsources/dynamodb/#creatingDynamoDBStream:d680e8a854a7cbad6d490c445cba2eba) for details on how to create the stream. We've been asked to write a lambda function that logs when operations are performed to the table so that we can perform offline analysis. 12 13 ## Getting Started 14 15 We'll start with an empty lambda function and build up the needed functionality. 16 17 ```go 18 import ( 19 awsLambdaEvents "github.com/aws/aws-lambda-go/events" 20 ) 21 func echoDynamoDBEvent(ctx context.Context, ddbEvent awsLambdaEvents.DynamoDBEvent) (*awsLambdaEvents.DynamoDBEvent, error) { 22 logger, _ := ctx.Value(sparta.ContextKeyRequestLogger).(*logrus.Entry) 23 logger.WithFields(logrus.Fields{ 24 "Event": ddbEvent, 25 }).Info("Event received") 26 return &ddbEvent, nil 27 } 28 ``` 29 30 Since the `echoDynamoDBEvent` is triggered by Dynamo events, we can leverage the AWS Go Lambda SDK [event types](https://godoc.org/github.com/aws/aws-lambda-go/events) 31 to access the record. 32 33 ## Sparta Integration 34 35 With the core of the `echoDynamoDBEvent` complete, the next step is to integrate the **go** function with Sparta. This is performed by the [appendDynamoDBLambda](https://github.com/mweagle/SpartaApplication/blob/master/application.go#L114) function. Since the `echoDynamoDBEvent` function doesn't access any additional services (Sparta enables CloudWatch Logs privileges by default), the integration is pretty straightforward: 36 37 ```go 38 lambdaFn, _ := sparta.NewAWSLambda( 39 sparta.LambdaName(echoDynamoDBEvent), 40 echoDynamoDBEvent, 41 sparta.IAMRoleDefinition{}) 42 ``` 43 44 ## Event Source Mappings 45 46 If we were to deploy this Sparta application, the `echoDynamoDBEvent` function would have the ability to log DynamoDB stream events, but would not be invoked in response to events published by the stream. To register for notifications, we need to configure the lambda's [EventSourceMappings](http://docs.aws.amazon.com/lambda/latest/dg/intro-core-components.html#intro-core-components-event-sources): 47 48 ```go 49 lambdaFn.EventSourceMappings = append(lambdaFn.EventSourceMappings, 50 &lambda.CreateEventSourceMappingInput{ 51 EventSourceArn: aws.String(dynamoTestStream), 52 StartingPosition: aws.String("TRIM_HORIZON"), 53 BatchSize: aws.Int64(10), 54 Enabled: aws.Bool(true), 55 }) 56 lambdaFunctions = append(lambdaFunctions, lambdaFn) 57 ``` 58 59 The `dynamoTestStream` param is the ARN of the Dynamo stream that that your lambda function will [poll](http://docs.aws.amazon.com/lambda/latest/dg/intro-invocation-modes.html) (eg: _arn:aws:dynamodb:us-west-2:000000000000:table/myDynamoDBTable/stream/2015-12-05T16:28:11.869_). 60 61 The `EventSourceMappings` field is transformed into the appropriate [CloudFormation Resource](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html) which enables automatic polling of the DynamoDB stream. 62 63 ## Wrapping Up 64 65 With the `lambdaFn` fully defined, we can provide it to `sparta.Main()` and deploy our service. The workflow below is shared by all DynamoDB stream based lambda functions: 66 67 * Define the lambda function (`echoDynamoDBEvent`). 68 * If needed, create the required [IAMRoleDefinition](https://godoc.org/github.com/mweagle/Sparta*IAMRoleDefinition) with appropriate privileges if the lambda function accesses other AWS services. 69 * Provide the lambda function & IAMRoleDefinition to `sparta.NewAWSLambda()` 70 * Add the necessary [EventSourceMappings](https://godoc.org/github.com/aws/aws-sdk-go/service/lambda#CreateEventSourceMappingInput) to the `LambdaAWSInfo` struct so that the lambda function is properly configured. 71 72 ## Other Resources 73 74 * [Using Triggers for Cross Region DynamoDB Replication](https://aws.amazon.com/blogs/aws/dynamodb-update-triggers-streams-lambda-cross-region-replication-app/) 75 76 <hr /> 77 # Appendix 78 79 ### Creating a DynamoDB Stream 80 81 To create a DynamoDB stream for a given table, follow the steps below: 82 83 #### Select Table 84 85  86 87 #### Enable Stream 88 89  90 91 #### Copy ARN 92  93 94 The **Latest stream ARN** value is the value that should be provided as the `EventSourceArn` in to the [Event Source Mappings](http://localhost:1313/docs/eventsources/dynamodb/#eventSourceMapping:d680e8a854a7cbad6d490c445cba2eba).