Skip to content

Commit 5299a2d

Browse files
committed
sns simple example added
1 parent 3769cfa commit 5299a2d

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ Make sure :
105105
This file you can obtain on Getting Started page inside your IronWorker project (https://hud.iron.io/dashboard).
106106
* you have specified a valid docker repository name in DOCKER_REPO_NAME variable of _./deploy.env_ file.
107107

108+
## Examples
109+
110+
* [Amazon Simple Notification Service](./examples/sns/README.md)
111+
108112
## Notes
109113
### Windows Users
110114

examples/sns/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Amazon SNS can be used as a mediator to bypass events from other Amazon Web Services to iron-lambda ([Amazon Simple Storage Service (S3)](http://docs.aws.amazon.com/AmazonS3/latest/dev/ways-to-add-notification-config-to-bucket.html) for example)
2+
3+
To use Amazon SNS as event source to you lambda function you should the following steps:
4+
5+
1. Upload your lambda function with [`iron-lambda deploy`](../../README.md)
6+
2. On existing SNS topic Create New Subsription with the parameters:
7+
* Protocol: HTTPS
8+
* Endpoint: The webhook url for the lambda function (the url is located in _Webhook URL_ field on _Code_ page of the worker created for your lambda function)
9+
3. Request the confirmation of the new subscription. The confirmation is sent to your lambda function. You should be interested in _SubscribeURL_ in the payload. The payload can be viewed on on _Tasks_ page of the worker.
10+
4. Confirm subscription by either following the _SubscribeURL_ or entring this URL on _Confirm subscription_ dialog in AWS Management Console.
11+
5. From this point every message published to topic will be sent to the lambda function in `Message` property of `event` parameter. Optionally, you could parse the message to json (see [example](./lambda.js)).
12+
13+
**Note**: For security reason it's desured to validate the incoming message signature. More information can be obtained [here](http://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.verify.signature.html)

examples/sns/lambda.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
exports.handler = function(event, context) {
2+
//structure of SNS event see here: http://docs.aws.amazon.com/sns/latest/dg/json-formats.html#http-notification-json
3+
console.log("event.Type", event.Type);
4+
5+
if (event.Type == 'Notification') {
6+
var message = JSON.parse(event.Message);
7+
8+
console.log( "message", message);
9+
10+
context.done();
11+
}
12+
else if (event.Type == 'SubscriptionConfirmation'){
13+
console.log( "confimation URL", event.SubscribeURL);
14+
15+
context.done();
16+
}
17+
else if (event.Type == 'UnsubscribeConfirmation'){
18+
19+
context.done();
20+
}
21+
else {
22+
console.log("unknown event.Type", event.Type);
23+
24+
context.fail();
25+
}
26+
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"url" : "https://github.com/vlopatkin/iron-lambda"
1919
},
2020
"files": [
21+
"examples",
2122
"templates",
2223
"app.js",
2324
"package.json",

0 commit comments

Comments
 (0)