github.com/mweagle/Sparta@v1.15.0/docs_source/content/reference/eventsources/kinesis.md (about) 1 --- 2 date: 2016-03-09T19:56:50+01:00 3 title: Kinesis 4 weight: 10 5 --- 6 7 In this section we'll walkthrough how to trigger your lambda function in response to [Amazon Kinesis](https://aws.amazon.com/kinesis/) streams. This overview is based on the [SpartaApplication](https://github.com/mweagle/SpartaApplication/blob/master/application.go#L130) sample code if you'd rather jump to the end result. 8 9 # Goal 10 11 The goal of this example is to provision a Sparta lambda function that logs Amazon Kinesis events to CloudWatch logs. 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 echoKinesisEvent(ctx context.Context, kinesisEvent awsLambdaEvents.KinesisEvent) (*awsLambdaEvents.KinesisEvent, error) { 22 logger, _ := ctx.Value(sparta.ContextKeyRequestLogger).(*logrus.Entry) 23 logger.WithFields(logrus.Fields{ 24 "Event": kinesisEvent, 25 }).Info("Event received") 26 return &kinesisEvent, nil 27 } 28 ``` 29 30 For this sample all we're going to do is transparently unmarshal the Kinesis event to an AWS Lambda [event](https://godoc.org/github.com/aws/aws-lambda-go/events), log 31 it, and return the value. 32 33 With the function defined let's register it with Sparta. 34 35 ## Sparta Integration 36 37 First we wrap the **go** function in a [LambdaAWSInfo](https://godoc.org/github.com/mweagle/Sparta#LambdaAWSInfo) struct: 38 39 ```go 40 lambdaFn, _ := sparta.NewAWSLambda(sparta.LambdaName(echoKinesisEvent), 41 echoKinesisEvent, 42 sparta.IAMRoleDefinition{}) 43 ``` 44 45 Since our lambda function doesn't access any other AWS Services, we can use an empty IAMRoleDefinition (`sparta.IAMRoleDefinition{}`). 46 47 ## Event Source Registration 48 49 Then last step is to configure our AWS Lambda function with Kinesis as the [EventSource](http://docs.aws.amazon.com/lambda/latest/dg/intro-core-components.html) 50 51 ```go 52 lambdaFn.EventSourceMappings = append(lambdaFn.EventSourceMappings, 53 &lambda.CreateEventSourceMappingInput{ 54 EventSourceArn: aws.String(kinesisTestStream), 55 StartingPosition: aws.String("TRIM_HORIZON"), 56 BatchSize: aws.Int64(100), 57 Enabled: aws.Bool(true), 58 }) 59 ``` 60 61 The `kinesisTestStream` parameter is the Kinesis stream ARN (eg: _arn:aws:kinesis:us-west-2:123412341234:stream/kinesisTestStream_) whose events will trigger lambda execution. 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 Kinesis-triggered lambda functions: 66 67 * Define the lambda function (`echoKinesisEvent`). 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 # Notes 73 74 * The Kinesis stream and the AWS Lambda function must be provisioned in the same region. 75 * The AWS docs have an excellent [Kinesis EventSource](http://docs.aws.amazon.com/lambda/latest/dg/walkthrough-kinesis-events-adminuser.html) walkthrough.