Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Laeeth committed Apr 21, 2017
1 parent eab5d25 commit c6d5c38
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
88 changes: 88 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
Document source: <http://awslambda-d.readthedocs.org/>

This post is heavily inspired by Ruben Fonesca's work on writing AWS Lambda Functions in Go. Please see his post here:
<http://blog.0x82.com/2014/11/24/aws-lambda-functions-in-go/>

AWS Lambda is a 'compute service that runs your code in response to events and automatically manages the new compute resources for you, making it easy to build applications that respond quickly to new information'.

There are many uses for such, and in my own work I am more interested in the ability to use them to process bursts of data relating to news and financial markets than the typical web applications. So this post is written more with those concerns in mind.

Current supported runtimes (at least when I wrote the code, some months back) were Javascript (node.js) and Java, but I wanted to be able to write scripts in D, since that's what I use for my existing work.

First compile your D script on a linux machine:

module doit;
import std.stdio;

void main(string[] args)
{
writefln("Hello world!");
foreach(arg;args)
writefln("you gave me: %s",arg);
writefln("==");
writefln("bye");
}

`dmd doit.d`

Then we need to create a javascript (node.js) module to fork to the binary.

'use strict';

var child_process = require('child_process');

// Set the PATH and LD_LIBRARY_PATH environment variables.
process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'] + '/';
process.env['LD_LIBRARY_PATH'] = process.env['LAMBDA_TASK_ROOT'] + '/';

exports.handler = function(event, context) {
var proc = child_process.exec('./doit ' + JSON.stringify(event) ,function(code,stdout,stderr) {
console.log(stdout);
context.succeed(stdout);
});
}


Since dmd links phobos dynamically on linux, and phobos/druntime aren't installed on the AWS lambda server, we will need to upload these to the servers and tell the application where to find them. (I should really have appended to LD_LIBRARY_PATH as I did with PATH).

Now one can follow the regular instructions for AWS Lambda: create a .zip file with the D binary, the JS file, and the following libraries (update version numbers as appropriate):

libcrypto.so.1.0.0
libphobos2.so.0.74
libevent-2.0.so.5
libssl.so.1.0.0


To create the lambda, use the aws command line tool as follows, changing region, function-name, zip-file name, role and handler as appropriate:

aws lambda create-function \
--region us-west-2 \
--function-name lambda \
--zip-file fileb://./lambda.zip \
--role arn:aws:iam::iam_number_here:role/lambda_basic_execution \
--handler lambda.handler \
--runtime nodejs


(see create.sh in the github repository - you will need to modify at least the ARN for the role. Ie change:

--role arn:aws:iam::insertiamnumberhere:role/lambda_basic_execution

)


To invoke the lambda:

aws lambda invoke \
--invocation-type RequestResponse \
--function-name lambda \
--region us-west-2 \
--log-type Tail \
--payload '{"key1":"value1", "key2":"value2", "key3":"value3"}' \
outputfile.txt

And to list lambda functions on server:

aws lambda list-functions \
--max-items 10

12 changes: 11 additions & 1 deletion index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Document source: <http://awslambda-d.readthedocs.org/>

This post is heavily inspired by Ruben Fonesca's work on writing AWS Lambda Functions in Go. Please see his post here:
<http://blog.0x82.com/2014/11/24/aws-lambda-functions-in-go/>

Expand Down Expand Up @@ -46,7 +48,7 @@ Since dmd links phobos dynamically on linux, and phobos/druntime aren't installe
Now one can follow the regular instructions for AWS Lambda: create a .zip file with the D binary, the JS file, and the following libraries (update version numbers as appropriate):

libcrypto.so.1.0.0
libphobos2.so.0.67
libphobos2.so.0.74
libevent-2.0.so.5
libssl.so.1.0.0

Expand All @@ -61,6 +63,14 @@ To create the lambda, use the aws command line tool as follows, changing region,
--handler lambda.handler \
--runtime nodejs


(see create.sh in the github repository - you will need to modify at least the ARN for the role. Ie change:

--role arn:aws:iam::insertiamnumberhere:role/lambda_basic_execution

)


To invoke the lambda:

aws lambda invoke \
Expand Down

0 comments on commit c6d5c38

Please sign in to comment.