-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(event-source): add SQS as Lambda event source (#451)
- Loading branch information
1 parent
3cfb56d
commit a5a3845
Showing
29 changed files
with
596 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
transformed-cfn-template.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# SQS Event Source Example | ||
|
||
Example SAM template for processing messages on an SQS queue. | ||
|
||
## Running the example | ||
|
||
```bash | ||
# Replace YOUR_S3_ARTIFACTS_BUCKET | ||
YOUR_S3_ARTIFACTS_BUCKET='YOUR_S3_ARTIFACTS_BUCKET'; \ | ||
aws cloudformation package --template-file template.yaml --output-template-file cfn-transformed-template.yaml --s3-bucket $YOUR_S3_ARTIFACTS_BUCKET | ||
aws cloudformation deploy --template-file ./cfn-transformed-template.yaml --stack-name example-logs-processor --capabilities CAPABILITY_IAM | ||
``` | ||
|
||
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: | ||
|
||
```bash | ||
YOUR_SQS_QUEUE_URL=https://sqs.us-east-1.amazonaws.com/123456789012/my-queue; \ | ||
aws sqs send-message --queue-url $YOUR_SQS_QUEUE_URL --message-body '{ "myMessage": "Hello SAM!" }' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
async function handler (event, context) { | ||
// TODO: Handle message... | ||
|
||
console.log(event) | ||
|
||
return {} | ||
} | ||
|
||
module.exports.handler = handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
Description: Example of processing messages on an SQS queue with Lambda | ||
Resources: | ||
MySQSQueueFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: ./index.js | ||
Handler: index.handler | ||
Runtime: nodejs8.10 | ||
Events: | ||
MySQSEvent: | ||
Type: SQS | ||
Properties: | ||
Queue: !Ref MyQueue | ||
|
||
MyQueue: | ||
Type: AWS::SQS::Queue | ||
Properties: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
13 changes: 13 additions & 0 deletions
13
tests/translator/input/error_missing_startingposition.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Resources: | ||
KinesisFunction: | ||
Type: 'AWS::Serverless::Function' | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/streams.zip | ||
Handler: stream.kinesis_handler | ||
Runtime: python2.7 | ||
Events: | ||
MyKinesisStream: | ||
Type: Kinesis | ||
Properties: | ||
Stream: arn:aws:kinesis:us-west-2:012345678901:stream/my-stream | ||
BatchSize: 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
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: | ||
Queue: arn:aws:sqs:us-west-2:012345678901:my-queue | ||
BatchSize: 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"Resources": { | ||
"SQSFunctionRole": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"ManagedPolicyArns": [ | ||
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole", | ||
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaSQSExecutionRole" | ||
], | ||
"AssumeRolePolicyDocument": { | ||
"Version": "2012-10-17", | ||
"Statement": [{ | ||
"Action": [ | ||
"sts:AssumeRole" | ||
], | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": [ | ||
"lambda.amazonaws.com" | ||
] | ||
} | ||
}] | ||
} | ||
} | ||
}, | ||
"SQSFunction": { | ||
"Type": "AWS::Lambda::Function", | ||
"Properties": { | ||
"Code": { | ||
"S3Bucket": "sam-demo-bucket", | ||
"S3Key": "queues.zip" | ||
}, | ||
"Handler": "queue.sqs_handler", | ||
"Role": { | ||
"Fn::GetAtt": [ | ||
"SQSFunctionRole", | ||
"Arn" | ||
] | ||
}, | ||
"Runtime": "python2.7", | ||
"Tags": [{ | ||
"Value": "SAM", | ||
"Key": "lambda:createdBy" | ||
}] | ||
} | ||
}, | ||
"SQSFunctionMySqsQueue": { | ||
"Type": "AWS::Lambda::EventSourceMapping", | ||
"Properties": { | ||
"BatchSize": 10, | ||
"EventSourceArn": "arn:aws:sqs:us-west-2:012345678901:my-queue", | ||
"FunctionName": { | ||
"Ref": "SQSFunction" | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.