Skip to content

Commit f48e450

Browse files
author
Anthony Nowell
committed
Add lambda blueprint without KMS
1 parent c68c5e4 commit f48e450

3 files changed

Lines changed: 128 additions & 14 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"lambda": {
3+
"FunctionName": "algorithmia",
4+
"Handler": "index.handler",
5+
"Runtime": "nodejs",
6+
"Description": "Run any algorithm in the Algorithmia marketplace.",
7+
"MemorySize": 256,
8+
"Timeout": 60
9+
},
10+
"eventSources": {
11+
"s3": [{}]
12+
},
13+
"version": "1.0.2",
14+
"build": "https://s3.amazonaws.com/algorithmia-lambda/algorgthmia-lambda-nodejs-no-kms.zip",
15+
"license": "MIT",
16+
"tags": [
17+
"nodejs",
18+
"algorithms",
19+
"backend",
20+
"api",
21+
"mobile"
22+
],
23+
"authors": [
24+
"Algorithmia <support@algorithmia.com> (https://www.algorithmia.com)"
25+
],
26+
"private": false
27+
}

contrib/lambda/index-no-kms.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Algorithmia Lambda Function
3+
*
4+
* Calls any algorithm in the Algorithmia marketplace
5+
* Get an API key and free credits by creating an account at algorithmia.com
6+
* For more documentation see: algorithmia.com/developers/clients/lambda
7+
*/
8+
9+
var algorithmia = require("algorithmia");
10+
var AWS = require('aws-sdk');
11+
var apiKey;
12+
13+
// Provide your Algorithmia API key
14+
apiKey= "<YourApiKey>";
15+
16+
/*
17+
* This is the lambda entrypoint (no modification necessary)
18+
* it ensures apiKey is set
19+
* and then calls processEvent with the same event and context
20+
*/
21+
exports.handler = function(event, context) {
22+
if(apiKey) {
23+
processEvent(event, context);
24+
} else {
25+
context.fail("API Key has not been set.")
26+
}
27+
};
28+
29+
30+
/*
31+
* Configure your function to interact
32+
*/
33+
var processEvent = function(event, context) {
34+
/*
35+
* Step 1: Set the algorithm you want to call
36+
* This may be any algorithm in the Algorithmia marketplace
37+
*/
38+
var algorithm = "algo://demo/Hello"; // algorithmia.com/algorithms/demo/Hello
39+
40+
/*
41+
* Step 2: Use your event source to set inputData according to the algorithm's input format
42+
* This demo example uses the S3 Object's name as inputData
43+
*/
44+
var inputData = event.Records[0].s3.object.key; // Example for algo://demo/Hello
45+
46+
/* Advanced example:
47+
* Create 200x50 thumbnails for S3 file events using algo://opencv/SmartThumbnail
48+
* Algorithm expects input as [URL, WIDTH, HEIGHT]
49+
* Output is a base64 encoding of the resulting PNG thumbnail
50+
*
51+
* var algorithm = "algo://opencv/SmartThumbnail"
52+
* var s3 = new AWS.S3();
53+
* var bucket = event.Records[0].s3.bucket.name;
54+
* var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " ")) ;
55+
* var params = {Bucket: bucket, Key: key};
56+
* var signedUrl = s3.getSignedUrl('getObject', params);
57+
* var inputData = [signedUrl, 200, 50];
58+
*/
59+
60+
// Run the algorithm
61+
var client = algorithmia(apiKey);
62+
client.algo(algorithm).pipe(inputData).then(function(output) {
63+
if(output.error) {
64+
console.log("Error: " + output.error.message);
65+
context.fail(output.error.message);
66+
} else {
67+
/*
68+
* Step 3: Process the algorithm output here
69+
* This demo example prints and succeeds with the algorithm result
70+
*/
71+
console.log(output);
72+
context.succeed(output.result);
73+
}
74+
});
75+
}

make-lambda-build

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,33 @@ set -e
44

55
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
66
TMP=$DIR/tmp/lambda
7-
rm -rf "$TMP"
87

9-
# Create the blueprint structure
10-
mkdir -p $TMP
11-
cp contrib/lambda/index.js $TMP/
12-
cd $TMP
13-
npm install $DIR --prefix .
8+
function make_blueprint {
9+
if [[ -n $1 ]]; then
10+
local suffix="-$1"
11+
fi
12+
rm -rf "$TMP"
1413

15-
# Tweak User-Agent in the client
16-
sed -i -e 's/\(User-Agent.*\)algorithmia-nodejs/\1 algorithmia-lambda/' $TMP/node_modules/algorithmia/lib/algorithmia.js
14+
# Create the blueprint structure
15+
mkdir -p $TMP
16+
cp contrib/lambda/index${suffix}.js $TMP/
17+
cd $TMP
18+
npm install $DIR --prefix .
1719

18-
# Zip up the blueprint
19-
zip -r algorgthmia-lambda-nodejs.zip .
20+
# Tweak User-Agent in the client
21+
sed -i -e 's/\(User-Agent.*\)algorithmia-nodejs/\1 algorithmia-lambda/' $TMP/node_modules/algorithmia/lib/algorithmia.js
22+
23+
# Zip up the blueprint
24+
zip -r algorithmia-lambda-nodejs${suffix}.zip .
25+
mv algorithmia-lambda-nodejs${suffix}.zip ..
26+
27+
echo "-----------------------------"
28+
echo "Finished packaging blueprint."
29+
echo "Blueprint config: $DIR/contrib/lambda/algorithmia-lambda-nodejs${suffix}.json"
30+
echo "Blueprint zip: algorithmia-lambda-nodejs${suffix}.zip"
31+
cd $DIR
32+
}
33+
34+
make_blueprint
35+
make_blueprint no-kms
2036

21-
echo "-----------------------------"
22-
echo "Finished packaging blueprint."
23-
echo "Blueprint config: $DIR/contrib/lambda/algorithmia-lambda-nodejs.json"
24-
echo "Blueprint zip: $TMP/algorithmia-lambda-nodejs.zip"

0 commit comments

Comments
 (0)