How to create a private Ethereum blockchain node hosted on AWS in 5 minutes for free
Before deploying your application to the main network Ethereum blockchain, your application code must be thoroughly tested — especially the remote procedures calls (RPC) used to connect application code with the underlying Smart Contract. If your application code or smart contract contains bugs for attackers to exploit, be assured that it will be exploited. This is where Ganache, an in-memory virtual blockchain that simulates the Ethereum blockchain comes to play.
What this tutorial covers
- Setting up an AWS instance running Ganache-cli for free
- Setting up a firewall for your AWS instance to prevent denial-of-service (DoS) attacks on your machine
- Running a private blockchain that saves state
- Running the blockchain without it timing out
Ganache can be thought of as your own private blockchain. You can create an unlimited amount of Ether and Ethereum addresses that allows you to truly simulate the Ethereum blockchain without needing a large machine to store and process blocks.
Tutorials online only show you how to setup and run a Ganache blockchain locally on your machine, however, this limits production testing as your local machine is the only machine able to connect to the private blockchain. This post will show you how to set up your own private blockchain on AWS that can be accessed by any specified machine, which will allow you to test your application at a larger scale.
- Create an AWS account or login to an existing account: https://aws.amazon.com/.
- Go to the AWS Management Console, and select the EC2 service.
3. Launch a new Ubuntu instance (this can be done without a template)
4. Select the t2.micro instance, and press the blue Review and Launch button.
5. Press the blue Launch button.
6. Once your instance is created, create a Firewall for your Instance.
Setting up a firewall for your EC2 instance
Setting up a firewall is important to prevent DoS attacks against your EC2 instance.
Create and add a Security Group to your instance with the following inbound rules
The SSH rule will allow you to SSH into your machine. The Custom UDP/TCP rule will allow you to connect to the blockchain on port 8545. The ICMP Echo request rule will allow you to ping your instance.
*Select “My IP” for source such that only your IP address can make the certain request to the instance. Replicate each rule for additional IP address, respectively.
**Without the first SSH rule, you will not be able to ssh into your machine.
7. Once the instance state is “running”, select it and press the Connect button.
8. Copy and paste the example ssh command, and enter it into your Terminal.
9. Once you’re in the Ubuntu machine, you are ready to install Ganache.
In your Ubuntu machine, run the following commands in the terminal.
sudo apt-get update
sudo apt install npm
Verify npm successfully installed
npm -v --this returns for me 3.5.2
sudo npm install -g ganache-cli
Ganache is now installed. To run it, run the following command:
ganache-cli -d [seed_number] --host "[private_ip_of_AWS_instance]" --port 8545 --db [path_to_store_blockchain_data]
- d [seed_number] — Ganache outputs you by default 10 addresses. By adding a seed number, Ganache will recreate the same 10 addresses preloaded with 100 Ether.
- --host “[private_ip_of_AWS_instance]” can be found by selecting your EC2 instance in the management console. This is crucial for Ganache to successfully run.
- --port 8545 — blockchain nodes are typically run on port 8545, but feel free to change this.
- — db [path_to_store_blockchain_data] — include this if you want the state of transactions on your private blockchain to be saved.
To run Ganache indefinitely without needing to keep the AWS instance up, use Ubuntu’s included nohup tool.
/usr/bin/nohup ganache-cli -d [seed_number] --host "[private_ip_of_AWS_instance]" --port 8545 --db [path_to_store_blockchain_data] &
Verify Ganache has a pid
ps -ef | grep ganache
10. You can now connect your application to the blockchain using web3. For example, in python do:
from web3 import HTTPProvider, Web3, exceptions
w3 = Web3(HTTPProvider('http://[instance_public_ip]:[port]'))
Comment any questions and I’ll get back to you.