3

I tried to continue the discussion on this similar question: Connection Error - Couldn't connect to ethereum node on AWS at http://xx.xxx.xx.xxx:8545. Mods told me to create a new one so here goes.

I have an EC2 instance running an Ethereum node (Parity in my case). The instance is public to the world. I can run some simple code on my local machine at home and connect to the AWS instance:

const Web3 = require('web3')
let web3 = new Web3(
  new Web3.providers.HttpProvider('http://<EC2_PUBLIC_IP>:8545')
)
console.log(web3.eth.blockNumber)

This works fine from any computer that can connect to the internet.

I want to set up a Lambda function that can set up a web3 connection to this node. I've tried a simple function like this:

const Web3 = require('web3')

module.exports.hello = (event, context, callback) => {
  let web3 = new Web3(
    new Web3.providers.HttpProvider(
      'http://<EC2_PUBLIC_IP>:8545'
    )
  )
  callback(
    null,
    success({
      blockNumber: web3.eth.blockNumber
    })
  )
}

When I run this, I always get the error:

Error: CONNECTION ERROR: Couldn't connect to node http://<EC2_PUBLIC_IP>:8545.

I have tried everything and verified that the Lambda can talk to the EC2 by serving an HTTP request (they are in the same VPC). I tried to use the private IP instead of public, DNS instead of IP address, everything.

I've read that people have been able to have Lambda functions call contracts, so I think this has to be possible.

Any ideas on what we can try?

2

I upgraded to the latest Web3 (1.0.0 beta) and somehow the issue was resolved.

0

Please notice that web3 changed between 0.20 and 1.0.0-beta31 from synchronous to asynchronous.

This is how I got it running with web3@1.0.0-beta.31

exports.handler = (event, context, callback) => {
  let ip = '123.123.123.123';
  let web3 = new Web3(
  new Web3.providers.HttpProvider(
      'http://' + ip + ':8545'
    )
  )

  function s1(res) {
    console.log("SUCC: " + res);
    callback(null, res);
  }

  function f1(err) {
    console.log("ERR: " + err);
    callback(err, null);
  }

  web3.eth.getBlockNumber().then(s1, f1);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.