Skip to content

Commit

Permalink
feat(serverless-example): add basic serverless SNS example
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Mar 31, 2018
1 parent 6eaf3d3 commit 08cd73b
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/serverless/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.serverless
28 changes: 28 additions & 0 deletions examples/serverless/README.md
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
```
27 changes: 27 additions & 0 deletions examples/serverless/consumer/index.js
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)

}
6 changes: 6 additions & 0 deletions examples/serverless/publisher/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless
45 changes: 45 additions & 0 deletions examples/serverless/publisher/index.js
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,
}),
};
};
26 changes: 26 additions & 0 deletions examples/serverless/serverless.yml
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

0 comments on commit 08cd73b

Please sign in to comment.