-
-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(serverless-example): add basic serverless SNS example
- Loading branch information
Showing
6 changed files
with
133 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.serverless |
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,28 @@ | ||
# Serverless example | ||
|
||
Fictional application running using the [Serverless](https://github.com/serverless/serverless) framework. | ||
|
||
**Message Producer** | ||
|
||
Small utility that takes a request and publishes a message to an SQS queue. | ||
|
||
**Message Consumer** | ||
|
||
Lambda function that reads from SQS and processes the data. | ||
|
||
|
||
## Deploy service | ||
|
||
Ensure you have valid AWS credentials in your environment, and then run; | ||
|
||
```sh | ||
export AWS_ACCOUNT_ID=1234xxxx5678 # Required by function at runtime | ||
serverless deploy -v | ||
``` | ||
|
||
## How we set this up | ||
|
||
```sh | ||
npm install -g serverless | ||
serverless create --template aws-nodejs --path sqs-publisher | ||
``` |
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,27 @@ | ||
'use strict'; | ||
|
||
const AWS = require('aws-sdk'); | ||
|
||
// Consumer handler, responsible for extracting message from SNS | ||
// and dealing with lambda-related things. | ||
module.exports.handler = (event, context, callback) => { | ||
console.log("Received event from SNS"); | ||
|
||
event.Records.forEach(e => { | ||
console.log("Event:", JSON.parse(e.Sns.Message)); | ||
consumeEvent(e) | ||
}); | ||
|
||
callback(null, { | ||
event | ||
}); | ||
}; | ||
|
||
// Actual consumer code, has no Lambda/AWS/Protocol specific stuff | ||
// This is the thing we test in the Consumer Pact tests | ||
const consumeEvent = (event) => { | ||
|
||
// save in dynamo or something... | ||
console.log('consuming event', event) | ||
|
||
} |
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 @@ | ||
# package directories | ||
node_modules | ||
jspm_packages | ||
|
||
# Serverless directories | ||
.serverless |
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,45 @@ | ||
'use strict'; | ||
|
||
const AWS = require('aws-sdk'); | ||
|
||
const TOPIC_ARN = process.env.TOPIC_ARN; | ||
|
||
// Handler is the Lambda and SNS specific code | ||
// The message generation logic is separated from the handler itself | ||
// in the | ||
module.exports.handler = (event, context, callback) => { | ||
const message = createEvent(event); | ||
|
||
const sns = new AWS.SNS(); | ||
|
||
const params = { | ||
Message: message.body, | ||
TopicArn: TOPIC_ARN | ||
}; | ||
|
||
sns.publish(params, (error, data) => { | ||
if (error) { | ||
callback(error); | ||
} | ||
|
||
console.log("Message successfully published to queue") | ||
callback(null, { | ||
message: 'Message successfully published to SNS topic "pact-events"', | ||
event | ||
}); | ||
}); | ||
|
||
callback(null, message); | ||
}; | ||
|
||
// Separate your producer code, from the lambda handler. | ||
// No Lambda/AWS/Protocol specific stuff in here.. | ||
const createEvent = (event) => { | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
message: 'Go Serverless v1.0! Your function executed successfully!', | ||
input: event, | ||
}), | ||
}; | ||
}; |
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,26 @@ | ||
# Serverless SNS | ||
service: pact-events | ||
|
||
provider: | ||
name: aws | ||
runtime: nodejs6.10 | ||
iamRoleStatements: | ||
- Effect: "Allow" | ||
Resource: "*" | ||
Action: | ||
- "sns:*" | ||
environment: | ||
ACCOUNT_ID: { "Ref": "AWS::AccountId" } | ||
# TOPIC_ARN: { "Ref": "SNSTopicPactevents" } | ||
TOPIC_ARN: { "Fn::Join": ["", ["arn:aws:sns:us-east-1:", { "Ref": "AWS::AccountId" }, ":pact-events"] ] } | ||
|
||
package: | ||
individually: true | ||
|
||
functions: | ||
publisher: | ||
handler: publisher/index.handler | ||
consumer: | ||
handler: consumer/index.handler | ||
events: | ||
- sns: pact-events |