Putting it all together

Let's step through a real-life example of this. By the end, we should be able to have a Lambda function working using our own custom runtime. The example we will use is creating a function using lolcode, just because it's an easy example and it's hilarious. There is one limitation in that we will still need to write our bootstrap file in bash because lolcode doesn't support accessing environment variables, and, therefore, we wouldn't be able to process events. We will still need to bundle our lolcode interpreter because, understandably, this isn't included in the base Lambda execution environment. Hopefully, in keeping it simple, everyone can follow along to see what is happening.

First of all, we need to sort out our interpreter, making sure that we can create a binary so that we can package it in our Lambda deployable. Lambda uses Amazon Linux as the underlying environment, so I'm going to fire up a Docker container running this OS so that we can build our binary. We'll only need to do this once:

docker pull amazonlinux
docker run -it -v ~/Desktop/lolcode-lambda:/root amazonlinux /bin/bash
yum -y install git cmake make gcc gcc-c++
git clone https://github.com/justinmeza/lci
cd lci && cmake . && make

Awesome  our interpreter binary has been built and is ready at our bind mounted location. Find the file called "lci" and copy that into your workspace:

cp ~/Desktop/lolcode-lambda/lci/lci ~/Workspace/lolcode-lambda/bin/

Next, we need to create a bootstrap file. Use the file that we provided in the previous section and save it as bootstrap in your workspace. Remember to make it executable!

chmod 755 bootstrap

The last file we need now is our actual function code—here's a lolcode function I created earlier! 

HAI 1.2
HOW IZ I HANDLER
I HAS A EVENT ITZ A YARN
GIMMEH EVENT
CAN HAS STDIO?
VISIBLE SMOOSH "HEAZ UR EV KK:: " AN EVENT MKAY
IF U SAY SO
I IZ HANDLER MKAY
KTHXBYE

Lolcode is a fun and hilarious language to write things in. This function takes the event data that was piped in when the code was run, creates a variable, and then concatenates a string to print to stdout. Check out the lolcode specifications document here to decipher what's going on: https://github.com/justinmeza/lolcode-spec/blob/master/v1.2/lolcode-spec-v1.2.md.

Now, we should have three files that are arranged like this:

lolcode-lambda
├ bin
    └ lci
├ bootstrap.shj
└ hai.lo

It's time to package up the files and deploy them to a new Lambda function. The following commands will compress the files into a ZIP file and then create and deploy a new Lambda function:

zip -r function.zip bin bootstrap hai.lo

aws lambda create-function
--function-name lolcode-runtime
--zip-file fileb://function.zip
--handler hai.lo
--runtime provided
--role arn:aws:iam::${account-id}:role/lambda_basic_execution

With the following command, we can invoke the function with some data. We can use some arbitrary input as the payload data:

aws lambda invoke --function-name lolcode-runtime --payload '{"text":"OH HAI!!!!1!"}' response.txt

Okay, so the finer details of the lolcode function don't really handle JSON very well and using bash as a bootstrap file is kind of cheating, but this exercise was more about following the process so that you can create your own. With all the languages that are available to you, this process can get quite complex, and you can find some examples that have been made by other developers on GitHub.

Using your own runtime is quite an advanced feature. When your development progresses to advanced levels, you may also be interested in efficiency. In the next section, we will explain how to reuse code across multiple Lambda functions using layers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.47.25