github.com/mweagle/Sparta@v1.15.0/docs_source/content/reference/eventsources/sqs.md (about) 1 --- 2 date: 2016-03-09T19:56:50+01:00 3 title: SQS 4 weight: 10 5 --- 6 7 8 In this section we'll walkthrough how to trigger your lambda function in response to AWS Simple Queue Service (SQS) events. This overview is based on the [SpartaSQS](https://github.com/mweagle/SpartaSQS) sample code if you'd rather jump to the end result. 9 10 # Goal 11 12 The goal here is to create a self-contained service that provisions a SQS queue, an AWS Lambda function that processes messages posted to the queue 13 14 ## Getting Started 15 16 We'll start with an empty lambda function and build up the needed functionality. 17 18 ```go 19 import ( 20 "context" 21 22 awsLambdaGo "github.com/aws/aws-lambda-go/events" 23 sparta "github.com/mweagle/Sparta" 24 spartaCF "github.com/mweagle/Sparta/aws/cloudformation" 25 gocf "github.com/mweagle/go-cloudformation" 26 "github.com/sirupsen/logrus" 27 ) 28 29 func sqsHandler(ctx context.Context, sqsRequest awsLambdaGo.SQSEvent) error { 30 logger, _ := ctx.Value(sparta.ContextKeyLogger).(*logrus.Logger) 31 logger.WithField("Event", sqsRequest).Info("SQS Event Received") 32 return nil 33 } 34 ``` 35 36 Since the `sqsHandler` function subscribes to SQS messages, it can use the AWS provided [SQSEvent](https://godoc.org/github.com/aws/aws-lambda-go/events#SQSEvent) to automatically unmarshal the incoming event. 37 38 Typically the lambda function would process each record in the event, but for this example we'll just log the entire batch and then return. 39 40 ## Sparta Integration 41 42 The next step is to integrate the lambda function with Sparta: 43 44 ```go 45 // 1. Create the Sparta Lambda function 46 lambdaFn, _ := sparta.NewAWSLambda(sparta.LambdaName(sqsHandler), 47 sqsHandler, 48 sparta.IAMRoleDefinition{}) 49 ``` 50 51 Once the lambda function is integrated with Sparta, we can use a [TemplateDecoratorHandler](https://godoc.org/github.com/mweagle/Sparta#TemplateDecoratorHandler) to include the SQS provisioning request as part of the overall service creation. 52 53 ## SQS Topic Definition 54 55 Decorators enable a Sparta service to provision other types of infrastructure together with the core lambda functions. In this example, our `sqsHandler` function should also provision an SQS queue from which it will receive events. This is done as in the following: 56 57 ```go 58 sqsResourceName := "LambdaSQSFTW" 59 sqsDecorator := func(serviceName string, 60 lambdaResourceName string, 61 lambdaResource gocf.LambdaFunction, 62 resourceMetadata map[string]interface{}, 63 S3Bucket string, 64 S3Key string, 65 buildID string, 66 template *gocf.Template, 67 context map[string]interface{}, 68 logger *logrus.Logger) error { 69 70 // Include the SQS resource in the application 71 sqsResource := &gocf.SQSQueue{} 72 template.AddResource(sqsResourceName, sqsResource) 73 return nil 74 } 75 lambdaFn.Decorators = []sparta.TemplateDecoratorHandler{sparta.TemplateDecoratorHookFunc(sqsDecorator)} 76 ``` 77 78 This function-level decorator includes an AWS CloudFormation [SQS::Queue](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues.html) definition that will be included with the stack definition. 79 80 ## Connecting SQS to AWS Lambda 81 82 The final step is to make the `sqsHandler` the Lambda's [EventSourceMapping](https://godoc.org/github.com/mweagle/Sparta#EventSourceMapping) target for the dynamically provisioned Queue's _ARN_: 83 84 ```go 85 lambdaFn.EventSourceMappings = append(lambdaFn.EventSourceMappings, 86 &sparta.EventSourceMapping{ 87 EventSourceArn: gocf.GetAtt(sqsResourceName, "Arn"), 88 BatchSize: 2, 89 }) 90 ``` 91 92 # Wrapping Up 93 94 With the `lambdaFn` fully defined, we can provide it to `sparta.Main()` and deploy our service. It's also possible to use a pre-existing SQS resource by providing a string literal as the `EventSourceArn` value. 95 96 ## Other Resources 97 98 * The AWS docs have an excellent [SQS event source](https://aws.amazon.com/blogs/aws/aws-lambda-adds-amazon-simple-queue-service-to-supported-event-sources/) walkthrough.