github.com/mweagle/Sparta@v1.15.0/docs_source/content/reference/eventsources/sns.md (about) 1 --- 2 date: 2016-03-09T19:56:50+01:00 3 title: SNS 4 weight: 10 5 --- 6 7 In this section we'll walkthrough how to trigger your lambda function in response to SNS events. This overview is based on the [SpartaApplication](https://github.com/mweagle/SpartaApplication/blob/master/application.go#L79) sample code if you'd rather jump to the end result. 8 9 # Goal 10 11 Assume that we have an SNS topic that broadcasts notifications. We've been asked to write a lambda function that logs the _Subject_ and _Message_ text to CloudWatch logs for later processing. 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 echoSNSEvent(ctx context.Context, snsEvent awsLambdaEvents.SNSEvent) (*awsLambdaEvents.SNSEvent, error) { 22 logger, _ := ctx.Value(sparta.ContextKeyRequestLogger).(*logrus.Entry) 23 logger.WithFields(logrus.Fields{ 24 "Event": snsEvent, 25 }).Info("Event received") 26 return &snsEvent, nil 27 } 28 ``` 29 30 ## Unmarshalling the SNS Event 31 32 SNS events are delivered in batches, via lists of [SNSEventRecords](https://godoc.org/github.com/aws/aws-lambda-go/events#SNSEventRecord 33 ), so we'll need to process each record. 34 35 ```go 36 for _, eachRecord := range lambdaEvent.Records { 37 logger.WithFields(logrus.Fields{ 38 "Subject": eachRecord.Sns.Subject, 39 "Message": eachRecord.Sns.Message, 40 }).Info("SNS Event") 41 } 42 ``` 43 44 That's enough to get the data into CloudWatch Logs. 45 46 ## Sparta Integration 47 48 With the core of the `echoSNSEvent` complete, the next step is to integrate the **go** function with Sparta. This is performed by 49 the [appendSNSLambda](https://github.com/mweagle/SpartaApplication/blob/master/application.go#L79) function. Since the `echoSNSEvent` 50 function doesn't access any additional services (Sparta enables CloudWatch Logs privileges by default), the integration is 51 pretty straightforward: 52 53 ```go 54 lambdaFn, _ := sparta.NewAWSLambda(sparta.LambdaName(echoSNSEvent), 55 echoSNSEvent, 56 sparta.IAMRoleDefinition{}) 57 ``` 58 59 ## Event Source Registration 60 61 If we were to deploy this Sparta application, the `echoSNSEvent` function would have the ability to log SNS events, but would not be invoked in response to messages published to that topic. To register for notifications, we need to configure the lambda's [Permissions](http://docs.aws.amazon.com/lambda/latest/dg/intro-permission-model.html): 62 63 ```go 64 lambdaFn.Permissions = append(lambdaFn.Permissions, sparta.SNSPermission{ 65 BasePermission: sparta.BasePermission{ 66 SourceArn: snsTopic, 67 }, 68 }) 69 lambdaFunctions = append(lambdaFunctions, lambdaFn) 70 ``` 71 72 The `snsTopic` param is the ARN of the SNS topic that will notify your lambda function (eg: _arn:aws:sns:us-west-2:000000000000:myTopicName). 73 74 See the [S3 docs](http://gosparta.io/docs/eventsources/s3/#eventSourceRegistration) for more information on how the _Permissions_ data is processed. 75 76 # Wrapping Up 77 78 With the `lambdaFn` fully defined, we can provide it to `sparta.Main()` and deploy our service. The workflow below is shared by all SNS-triggered lambda function: 79 80 * Define the lambda function (`echoSNSEvent`). 81 * 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. 82 * Provide the lambda function & IAMRoleDefinition to `sparta.NewAWSLambda()` 83 * Add the necessary [Permissions](https://godoc.org/github.com/mweagle/Sparta#LambdaAWSInfo) to the `LambdaAWSInfo` struct so that the lambda function is triggered. 84 85 ## Other Resources 86 87 * TBD