This will be a 2 part series!
I have been fiddling in the blockchain world for a while trying to figure how the blockchain works and how to create one. There isn't one guide that helps one to build his very own blockchain! So I'm writing this article to help you speed up your process of building one. If you are interested to build a blockchain and desire to create your own private ethereum network, look no further.
In this article, I'll be sharing with you on how to create a private blockchain from scratch with almost no cost. It's best used for testing your application code or smart contract that may contain bugs for attackers to exploit. By setting up your own private test blockchain, it helps to simulate the Ethereum blockchain before deploying it to the main network.
- Setting up an AWS instance running Ganache
- Setting up security group to only allow your IP to access the blockchain
- Running the blockchain
- Containerise the blockchain to have it run in background and easier deployment (Part 2)
- Using Nginx to route your blockchain to a URI (Part 2 Coming Soon)
- Request api token with Nginx (Part 2 Coming Soon)
Let's start! Create a small instance in AWS to deploy our blockchain. The smallest instance will do!
- Head over to EC2 service homepage and click on
- Choose the free tier eligible
Ubuntu Server 20.04 LTS (HVM), SSD Volumetype.
t2.microType and press on
- Keep pressing
Nextuntil Step 6: Configure Security Group. Create a new security group with the default SSH rule updated with source
My IP. Add two more rules
Custom UDP. Select
My IPas your source to only allow your IP address to access the blockchain.
- Select Next and press Launch. On the
Select key pairprompt. Select an existing key pair if you have one. Else create a new key pair. Press launch and wait until the Instance State gets to
- Connect into your newly created EC2 instance by SSH or using Instance Connect.
Run the following commands:
$ sudo apt-get update $ sudo apt install curl $ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash $ source ~/.profile $ nvm install --lts
npmis successfully installed
$ npm -v
Install Ganache CLI
$ sudo npm install -g ganache-cli
$ ganache-cli --host "[private_ip_of_AWS_instance]" --port 8545 --networkId 5777
Private IP should be Private IPv4 addresses of your instance
You should see a prompt that Ganache is listening on port 8545 to indicate you have a running blockchain! 🎉
I have created a small node app for you to connect to your blockchain and get the balance of one created account. I used Web3 JS to call the blockchain.
In another tab in your terminal, clone this repository:
$ git clone https://github.com/nazmifeeroz/node-test-blockchain
.env.exampleinto a new file
$ cp .env.example .env
BLOCKCHAIN_IP_ADDRESSfrom your EC2 instance console. This should be the Private IPv4 address
Run the app
$ yarn && yarn start
You should see the balance of your first account of your blockchain. 😎
If you would open up
main.js, there's a
Main() function that is fetching all the accounts available in your blockchain (default is 10 accounts). It then calls the
getBalance() method with the first account address. That simple!
You now have a running blockchain at your own dispense. In part 2 of my series, I will show you how to dockerize your blockchain so that you can startup a blockchain easily and have your blockchain programmatically accessible by using an API token.