AWS Lambda, please meet IOTA.

AWS Lambda and IOTA FTW!

Lewis Daly
vessels
Published in
4 min readMay 16, 2018

--

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:
const IotaLambdaShim = require('iota-lambda-shim');// Patch the current IOTA instance
IotaLambdaShim({iota, lambda, functionName});

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:

$ docker --version
$ docker-compose --version
$ aws s3 ls

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:

$ cat ~/.aws/credentials
[vessels-lewis.daly]
aws_access_key_id = *****
aws_secret_access_key = *********

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.

export AWS_PROFILE=vessels-lewis.daly

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

# build the docker container
$ ./_enter_docker.sh build
# run and log into the container
$ ./_enter_docker.sh

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

$ echo $AWS_PROFILE

If that’s all working like it should, run

$ ./_deploy.sh

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.

There it is!

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.
$ yarn add iota-lambda-shim aws-sdk

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:

//config.js
module.exports = {
provider: 'http://5.9.149.169:14265',
functionName: 'IotaProxy-dev-attHandler',
}

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

--

--