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?