-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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: Add SQS option to SNS event #1065
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8bf2077
refactor: SNS._inject_subscription method
53ningen 8ec4b96
feat: Add SQS option to SNS event
53ningen e7e2dd8
Update versions/2016-10-31.md
53ningen 2ed258f
Update docs/internals/generated_resources.rst
53ningen abd53c7
Update generated_resources.rst
keetonian f36fd1a
Added an example of how to use SqsSubscription option
53ningen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ | ||
# SNS-SQS Event Source Example | ||
|
||
Example SAM template for processing messages on an SNS-SQS. | ||
|
||
## Running the example | ||
|
||
```bash | ||
# Set YOUR_S3_ARTIFACTS_BUCKET to a bucket you own | ||
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 lambda-sns-sqs-processor --capabilities CAPABILITY_IAM | ||
``` | ||
|
||
After your CloudFormation Stack has completed creation, send a message to the SNS topic to see it in action: | ||
|
||
```bash | ||
YOUR_SNS_TOPIC_ARN=arn:aws:sns:us-east-1:[your_account_id]:[your_topic_name]; \ | ||
aws sqs send-message --target-arn $YOUR_SNS_TOPIC_ARN --message '{ "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,7 @@ | ||
async function handler (event, context) { | ||
const records = event.Records | ||
console.log(records) | ||
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,23 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
Description: Example of processing messages on an SNS-SQS with Lambda | ||
Resources: | ||
MyTopic: | ||
Type: AWS::SNS::Topic | ||
MyFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: ./index.js | ||
Handler: index.handler | ||
Runtime: nodejs8.10 | ||
Events: | ||
MySQSEvent: | ||
Type: SNS | ||
Properties: | ||
Topic: !Ref MyTopic | ||
SqsSubscription: true | ||
|
||
Outputs: | ||
MyTopic: | ||
Description: "MyTopic ARN" | ||
Value: !Ref MyTopic |
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,44 @@ | ||
from samtranslator.model import PropertyType, Resource | ||
from samtranslator.model.types import is_type, list_of | ||
from samtranslator.model.intrinsics import fnGetAtt, ref | ||
|
||
|
||
class SQSQueue(Resource): | ||
resource_type = 'AWS::SQS::Queue' | ||
property_types = { | ||
} | ||
runtime_attrs = { | ||
"queue_url": lambda self: ref(self.logical_id), | ||
"arn": lambda self: fnGetAtt(self.logical_id, "Arn"), | ||
} | ||
|
||
|
||
class SQSQueuePolicy(Resource): | ||
resource_type = 'AWS::SQS::QueuePolicy' | ||
property_types = { | ||
'PolicyDocument': PropertyType(True, is_type(dict)), | ||
'Queues': PropertyType(True, list_of(str)), | ||
} | ||
runtime_attrs = { | ||
"arn": lambda self: fnGetAtt(self.logical_id, "Arn") | ||
} | ||
|
||
|
||
class SQSQueuePolicies: | ||
@classmethod | ||
def sns_topic_send_message_role_policy(cls, topic_arn, queue_arn): | ||
document = { | ||
'Version': '2012-10-17', | ||
'Statement': [{ | ||
'Action': 'sqs:SendMessage', | ||
'Effect': 'Allow', | ||
'Principal': '*', | ||
'Resource': queue_arn, | ||
'Condition': { | ||
'ArnEquals': { | ||
'aws:SourceArn': topic_arn | ||
} | ||
} | ||
}] | ||
} | ||
return document |
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,23 @@ | ||
Resources: | ||
SaveNotificationFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/notifications.zip | ||
Handler: index.save_notification | ||
Runtime: nodejs8.10 | ||
Events: | ||
NotificationTopic: | ||
Type: SNS | ||
Properties: | ||
Topic: !Ref Notifications | ||
SqsSubscription: true | ||
FilterPolicy: | ||
store: | ||
- example_corp | ||
price_usd: | ||
- numeric: | ||
- ">=" | ||
- 100 | ||
|
||
Notifications: | ||
Type: AWS::SNS::Topic |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this correct way to create
LambdaEventSourceMapping
resource?Should I add
LambdaEventSourceMapping
and put linked_policy to IAM Role directory instead use SQS object?I think the logic to create LambdaEventSourceMapping and linked_policy is completely same as SQS pull event. So I use SQS object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like that is the correct way to add this event source mapping 👍