Created
November 26, 2014 15:33
-
-
Save TheRyanBurke/be7c9325bafb19ed03cc to your computer and use it in GitHub Desktop.
Basics of ThingFabric and AWS Lambda
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
var aws = require('aws-sdk'); | |
var http = require('http'); | |
var s3 = new aws.S3({apiVersion: '2006-03-01'}); | |
exports.handler = function(event, context) { | |
var bucket = event.Records[0].s3.bucket.name; | |
var key = event.Records[0].s3.object.key; | |
s3.getObject({Bucket:bucket, Key:key}, | |
function(err,data) { | |
if (err) { | |
console.log('error getting object ' + key + ' from bucket ' + bucket + | |
'. Make sure they exist and your bucket is in the same region as this function.'); | |
context.done('error','error getting file'+err); | |
} | |
else { | |
console.log('CONTENT TYPE:',data.ContentType); | |
var inbound_payload = JSON.parse(data.Body); | |
if(inbound_payload.temperature) { | |
inbound_payload.temp = { | |
value: (inbound_payload.temperature - 32) * 5 / 9, | |
units: 'Celsius' | |
}; | |
var content = JSON.stringify(inbound_payload); | |
var options = { | |
host: 'q.thingfabric.com', | |
path: '/3/publish?domain=foo&client=bar', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': content.length, | |
'Authorization': 'Bearer token' | |
} | |
}; | |
var req = http.request(options, null); | |
req.write(content); | |
req.end(); | |
} | |
context.done(null,''); | |
} | |
} | |
); | |
}; |
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
{ | |
"Records": [ | |
{ | |
"eventVersion": "2.0", | |
"eventSource": "aws:s3", | |
"awsRegion": "us-east-1", | |
"eventTime": "1970-01-01T00:00:00.000Z", | |
"eventName": "ObjectCreated:Put", | |
"userIdentity": { | |
"principalId": "AIDAJDPLRKLG7UEXAMPLE" | |
}, | |
"requestParameters": { | |
"sourceIPAddress": "127.0.0.1" | |
}, | |
"responseElements": { | |
"x-amz-request-id": "C3D13FE58DE4C810", | |
"x-amz-id-2": "FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD" | |
}, | |
"s3": { | |
"s3SchemaVersion": "1.0", | |
"configurationId": "testConfigRule", | |
"bucket": { | |
"name": "sourcebucket", | |
"ownerIdentity": { | |
"principalId": "A3NL1KOZZKExample" | |
}, | |
"arn": "arn:aws:s3:::mybucket" | |
}, | |
"object": { | |
"key": "sourcebucket/HappyFace.jpg", | |
"size": 1024, | |
"eTag": "d41d8cd98f00b204e9800998ecf8427e" | |
} | |
} | |
} | |
] | |
} |
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
var aws = require('aws-sdk'); | |
var s3 = new aws.S3({apiVersion: '2006-03-01'}); | |
exports.handler = function(event, context) { | |
var bucket = event.Records[0].s3.bucket.name; | |
var key = event.Records[0].s3.object.key; | |
s3.getObject({Bucket:bucket, Key:key}, | |
function(err,data) { | |
if (err) { | |
console.log('error getting object ' + key + ' from bucket ' + bucket + | |
'. Make sure they exist and your bucket is in the same region as this function.'); | |
context.done('error','error getting file'+err); | |
} | |
else { | |
var inbound_payload = JSON.parse(data.Body); | |
console.log(inbound_payload); | |
context.done(null,''); | |
} | |
} | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please change the error handling code to ensure a useful error message is returned.
From:
if (err) {
console.log('error getting object ' + key + ' from bucket ' + bucket +
'. Make sure they exist and your bucket is in the same region as this function.');
}
To:
if (err) console.log(err, err.stack);
Amazingly this exact error message has spread across 50 GitHub Gists. #copypasta