DEV Community

Cover image for Build your own remote private blockchain with AWS and Ganache
Nazmi for Zenika

Posted on • Updated on

Build your own remote private blockchain with AWS and Ganache

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.

This article will cover

  • 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)

Setting up an AWS EC2 instance

Let's start! Create a small instance in AWS to deploy our blockchain. The smallest instance will do!

  1. Head over to EC2 service homepage and click on Launch Instance.
  2. Choose the free tier eligible Ubuntu Server 20.04 LTS (HVM), SSD Volume type. Ubuntu Server
  3. Select t2.micro Type and press on Next button. Instance type
  4. Keep pressing Next until 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 TCP and Custom UDP. Select My IP as your source to only allow your IP address to access the blockchain. Security Group
  5. Select Next and press Launch. On the Select key pair prompt. 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 Running

Running the blockchain

  1. Connect into your newly created EC2 instance by SSH or using Instance Connect.
  2. 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
    
  3. Verify that npm is successfully installed

    $ npm -v
    
  4. Install Ganache CLI

    $ sudo npm install -g ganache-cli
    
  5. Run Ganache

    $ 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! πŸŽ‰

Test out your 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.

  1. In another tab in your terminal, clone this repository:

    $ git clone https://github.com/nazmifeeroz/node-test-blockchain
    
  2. Duplicate .env.example into a new file .env

    $ cp .env.example .env
    
  3. Get your BLOCKCHAIN_IP_ADDRESS from your EC2 instance console. This should be the Private IPv4 address

  4. Run the app

    $ yarn && yarn start
    

You should see the balance of your first account of your blockchain. 😎

Output Test

About the app

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!

πŸŽ‰ Congratulations!

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.

Cheers!

Top comments (1)

Collapse
 
sephdr profile image
SephDelosReyes

Hi! BLOCKCHAIN_IP_ADDRESS should be the public IP of the EC2 instance when I tested. But thanks for this really helpful walkthrough.