Web3.js on AWS Lambda for Serverless Ethereum Development

by CodeRecipe devs

June 25, 2019

This example packages the web3.js library and the required native extensions into an AWS Lambda function. When requests are sent to API Gateway, ethereum transactions can be sent via web3.js within the Lambda function.

Quick Notes
Web3.js on AWS Lambda for Serverless Ethereum Development

Problem Statment Build serverless apps using Web3 library on AWS Lambda. Currently, the web3.js library requires native extensions which have to be manually installed in the same environment as AWS Lambda.

Solution

Using serverless-plugin-scripts and docker to precompile node modules and native extensions to build a working deployment of web3.js on AWS Lambda.

Setup

Prerequisites

npm install serverless  

Make sure you have AWS access key and secrete keys setup locally, following this video here

Obtain an Infura ropsten node url and key from here and generate an ethereum address from a wallet service like MyEtherWallet

Download the code locally

serverless create --template-url https://github.com/CodeRecipe-dev/Serverless-Web3 --path serverless-web3

Deploy to the cloud

cd serverless-web3

npm install

serverless deploy --stage <stage-name> --ROPSTEN_INFURA_URL "https://ropsten.infura.io/v3/<key>" --ETH_PRIV_KEY <eth-private-key> --ETH_FROM_ADDRESS <eth-from-address>

Invoke

serverless invoke -f EthHandler -d {'body': {'toAddress':'0x', 'ethAmount': 0.001}} --stage <stage-name> --ROPSTEN_INFURA_URL "https://ropsten.infura.io/v3/<KEY>" --ETH_PRIV_KEY <eth-private-key> --ETH_FROM_ADDRESS <eth-from-address>
source code
platforms

AWS