AWS Lambda, please meet IOTA.

AWS Lambda and IOTA FTW!

Lewis Daly
May 16, 2018 · 4 min read

A little while back, I worked on little demo of performing IOTA Proof of Work on AWS Lambda Functions.

I never got around to writing a tutorial, so I thought I’d get back to turning it into a proper npm package, and making an easy-to-deploy environment.

Why?

Why would we want to perform the PoW in AWS Lambda?

Well for a number of reasons:

  • You don’t want to host your own IOTA Full Node, but you can’t or don’t want to perform PoW on your local device.
  • You want an easy way to provision basic resources for a service you’re writing, but want to be able to scale your resources super quick.
  • You’re using a public IOTA node that doesn’t support attachToTangle.

How do I get it working?

Three steps:

  1. You deploy a serverless service from the template found in here (an AWS Account is required)
  2. yarn add iota-lambda-shim to your IOTA JS Project
  3. Add the following lines of code to shim your iota api:

Where iota is your iota library instance, lambda is your AWS Lambda client, and functionName is the name of the lambda function you deployed.

How does it work?

Using black magic. Ok, well maybe not quite, but there’s a few cool tricks going on here.

If you’re following along with the code, the trick is in sls_iotaProxy/AttHandler.js. This is a request handler run by the lambda function. We give it the parameters needed for a normal attachToTangle command, and it is executed on AWS lambda.

The second trick is that ccurl is kind of fiddly (compared to other node modules), and is a little less forgiving if we compile and run on different architecture. The normal way to deploy lambda functions with dependencies is to install your node modules locally, and zip them all up together. This normally works fine - but it’s not the case with the tools we are using.

Instead, we install and compile the lambda function from inside of Docker. If you look at Dockerfile, you will see that it's FROM amazonlinux:latest. This is the very same environment running the AWS Lambda. So as long as we compile our node modules inside of this docker container, we will be fine!

Please, Just walk me through it!

Setting up and Deploying the Lambda

Ok! Let’s go from scratch.

I’m going to assume you have docker, docker-compose and the aws command line tools, along with an active profile installed. Let’s check:

Our docker-compose.yml file also uses our aws credentials from their default location, so make sure you have them in your file system like so:

We should edit the _env.sh file to point to the correct AWS Account - this is just a handy way to switch between multiple AWS accounts using environment variables. My profile is called vessels-lewis.daly.

Should all this work (please let me know if you’re having trouble), you then can run:

Once inside the running container, ensure your credentials have been mounted and the AWS_PROFILE environment variable have been set properly:

If that’s all working like it should, run

And the IOTA Lambda function will be deployed. This tends to take a little while, there’s a lot of stuff going on underneath, like packaging up the lambda function, and using cloudformation to deploy it.

Once deployed, note down the name of your lambda function name. Mine is IotaProxy-dev-attHandler really rolls off the tongue, doesn't it? If you log into the AWS console, you should be able to see it there.

Ok. Hard part over, now let’s move on to actually using this lambda with the IOTA JS Client.

Using the Lambda

In order to use the Lambda, we’re going to use a pretty similar approach to this. This is an example of ‘monkey patching’ the IOTA client for use with the PowBox — but it’s pretty close to what we’re trying to do.

For further reading, take a look at the examples directory in the repo, but I'll write it out here step by step anyway.

  1. Install the iota-lambda-shim and aws-sdk packages. I assume you already have the iota package(s) installed.

2. Make a note of your provider (this can be a public full node that doesn't support AttachToTangle) the functionName. Put them in a config file like so:

3. Now when you set up your IOTA library, do the following:

And that’s it! You can now use a public node that doesn’t allow attachToTangle, and perform the attachToTangleyness yourself.

For example:

Did you manage to get this working? Or got stuck? Let me know. I’d love to help. You can also reach me on twitter @lewdaly, or on the IOTA Discord at lwilld.

If you enjoyed this post, or have any suggestions or questions, let me know in the comments. If you liked this post, give it a ❤️ or a 👏, or whatever you crazy cats are calling it nowadays.

Tips are always welcome 🙌🙌🙌

BJSLSJNPWSM9QLO9JYJAG9A9LLAUKZAQJGYZLNN9YMBNPCUUS9E9EYE9PIKIKNYHXAPNFAMDGXVIPVKIWGDUVDALPD

vessels

Digital Innovation in the Developing World

Lewis Daly

Written by

CTO of vesselstech.com | Digital Tech for Development

vessels

vessels

Digital Innovation in the Developing World

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade