github.com/mweagle/Sparta@v1.15.0/docs_source/content/reference/eventsources/cloudwatchlogs.md (about) 1 --- 2 date: 2016-03-09T19:56:50+01:00 3 title: CloudWatch Logs 4 weight: 10 5 --- 6 In this section we'll walkthrough how to trigger your lambda function in response to [CloudWatch Logs](https://aws.amazon.com/blogs/aws/new-cloudwatch-events-track-and-respond-to-changes-to-your-aws-resources/). This overview is based on the [SpartaApplication](https://github.com/mweagle/SpartaApplication) sample code if you'd rather jump to the end result. 7 8 # Goal 9 10 Assume that we're supposed to write a simple "HelloWorld" CloudWatch Logs function that should be triggered in response to any log message issued to a specific Log Group. 11 12 ## Getting Started 13 14 Our lambda function is relatively short: 15 16 ```go 17 import ( 18 awsLambdaEvents "github.com/aws/aws-lambda-go/events" 19 ) 20 func echoCloudWatchLogsEvent(ctx context.Context, cwlEvent awsLambdaEvents.CloudwatchLogsEvent) (*awsLambdaEvents.CloudwatchLogsEvent, error) { 21 logger, _ := ctx.Value(sparta.ContextKeyRequestLogger).(*logrus.Entry) 22 23 logger.WithFields(logrus.Fields{ 24 "Event": cwlEvent, 25 }).Info("Request received") 26 return &cwlEvent, nil 27 } 28 ``` 29 30 Our lambda function doesn't need to do much with the log message other than log and return it. 31 32 ## Sparta Integration 33 34 With `echoCloudWatchLogsEvent()` implemented, the next step is to integrate the **go** function with Sparta. This is done by the `appendCloudWatchLogsLambda` in the SpartaApplication [application.go](https://github.com/mweagle/SpartaApplication/blob/master/application.go) source. 35 36 Our lambda function only needs logfile write privileges, and since these are enabled by default, we can use an empty `sparta.IAMRoleDefinition` value: 37 38 ```go 39 func appendCloudWatchLogsHandler(api *sparta.API, 40 lambdaFunctions []*sparta.LambdaAWSInfo) []*sparta.LambdaAWSInfo { 41 lambdaFn, _ := sparta.NewAWSLambda(sparta.LambdaName(echoCloudWatchLogsEvent), 42 echoCloudWatchLogsEvent, 43 sparta.IAMRoleDefinition{}) 44 ``` 45 46 The next step is to add a `CloudWatchLogsSubscriptionFilter` value that represents the [CloudWatch Lambda](http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/Subscriptions.html#LambdaFunctionExample) subscription [filter information](http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/CreateSubscriptionFilter.html). 47 48 ```go 49 cloudWatchLogsPermission := sparta.CloudWatchLogsPermission{} 50 cloudWatchLogsPermission.Filters = make(map[string]sparta.CloudWatchLogsSubscriptionFilter, 1) 51 cloudWatchLogsPermission.Filters["MyFilter"] = sparta.CloudWatchLogsSubscriptionFilter{ 52 LogGroupName: "/aws/lambda/versions", 53 } 54 ``` 55 56 The `sparta.CloudWatchLogsPermission` struct provides fields for both the LogGroupName and optional Filter expression (not shown here) to use when calling [putSubscriptionFilter](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudWatchLogs.html#putSubscriptionFilter-property). 57 58 ## Add Permission 59 60 With the subscription information configured, the final step is to add the `sparta.CloudWatchLogsPermission` to our `sparta.LambdaAWSInfo` value: 61 62 ```go 63 lambdaFn.Permissions = append(lambdaFn.Permissions, cloudWatchLogsPermission) 64 ``` 65 66 Our entire function is therefore: 67 68 ```go 69 func appendCloudWatchLogsHandler(api *sparta.API, 70 lambdaFunctions []*sparta.LambdaAWSInfo) []*sparta.LambdaAWSInfo { 71 72 lambdaFn, _ := sparta.NewAWSLambda(sparta.LambdaName(echoCloudWatchLogsEvent), 73 echoCloudWatchLogsEvent, 74 sparta.IAMRoleDefinition{}) 75 cloudWatchLogsPermission := sparta.CloudWatchLogsPermission{} 76 cloudWatchLogsPermission.Filters = make(map[string]sparta.CloudWatchLogsSubscriptionFilter, 1) 77 cloudWatchLogsPermission.Filters["MyFilter"] = sparta.CloudWatchLogsSubscriptionFilter{ 78 FilterPattern: "", 79 // NOTE: This LogGroupName MUST already exist in your account, otherwise 80 // the `provision` step will fail. You can create a LogGroupName in the 81 // AWS Console 82 LogGroupName: "/aws/lambda/versions", 83 } 84 lambdaFn.Permissions = append(lambdaFn.Permissions, cloudWatchLogsPermission) 85 return append(lambdaFunctions, lambdaFn) 86 } 87 ``` 88 89 # Wrapping Up 90 91 With the `lambdaFn` fully defined, we can provide it to `sparta.Main()` and deploy our service. The workflow below is shared by all CloudWatch Logs-triggered lambda functions: 92 93 * Define the lambda function (`echoCloudWatchLogsEvent`). 94 * If needed, create the required [IAMRoleDefinition](https://godoc.org/github.com/mweagle/Sparta*IAMRoleDefinition) with appropriate privileges. 95 * Provide the lambda function & IAMRoleDefinition to `sparta.NewAWSLambda()` 96 * Create a [CloudWatchLogsPermission](https://godoc.org/github.com/mweagle/Sparta#CloudWatchLogsPermission) value. 97 * Add one or more [CloudWatchLogsSubscriptionFilter](https://godoc.org/github.com/mweagle/Sparta#CloudWatchLogsSubscriptionFilter) to the `CloudWatchLogsPermission.Filters` map that defines your lambda function's logfile subscription information. 98 * Append the `CloudWatchLogsPermission` value to the lambda function's `Permissions` slice. 99 * Include the reference in the call to `sparta.Main()`. 100 101 ## Other Resources