Skip to content

Commit a5a3845

Browse files
authored
feat(event-source): add SQS as Lambda event source (aws#451)
1 parent 3cfb56d commit a5a3845

29 files changed

+596
-208
lines changed

docs/cloudformation_compatibility.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Kinesis
9191
Property Name Intrinsic(s) Supported Reasons
9292
======================== ================================== ========================
9393
Stream All
94+
Queue All
9495
StartingPosition All
9596
BatchSize All
9697
======================== ================================== ========================

docs/internals/generated_resources.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,33 @@ AWS::Lambda::Permissions MyFunction\ **MyTrigger**\ Permission
220220
AWS::Lambda::EventSourceMapping MyFunction\ **MyTrigger**
221221
================================== ================================
222222

223+
SQS
224+
^^^^^^^
225+
226+
Example:
227+
228+
.. code:: yaml
229+
230+
MyFunction:
231+
Type: AWS::Serverless::Function
232+
Properties:
233+
...
234+
Events:
235+
MyTrigger:
236+
Type: SQS
237+
Properties:
238+
Queue: arn:aws:sqs:us-east-1:123456789012:my-queue
239+
...
240+
241+
Additional generated resources:
242+
243+
================================== ================================
244+
CloudFormation Resource Type Logical ID
245+
================================== ================================
246+
AWS::Lambda::Permissions MyFunction\ **MyTrigger**\ Permission
247+
AWS::Lambda::EventSourceMapping MyFunction\ **MyTrigger**
248+
================================== ================================
249+
223250
DynamoDb
224251
^^^^^^^^
225252

examples/2016-10-31/sqs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
transformed-cfn-template.yaml

examples/2016-10-31/sqs/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SQS Event Source Example
2+
3+
Example SAM template for processing messages on an SQS queue.
4+
5+
## Running the example
6+
7+
```bash
8+
# Replace YOUR_S3_ARTIFACTS_BUCKET
9+
YOUR_S3_ARTIFACTS_BUCKET='YOUR_S3_ARTIFACTS_BUCKET'; \
10+
aws cloudformation package --template-file template.yaml --output-template-file cfn-transformed-template.yaml --s3-bucket $YOUR_S3_ARTIFACTS_BUCKET
11+
aws cloudformation deploy --template-file ./cfn-transformed-template.yaml --stack-name example-logs-processor --capabilities CAPABILITY_IAM
12+
```
13+
14+
After your CloudFormation Stack has completed creation, push a message to the SQS queue. To see it in action, modify and run the command below:
15+
16+
```bash
17+
YOUR_SQS_QUEUE_URL=https://sqs.us-east-1.amazonaws.com/123456789012/my-queue; \
18+
aws sqs send-message --queue-url $YOUR_SQS_QUEUE_URL --message-body '{ "myMessage": "Hello SAM!" }'
19+
```

examples/2016-10-31/sqs/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
async function handler (event, context) {
2+
// TODO: Handle message...
3+
4+
console.log(event)
5+
6+
return {}
7+
}
8+
9+
module.exports.handler = handler
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: Example of processing messages on an SQS queue with Lambda
4+
Resources:
5+
MySQSQueueFunction:
6+
Type: AWS::Serverless::Function
7+
Properties:
8+
CodeUri: ./index.js
9+
Handler: index.handler
10+
Runtime: nodejs8.10
11+
Events:
12+
MySQSEvent:
13+
Type: SQS
14+
Properties:
15+
Queue: !Ref MyQueue
16+
17+
MyQueue:
18+
Type: AWS::SQS::Queue
19+
Properties:

samtranslator/model/eventsources/pull.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33

44
from samtranslator.model.lambda_ import LambdaEventSourceMapping
55
from samtranslator.translator.arn_generator import ArnGenerator
6+
from samtranslator.model.exceptions import InvalidEventException
67

78

89
class PullEventSource(ResourceMacro):
910
"""Base class for pull event sources for SAM Functions.
1011
11-
The pull events are the streams--Kinesis and DynamoDB Streams. Both of these correspond to an EventSourceMapping in
12-
Lambda, and require that the execution role be given to Kinesis or DynamoDB Streams, respectively.
12+
The pull events are Kinesis Streams, DynamoDB Streams, and SQS Queues. All of these correspond to an EventSourceMapping in
13+
Lambda, and require that the execution role be given to Kinesis Streams, DynamoDB Streams, or SQS Queues, respectively.
1314
1415
:cvar str policy_arn: The ARN of the AWS managed role policy corresponding to this pull event source
1516
"""
1617
resource_type = None
1718
property_types = {
18-
'Stream': PropertyType(True, is_str()),
19-
'BatchSize': PropertyType(False, is_type(int)),
20-
'StartingPosition': PropertyType(True, is_str())
19+
'Stream': PropertyType(False, is_str()),
20+
'Queue': PropertyType(False, is_str()),
21+
'BatchSize': PropertyType(False, is_type(int)),
22+
'StartingPosition': PropertyType(False, is_str())
2123
}
2224

2325
def get_policy_arn(self):
@@ -32,23 +34,31 @@ def to_cloudformation(self, **kwargs):
3234
:rtype: list
3335
"""
3436
function = kwargs.get('function')
35-
37+
3638
if not function:
3739
raise TypeError("Missing required keyword argument: function")
3840

3941
resources = []
4042

4143
lambda_eventsourcemapping = LambdaEventSourceMapping(self.logical_id)
4244
resources.append(lambda_eventsourcemapping)
43-
45+
4446
try:
4547
# Name will not be available for Alias resources
4648
function_name_or_arn = function.get_runtime_attr("name")
4749
except NotImplementedError:
4850
function_name_or_arn = function.get_runtime_attr("arn")
4951

52+
if not self.Stream and not self.Queue:
53+
raise InvalidEventException(
54+
self.relative_id, "No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided.")
55+
56+
if self.Stream and not self.StartingPosition:
57+
raise InvalidEventException(
58+
self.relative_id, "StartingPosition is required for Kinesis and DynamoDB.")
59+
5060
lambda_eventsourcemapping.FunctionName = function_name_or_arn
51-
lambda_eventsourcemapping.EventSourceArn = self.Stream
61+
lambda_eventsourcemapping.EventSourceArn = self.Stream or self.Queue
5262
lambda_eventsourcemapping.StartingPosition = self.StartingPosition
5363
lambda_eventsourcemapping.BatchSize = self.BatchSize
5464

@@ -82,3 +92,11 @@ class DynamoDB(PullEventSource):
8292

8393
def get_policy_arn(self):
8494
return ArnGenerator.generate_aws_managed_policy_arn('service-role/AWSLambdaDynamoDBExecutionRole')
95+
96+
97+
class SQS(PullEventSource):
98+
"""SQS Queue event source."""
99+
resource_type = 'SQS'
100+
101+
def get_policy_arn(self):
102+
return ArnGenerator.generate_aws_managed_policy_arn('service-role/AWSLambdaSQSExecutionRole')

samtranslator/model/lambda_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class LambdaEventSourceMapping(Resource):
6161
'Enabled': PropertyType(False, is_type(bool)),
6262
'EventSourceArn': PropertyType(True, is_str()),
6363
'FunctionName': PropertyType(True, is_str()),
64-
'StartingPosition': PropertyType(True, is_str())
64+
'StartingPosition': PropertyType(False, is_str())
6565
}
6666

6767
runtime_attrs = {

samtranslator/validator/sam_schema/schema.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@
381381
{
382382
"$ref": "#/definitions/AWS::Serverless::Function.KinesisEvent"
383383
},
384+
{
385+
"$ref": "#/definitions/AWS::Serverless::Function.SQSEvent"
386+
},
384387
{
385388
"$ref": "#/definitions/AWS::Serverless::Function.DynamoDBEvent"
386389
},
@@ -481,6 +484,21 @@
481484
],
482485
"type": "object"
483486
},
487+
"AWS::Serverless::Function.SQSEvent": {
488+
"additionalProperties": false,
489+
"properties": {
490+
"BatchSize": {
491+
"type": "number"
492+
},
493+
"Queue": {
494+
"type": "string"
495+
}
496+
},
497+
"required": [
498+
"Queue"
499+
],
500+
"type": "object"
501+
},
484502
"AWS::Serverless::Function.S3Event": {
485503
"additionalProperties": false,
486504
"properties": {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# File: sam.yml
2+
# Version: 0.9
3+
4+
AWSTemplateFormatVersion: '2010-09-09'
5+
Parameters: {}
6+
Resources:
7+
SQSFunction:
8+
Type: 'AWS::Serverless::Function'
9+
Properties:
10+
CodeUri: s3://sam-demo-bucket/queues.zip
11+
Handler: queue.sqs_handler
12+
Runtime: python2.7
13+
Events:
14+
MySqsQueue:
15+
Type: SQS
16+
Properties:
17+
BatchSize: 10

0 commit comments

Comments
 (0)