├── .gitignore ├── 1-sqs ├── code │ ├── consumer.js │ └── publisher.js └── template.yaml ├── 2-sns ├── code │ ├── consumer.js │ └── publisher.js └── template.yaml ├── 3-eventbridge ├── code │ ├── consumer.js │ └── publisher.js └── template.yaml ├── 4-sns-sqs ├── template-basic.yaml └── template-filter-policy.yaml ├── 5-eventbridge-sns ├── event.json └── template.yaml ├── 6-eventbridge-sqs ├── code │ └── consumer.js ├── event.json └── template.yaml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/osx,node,linux,windows 3 | 4 | ### Linux ### 5 | *~ 6 | 7 | # temporary files which can be created if a process still has a handle open of a deleted file 8 | .fuse_hidden* 9 | 10 | # KDE directory preferences 11 | .directory 12 | 13 | # Linux trash folder which might appear on any partition or disk 14 | .Trash-* 15 | 16 | # .nfs files are created when an open file is removed but is still being accessed 17 | .nfs* 18 | 19 | ### Node ### 20 | # Logs 21 | logs 22 | *.log 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # Runtime data 28 | pids 29 | *.pid 30 | *.seed 31 | *.pid.lock 32 | 33 | # Directory for instrumented libs generated by jscoverage/JSCover 34 | lib-cov 35 | 36 | # Coverage directory used by tools like istanbul 37 | coverage 38 | 39 | # nyc test coverage 40 | .nyc_output 41 | 42 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 43 | .grunt 44 | 45 | # Bower dependency directory (https://bower.io/) 46 | bower_components 47 | 48 | # node-waf configuration 49 | .lock-wscript 50 | 51 | # Compiled binary addons (http://nodejs.org/api/addons.html) 52 | build/Release 53 | 54 | # Dependency directories 55 | node_modules/ 56 | jspm_packages/ 57 | 58 | # Typescript v1 declaration files 59 | typings/ 60 | 61 | # Optional npm cache directory 62 | .npm 63 | 64 | # Optional eslint cache 65 | .eslintcache 66 | 67 | # Optional REPL history 68 | .node_repl_history 69 | 70 | # Output of 'npm pack' 71 | *.tgz 72 | 73 | # Yarn Integrity file 74 | .yarn-integrity 75 | 76 | # dotenv environment variables file 77 | .env 78 | 79 | 80 | ### OSX ### 81 | *.DS_Store 82 | .AppleDouble 83 | .LSOverride 84 | 85 | # Icon must end with two \r 86 | Icon 87 | 88 | # Thumbnails 89 | ._* 90 | 91 | # Files that might appear in the root of a volume 92 | .DocumentRevisions-V100 93 | .fseventsd 94 | .Spotlight-V100 95 | .TemporaryItems 96 | .Trashes 97 | .VolumeIcon.icns 98 | .com.apple.timemachine.donotpresent 99 | 100 | # Directories potentially created on remote AFP share 101 | .AppleDB 102 | .AppleDesktop 103 | Network Trash Folder 104 | Temporary Items 105 | .apdisk 106 | 107 | ### Windows ### 108 | # Windows thumbnail cache files 109 | Thumbs.db 110 | ehthumbs.db 111 | ehthumbs_vista.db 112 | 113 | # Folder config file 114 | Desktop.ini 115 | 116 | # Recycle Bin used on file shares 117 | $RECYCLE.BIN/ 118 | 119 | # Windows Installer files 120 | *.cab 121 | *.msi 122 | *.msm 123 | *.msp 124 | 125 | # Windows shortcuts 126 | *.lnk 127 | 128 | 129 | # End of https://www.gitignore.io/api/osx,node,linux,windows 130 | 131 | # Editor directories and files 132 | .idea 133 | .vscode 134 | *.suo 135 | *.ntvs* 136 | *.njsproj 137 | *.sln 138 | *.sw? 139 | 140 | #AWS/SAM 141 | .aws-sam 142 | packaged.yaml 143 | samconfig.toml 144 | 145 | -------------------------------------------------------------------------------- /1-sqs/code/consumer.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 4 | software and associated documentation files (the "Software"), to deal in the Software 5 | without restriction, including without limitation the rights to use, copy, modify, 6 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 7 | permit persons to whom the Software is furnished to do so. 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 9 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 10 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 11 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 12 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 13 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 14 | */ 15 | 16 | 'use strict' 17 | 18 | // The Lambda handler 19 | exports.handler = async (event) => { 20 | console.log(JSON.stringify(event, 2, null)) 21 | } 22 | 23 | -------------------------------------------------------------------------------- /1-sqs/code/publisher.js: -------------------------------------------------------------------------------- 1 | /*! Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | * SPDX-License-Identifier: MIT-0 3 | */ 4 | 5 | 'use strict' 6 | 7 | const AWS = require('aws-sdk') 8 | AWS.config.region = process.env.AWS_REGION 9 | const sqs = new AWS.SQS({apiVersion: '2012-11-05'}) 10 | 11 | // The Lambda handler 12 | exports.handler = async (event) => { 13 | // Params object for SQS 14 | const params = { 15 | MessageBody: `Message at ${Date()}`, 16 | QueueUrl: process.env.SQSqueueName 17 | } 18 | 19 | // Send to SQS 20 | const result = await sqs.sendMessage(params).promise() 21 | console.log(result) 22 | } 23 | 24 | -------------------------------------------------------------------------------- /1-sqs/template.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | Description: messaging-sqs-example 4 | 5 | Resources: 6 | MySqsQueue: 7 | Type: AWS::SQS::Queue 8 | 9 | QueuePublisherFunction: 10 | Type: AWS::Serverless::Function 11 | Properties: 12 | CodeUri: code/ 13 | Handler: publisher.handler 14 | Runtime: nodejs12.x 15 | Timeout: 3 16 | MemorySize: 128 17 | Environment: 18 | Variables: 19 | SQSqueueName: !Ref MySqsQueue 20 | Policies: 21 | ## Read more about SAM Policy templates at: 22 | ## https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html 23 | - SQSSendMessagePolicy: 24 | QueueName: !GetAtt MySqsQueue.QueueName 25 | 26 | QueueConsumerFunction: 27 | Type: AWS::Serverless::Function 28 | Properties: 29 | CodeUri: code/ 30 | Handler: consumer.handler 31 | Runtime: nodejs12.x 32 | Timeout: 3 33 | MemorySize: 128 34 | # ReservedConcurrentExecutions: 1 35 | Policies: 36 | - SQSPollerPolicy: 37 | QueueName: !GetAtt MySqsQueue.QueueName 38 | Events: 39 | MySQSEvent: 40 | Type: SQS 41 | Properties: 42 | Queue: !GetAtt MySqsQueue.Arn 43 | BatchSize: 10 44 | 45 | Outputs: 46 | QueuePublisherFunction: 47 | Description: QueuePublisherFunction function name 48 | Value: !Ref QueuePublisherFunction 49 | QueueConsumerFunction: 50 | Description: QueueConsumerFunction function name 51 | Value: !Ref QueueConsumerFunction -------------------------------------------------------------------------------- /2-sns/code/consumer.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 4 | software and associated documentation files (the "Software"), to deal in the Software 5 | without restriction, including without limitation the rights to use, copy, modify, 6 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 7 | permit persons to whom the Software is furnished to do so. 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 9 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 10 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 11 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 12 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 13 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 14 | */ 15 | 16 | 'use strict' 17 | 18 | // The Lambda handler 19 | exports.handler = async (event) => { 20 | console.log(JSON.stringify(event, 2, null)) 21 | } 22 | 23 | -------------------------------------------------------------------------------- /2-sns/code/publisher.js: -------------------------------------------------------------------------------- 1 | /*! Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | * SPDX-License-Identifier: MIT-0 3 | */ 4 | 5 | 'use strict' 6 | 7 | const AWS = require('aws-sdk') 8 | AWS.config.region = process.env.AWS_REGION 9 | const sns = new AWS.SNS({apiVersion: '2012-11-05'}) 10 | 11 | // The Lambda handler 12 | exports.handler = async (event) => { 13 | // Params object for SNS 14 | const params = { 15 | Message: `Message at ${Date()}`, 16 | Subject: 'New message from publisher', 17 | TopicArn: process.env.SNStopic 18 | } 19 | 20 | // Send to SNS 21 | const result = await sns.publish(params).promise() 22 | console.log(result) 23 | } 24 | 25 | -------------------------------------------------------------------------------- /2-sns/template.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | Description: messaging-sns-example 4 | 5 | Resources: 6 | MySnsTopic: 7 | Type: AWS::SNS::Topic 8 | Properties: 9 | Subscription: 10 | - Protocol: lambda 11 | Endpoint: !GetAtt TopicConsumerFunction1.Arn 12 | - Protocol: lambda 13 | Endpoint: !GetAtt TopicConsumerFunction2.Arn 14 | 15 | TopicConsumerFunction1Permission: 16 | Type: 'AWS::Lambda::Permission' 17 | Properties: 18 | Action: 'lambda:InvokeFunction' 19 | FunctionName: !Ref TopicConsumerFunction1 20 | Principal: sns.amazonaws.com 21 | 22 | TopicConsumerFunction2Permission: 23 | Type: 'AWS::Lambda::Permission' 24 | Properties: 25 | Action: 'lambda:InvokeFunction' 26 | FunctionName: !Ref TopicConsumerFunction2 27 | Principal: sns.amazonaws.com 28 | 29 | TopicPublisherFunction: 30 | Type: AWS::Serverless::Function 31 | Properties: 32 | CodeUri: code/ 33 | Handler: publisher.handler 34 | Runtime: nodejs12.x 35 | Timeout: 3 36 | MemorySize: 128 37 | Environment: 38 | Variables: 39 | SNStopic: !Ref MySnsTopic 40 | Policies: 41 | ## Read more about SAM Policy templates at: 42 | ## https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html 43 | - SNSPublishMessagePolicy: 44 | TopicName: !GetAtt MySnsTopic.TopicName 45 | 46 | TopicConsumerFunction1: 47 | Type: AWS::Serverless::Function 48 | Properties: 49 | CodeUri: code/ 50 | Handler: consumer.handler 51 | Runtime: nodejs12.x 52 | Timeout: 3 53 | MemorySize: 128 54 | 55 | TopicConsumerFunction2: 56 | Type: AWS::Serverless::Function 57 | Properties: 58 | CodeUri: code/ 59 | Handler: consumer.handler 60 | Runtime: nodejs12.x 61 | Timeout: 3 62 | MemorySize: 128 63 | 64 | Outputs: 65 | TopicPublisherFunction: 66 | Description: TopicPublisherFunction function name 67 | Value: !Ref TopicPublisherFunction 68 | TopicConsumerFunction1: 69 | Description: TopicConsumerFunction1 function name 70 | Value: !Ref TopicConsumerFunction1 71 | TopicConsumerFunction2: 72 | Description: TopicConsumerFunction2 function name 73 | Value: !Ref TopicConsumerFunction2 74 | -------------------------------------------------------------------------------- /3-eventbridge/code/consumer.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 4 | software and associated documentation files (the "Software"), to deal in the Software 5 | without restriction, including without limitation the rights to use, copy, modify, 6 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 7 | permit persons to whom the Software is furnished to do so. 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 9 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 10 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 11 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 12 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 13 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 14 | */ 15 | 16 | 'use strict' 17 | 18 | // The Lambda handler 19 | exports.handler = async (event) => { 20 | console.log(JSON.stringify(event, 2, null)) 21 | } 22 | 23 | -------------------------------------------------------------------------------- /3-eventbridge/code/publisher.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 4 | software and associated documentation files (the "Software"), to deal in the Software 5 | without restriction, including without limitation the rights to use, copy, modify, 6 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 7 | permit persons to whom the Software is furnished to do so. 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 9 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 10 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 11 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 12 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 13 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 14 | */ 15 | 16 | 'use strict' 17 | 18 | const AWS = require('aws-sdk') 19 | AWS.config.update({region: 'us-east-1'}) 20 | const eventbridge = new AWS.EventBridge() 21 | 22 | exports.handler = async (event) => { 23 | const params = { 24 | Entries: [ 25 | { 26 | Detail: JSON.stringify({ 27 | "message": "Hello from publisher", 28 | "state": "new" 29 | }), 30 | DetailType: 'Message', 31 | EventBusName: 'default', 32 | Source: 'demo.event', 33 | Time: new Date 34 | } 35 | ] 36 | } 37 | const result = await eventbridge.putEvents(params).promise() 38 | console.log(result) 39 | } 40 | -------------------------------------------------------------------------------- /3-eventbridge/template.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | Description: messaging-eventbridge-example 4 | 5 | Resources: 6 | EventPublisherFunction: 7 | Type: AWS::Serverless::Function 8 | Properties: 9 | CodeUri: code/ 10 | Handler: publisher.handler 11 | Timeout: 3 12 | Runtime: nodejs12.x 13 | Policies: 14 | - Statement: 15 | - Effect: Allow 16 | Resource: '*' 17 | Action: 18 | - events:PutEvents 19 | 20 | EventConsumerFunction: 21 | Type: AWS::Serverless::Function 22 | Properties: 23 | CodeUri: code/ 24 | Handler: consumer.handler 25 | Timeout: 3 26 | Runtime: nodejs12.x 27 | 28 | EventRule: 29 | Type: AWS::Events::Rule 30 | Properties: 31 | Description: "EventRule" 32 | EventPattern: 33 | source: 34 | - "demo.event" 35 | detail: 36 | state: 37 | - "new" 38 | State: "ENABLED" 39 | Targets: 40 | - Arn: !GetAtt EventConsumerFunction.Arn 41 | Id: "ConsumerTarget" 42 | 43 | PermissionForEventsToInvokeLambda: 44 | Type: AWS::Lambda::Permission 45 | Properties: 46 | FunctionName: 47 | Ref: "EventConsumerFunction" 48 | Action: "lambda:InvokeFunction" 49 | Principal: "events.amazonaws.com" 50 | SourceArn: !GetAtt EventRule.Arn 51 | 52 | 53 | -------------------------------------------------------------------------------- /4-sns-sqs/template-basic.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | Description: messaging-sns-to-sqs 4 | 5 | Resources: 6 | MySqsQueue: 7 | Type: AWS::SQS::Queue 8 | 9 | MySnsTopic: 10 | Type: AWS::SNS::Topic 11 | Properties: 12 | Subscription: 13 | - Protocol: sqs 14 | Endpoint: !GetAtt MySqsQueue.Arn 15 | FilterPolicy: 16 | type: 17 | - orders 18 | - payments 19 | 20 | # Policy allows SNS to publish to this SQS queue 21 | SnsToSqsPolicy: 22 | Type: AWS::SQS::QueuePolicy 23 | Properties: 24 | PolicyDocument: 25 | Version: "2012-10-17" 26 | Statement: 27 | - Sid: "Allow SNS publish to SQS" 28 | Effect: Allow 29 | Principal: "*" 30 | Resource: !GetAtt MySqsQueue.Arn 31 | Action: SQS:SendMessage 32 | Condition: 33 | ArnEquals: 34 | aws:SourceArn: !Ref MySnsTopic 35 | Queues: 36 | - Ref: MySqsQueue 37 | 38 | Outputs: 39 | MySqsQueueName: 40 | Description: Queue name 41 | Value: !GetAtt MySqsQueue.QueueName 42 | MySnsTopicName: 43 | Description: Topic name 44 | Value: !GetAtt MySnsTopic.TopicName -------------------------------------------------------------------------------- /4-sns-sqs/template-filter-policy.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | Description: messaging-sns-to-sqs-with-filtering 4 | 5 | Resources: 6 | MySqsQueue: 7 | Type: AWS::SQS::Queue 8 | 9 | MySnsTopic: 10 | Type: AWS::SNS::Topic 11 | 12 | # Use AWS::SNS::Subscription resource to apply filter policy 13 | QueueSubcription: 14 | Type: 'AWS::SNS::Subscription' 15 | Properties: 16 | TopicArn: !Ref MySnsTopic 17 | Endpoint: !GetAtt MySqsQueue.Arn 18 | Protocol: sqs 19 | FilterPolicy: 20 | type: 21 | - orders 22 | - payments 23 | RawMessageDelivery: 'true' 24 | 25 | # Policy allows SNS to publish to this SQS queue 26 | SnsToSqsPolicy: 27 | Type: AWS::SQS::QueuePolicy 28 | Properties: 29 | PolicyDocument: 30 | Version: "2012-10-17" 31 | Statement: 32 | - Sid: "Allow SNS publish to SQS" 33 | Effect: Allow 34 | Principal: "*" 35 | Resource: !GetAtt MySqsQueue.Arn 36 | Action: SQS:SendMessage 37 | Condition: 38 | ArnEquals: 39 | aws:SourceArn: !Ref MySnsTopic 40 | Queues: 41 | - Ref: MySqsQueue 42 | 43 | Outputs: 44 | MySqsQueueName: 45 | Description: Queue name 46 | Value: !GetAtt MySqsQueue.QueueName 47 | MySnsTopicName: 48 | Description: Topic name 49 | Value: !GetAtt MySnsTopic.TopicName -------------------------------------------------------------------------------- /5-eventbridge-sns/event.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "Source": "demo.cli", 4 | "DetailType": "event", 5 | "Detail": "{ \"key1\": \"value1\", \"key2\": \"value2\" }" 6 | } 7 | ] -------------------------------------------------------------------------------- /5-eventbridge-sns/template.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | Description: messaging-eventbridge-sns-example 4 | 5 | Resources: 6 | MySnsTopic: 7 | Type: AWS::SNS::Topic 8 | 9 | EventRule: 10 | Type: AWS::Events::Rule 11 | Properties: 12 | Description: "EventRule" 13 | EventPattern: 14 | account: 15 | - !Sub '${AWS::AccountId}' 16 | source: 17 | - "demo.cli" 18 | Targets: 19 | - Arn: !Ref MySnsTopic 20 | Id: "SNStopic" 21 | 22 | # Allow EventBridge to invoke SNS 23 | EventBridgeToToSnsPolicy: 24 | Type: AWS::SNS::TopicPolicy 25 | Properties: 26 | PolicyDocument: 27 | Statement: 28 | - Effect: Allow 29 | Principal: 30 | Service: events.amazonaws.com 31 | Action: sns:Publish 32 | Resource: !Ref MySnsTopic 33 | Topics: 34 | - !Ref MySnsTopic 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /6-eventbridge-sqs/code/consumer.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 4 | software and associated documentation files (the "Software"), to deal in the Software 5 | without restriction, including without limitation the rights to use, copy, modify, 6 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 7 | permit persons to whom the Software is furnished to do so. 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 9 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 10 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 11 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 12 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 13 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 14 | */ 15 | 16 | 'use strict' 17 | 18 | // The Lambda handler 19 | exports.handler = async (event) => { 20 | console.log(JSON.stringify(event, 2, null)) 21 | } 22 | 23 | -------------------------------------------------------------------------------- /6-eventbridge-sqs/event.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "Source": "demo.cli", 4 | "DetailType": "event", 5 | "Detail": "{ \"key1\": \"value1\", \"key2\": \"value2\" }" 6 | } 7 | ] -------------------------------------------------------------------------------- /6-eventbridge-sqs/template.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | Description: messaging-eventbridge-sqs-example 4 | 5 | Resources: 6 | MySqsQueue: 7 | Type: AWS::SQS::Queue 8 | 9 | QueueConsumerFunction: 10 | Type: AWS::Serverless::Function 11 | Properties: 12 | CodeUri: code/ 13 | Handler: consumer.handler 14 | Runtime: nodejs12.x 15 | Timeout: 3 16 | MemorySize: 128 17 | # ReservedConcurrentExecutions: 1 18 | Policies: 19 | - SQSPollerPolicy: 20 | QueueName: !GetAtt MySqsQueue.QueueName 21 | Events: 22 | MySQSEvent: 23 | Type: SQS 24 | Properties: 25 | Queue: !GetAtt MySqsQueue.Arn 26 | BatchSize: 10 27 | 28 | EventRule: 29 | Type: AWS::Events::Rule 30 | Properties: 31 | Description: "EventRule" 32 | EventPattern: 33 | account: 34 | - !Sub '${AWS::AccountId}' 35 | source: 36 | - "demo.cli" 37 | Targets: 38 | - Arn: !GetAtt MySqsQueue.Arn 39 | Id: "SQSqueue" 40 | 41 | # Allow EventBridge to invoke SQS 42 | EventBridgeToToSqsPolicy: 43 | Type: AWS::SQS::QueuePolicy 44 | Properties: 45 | PolicyDocument: 46 | Statement: 47 | - Effect: Allow 48 | Principal: 49 | Service: events.amazonaws.com 50 | Action: SQS:SendMessage 51 | Resource: !GetAtt MySqsQueue.Arn 52 | Queues: 53 | - Ref: MySqsQueue 54 | 55 | Outputs: 56 | QueueConsumerFunction: 57 | Description: QueueConsumerFunction function name 58 | Value: !Ref QueueConsumerFunction 59 | 60 | 61 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 3 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 4 | opensource-codeofconduct@amazon.com with any additional questions or comments. 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *master* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | 61 | We may ask you to sign a [Contributor License Agreement (CLA)](http://en.wikipedia.org/wiki/Contributor_License_Agreement) for larger changes. 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 10 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 11 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 12 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 13 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 14 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Messaging services in serverless applications 2 | 3 | The examples in this repo are used in related messaging blog posts. You can read the blog posts at: 4 | - https://aws.amazon.com/blogs/compute/choosing-between-messaging-services-for-serverless-applications/ 5 | - https://aws.amazon.com/blogs/compute/building-resilient-no-code-serverless-patterns-by-combining-messaging-services/ 6 | 7 | Important: these applications use various AWS services and there are costs associated with these services after the Free Tier usage. Please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in these examples. 8 | 9 | ```bash 10 | . 11 | ├── README.MD <-- This instructions file 12 | ├── 1-sqs <-- Integrates an SQS queue with a Lambda function 13 | └── 2-sns <-- Integrates an SNS topic with a Lambda function 14 | └── 3-eventbridge <-- Creates an EventBridge rule with a Lambda function as a target 15 | └── 4-sns-sqs <-- Subscribes an SQS queue to an SNS topic 16 | └── 5-eventbridge-sns <-- Creates an EventBridge rule with an SNS topic as a target 17 | └── 6-eventbridge-sqs <-- Creates an EventBridge rule with an SQS queue as a target 18 | ``` 19 | 20 | ## Requirements 21 | 22 | * AWS CLI already configured with Administrator permission 23 | * [NodeJS 12.x installed](https://nodejs.org/en/download/) 24 | 25 | ## Installation Instructions 26 | 27 | 1. [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and login. 28 | 29 | 1. Clone the repo onto your local development machine using `git clone`. 30 | 31 | 1. In each example's directory, from the command line, run: 32 | ``` 33 | sam deploy --guided 34 | ``` 35 | Follow the prompts in the deploy process to set the stack name and AWS Region. 36 | 37 | ## Testing 38 | 39 | The SAM deployment provides key resource names in the output. 40 | 41 | Invoke a Lambda function from the CLI using: 42 | ``` 43 | aws lambda invoke --function-name << your-function-name >> response.json 44 | ``` 45 | Tail the function's CloudWatch logs using: 46 | ``` 47 | sam logs -n sqs-example-QueueConsumerFunction-M1GVFCEH030D --tail 48 | ``` 49 | With a test event in a local `event.json` file, send the event to the Amazon EventBridge using: 50 | ``` 51 | aws events put-events --entries file://event.json 52 | ``` 53 | 54 | ## Questions? 55 | 56 | Please raise an issue on this repo. 57 | 58 | ============================================== 59 | 60 | Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 61 | 62 | SPDX-License-Identifier: MIT-0 63 | --------------------------------------------------------------------------------