Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(event-source): add SQS as Lambda event source #451

Merged
merged 11 commits into from
Jun 1, 2018
5 changes: 5 additions & 0 deletions samtranslator/model/eventsources/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from samtranslator.model.lambda_ import LambdaEventSourceMapping
from samtranslator.translator.arn_generator import ArnGenerator
from samtranslator.model.exceptions import InvalidEventException


class PullEventSource(ResourceMacro):
Expand Down Expand Up @@ -48,6 +49,10 @@ def to_cloudformation(self, **kwargs):
except NotImplementedError:
function_name_or_arn = function.get_runtime_attr("arn")

if not self.Stream and not self.Queue:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you instead add a validate functino to each subclass that gets called here? Each subclass can then validate and return a customized error message. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deliberately went with this simpler approach since this is temporary code and I think the error message is clear enough even when they are combined for both cases.

raise InvalidEventException(
self.relative_id, "No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided.")

lambda_eventsourcemapping.FunctionName = function_name_or_arn
lambda_eventsourcemapping.EventSourceArn = self.Stream or self.Queue
lambda_eventsourcemapping.StartingPosition = self.StartingPosition
Expand Down
17 changes: 17 additions & 0 deletions tests/translator/input/error_missing_queue.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# File: sam.yml
# Version: 0.9

AWSTemplateFormatVersion: '2010-09-09'
Parameters: {}
Resources:
SQSFunction:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: s3://sam-demo-bucket/queues.zip
Handler: queue.sqs_handler
Runtime: python2.7
Events:
MySqsQueue:
Type: SQS
Properties:
BatchSize: 10
15 changes: 15 additions & 0 deletions tests/translator/input/error_missing_stream.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
AWSTemplateFormatVersion: '2010-09-09'
Parameters: {}
Resources:
DynamoDBFunction:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: s3://sam-demo-bucket/streams.zip
Handler: stream.ddb_handler
Runtime: python2.7
Events:
MyDDBStream:
Type: DynamoDB
Properties:
BatchSize: 200
StartingPosition: LATEST
6 changes: 6 additions & 0 deletions tests/translator/output/aws-cn/error_missing_queue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors": [{
"errorMessage": "Resource with id [SQSFunction] is invalid. Event with id [MySqsQueue] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [SQSFunction] is invalid. Event with id [MySqsQueue] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}
6 changes: 6 additions & 0 deletions tests/translator/output/aws-cn/error_missing_stream.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors": [{
"errorMessage": "Resource with id [DynamoDBFunction] is invalid. Event with id [MyDDBStream] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [DynamoDBFunction] is invalid. Event with id [MyDDBStream] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}
6 changes: 6 additions & 0 deletions tests/translator/output/aws-us-gov/error_missing_queue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors": [{
"errorMessage": "Resource with id [SQSFunction] is invalid. Event with id [MySqsQueue] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [SQSFunction] is invalid. Event with id [MySqsQueue] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors": [{
"errorMessage": "Resource with id [DynamoDBFunction] is invalid. Event with id [MyDDBStream] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [DynamoDBFunction] is invalid. Event with id [MyDDBStream] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}
6 changes: 6 additions & 0 deletions tests/translator/output/error_missing_queue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors": [{
"errorMessage": "Resource with id [SQSFunction] is invalid. Event with id [MySqsQueue] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [SQSFunction] is invalid. Event with id [MySqsQueue] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}
6 changes: 6 additions & 0 deletions tests/translator/output/error_missing_stream.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors": [{
"errorMessage": "Resource with id [DynamoDBFunction] is invalid. Event with id [MyDDBStream] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [DynamoDBFunction] is invalid. Event with id [MyDDBStream] is invalid. No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided."
}
2 changes: 2 additions & 0 deletions tests/translator/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def test_transform_success(self, testcase, partition_with_region):
'error_function_with_deployment_preference_missing_alias',
'error_function_with_invalid_deployment_preference_hook_property',
'error_invalid_logical_id',
'error_missing_queue',
'error_missing_stream',
'error_multiple_resource_errors',
'error_s3_not_in_template',
'error_table_invalid_attributetype',
Expand Down