Ethereum – Go-Ethereum and AWS – Part 2

September 13, 2018

Author: Michael Gord

Tutorial - MLG Blockchain
This is the second article in a three part blockchain development tutorial series on Ethereum that focuses on how to launch your own Ethereum blockchain using Geth on an AWS server and how to connect it to another node to mine on the same blockchain and interact with the same smart contracts. The first tutorial explained how to configure and connect to your Amazon Virtual Machine(s). This second section will explain how to install and configure your Ethereum blockchain using Geth on Ubuntu. The third section will walk through how to configure a second node to attach to your Ethereum blockchain. We will follow-up with more tutorials that explain how to launch a blockchain instance on other cloud providers such as Azure and Digital Ocean.

If you would prefer to learn how to setup a private Ethereum blockchain locally you can check out our Ethereum development tutorial on how to setup an Ethereum blockchain using Geth in your terminal. You can also find other Ethereum blockchain tutorials at mlgblockchain.com/ethereum-tutorials.html.

Install Geth

It is assumed that here that you have already installed your EC2 instance and have connected to your server through your terminal. If you need help setting this up you can read through part 1 of this ethereum development walkthrough. To start, we need to install the go-ethereum client for the ethereum blockchain. We do this with the commands below which should each be entered separately. The first three commands are downloading prerequisites and the last one is to install ethereum. If you are using IOS, Linux or Windows you can also find these commands on the command line tools for the ethereum network section of the ethereum website.

sudo apt-get install software-properties-common sudo add-apt-repository -y ppa:ethereum/ethereumsudo apt-get update sudo apt-get install ethereumEach step should take some time to complete and it is possible that during part of the loading for one or multiple commands you will see a 404 Not Found error, which is normal and you should ignore. Once your terminal has completed each step you should have successfully downloaded geth. You can test this by entering the help command below. If this does not return the list of commands in the image below you must have missed a step and should go back to our tutorial on how to correctly setup your AWS EC2 instance.

geth --helpGeth Help

Configure Ethereum Node

Congratulations if you have gotten this far you have successfully setup Geth on AWS. Now we will go over how to configure an Ethereum node. Make sure you are in your home directory on your cloud server with the pwd command and then create a new folder called whatever you want to create the genesis block of your Ethereum blockchain. You can do this with the following commands. The first command is to create the folder, change directory into the folder and then create a file called genesis.json.

mkdir mlg-ethchain cd mlg-ethchain nano genesis.jsonTo create a private blockchain you need to define the genesis block. Genesis blocks are usually embedded in the client but with Ethereum you are able to configure a genesis block using a json object. Paste the following JSON object into your genesis.json file and we explain each variable in the following section.

{
"nonce": "0xdeadbeefdeadbeef",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x0",
"gasLimit": "0x8000000",
"difficulty": "0x400",
"Mixhash":
"0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {
}
}
The coinbase is the default address of the mining address. Because you have not created a wallet yet this can be set to whatever you want provided that it is a valid Ethereum address. Difficulty is how hard it is for a miner to create a new block. For testing and development purposes it is recommended that you start with a low difficulty and then increase it. The parentHash is the hash of the parent block which doesnt exist because this is the first block in the chain. The gasLimit is the maximum amount of gas that is required to execute a transaction or send tokens from one account to another. The nonce is a random number for this block. This is the number that every miner has to guess to define the block but for the first block it must be hardcoded. You are able to provide any extraData is the extraData section. In the alloc section you can allocate a number of premined tokens or ether to certain addresses at the beginning of the blockchain. We do not want to do this so we will keep it blank.

After you have confirmed this you can check the file to make sure it has been configured correctly with the cat command. From the same directory input this command.

cat genesis.json

Initialize Chain Data

Now that we have configured our genesis block, we need to initialize a folder to store all the chain data using the command below. You can choose whatever name for the data directory folder you want to create and then you initialize the genesis.json file with the init command. After you run this command you should see a message returned by the termal that says you have successfully written your genesis block and the chain rule has been set. If you request to list the items in the folder you will see that two new folders have been created, one called geth which stores all the chaindata and the other called keystore which stores the key information.

geth --datadir ~// init genesis.jsonStart GenesisYou should see the following output from your terminal. Do not worry that the etherbase has not yet been set and that there is no default account found yet – we will get to that later on.

Init Genesis

Start Ethereum Node

The next step after initializing the chaindata is to launch the Ethereum server. You do this by copying the entire command below and copying it into your terminal. The datadir command is to specify which directory the client should use. The networkid can be customized and should be a random network to ensure the network is private as the networkid is used to connect to another blockchain (we only kept it simple for demonstration purposes). Verbosity is how much data should be saved in the log. We are disabling ipc and enabling rpc and then specifying the port that this Ethereum blockchain should start and on which port the rpc protocol should be at. The rpcaddr should be the Public DNS address of your first AWS instance. We use the console command to show you a javascript console for the geth client.

geth --datadir ~// --networkid 12345 --verbosity 3 --ipcdisable --rpc --port 30301 --rpcport 8545 --rpcaddr console 2>> ~/mlg-ethchain/mlg-ethchain.logIf you have successfully configured your Ethereum node your terminal should respond with a success message confirming that the server is starting and which enode and the HTTP endpoint the blockchain is listening at. As you can see in the picture below it is connected to my AWS instance. The message “Welcome to the Geth Javascript console!” confirms that you have setup the console correctly and you can now input Javascript code into the terminal. The last part is to define a file to store the log files in.

Starting Ethereum on AWSNow you should also have a newly created file in your terminal with the name of your directory and the suffix .log, which should contain a history of all the behaviour that has happened on your blockchain. You can view the contents of the log file with the cat command.

cat .logIt should look different for you as I have done more configuration to this node but the picture above should provide a good idea of how the log file should look if it is setup properly.

Connect Second Peer

If you have gotten this far in the tutorial congratulations because you have configured your first Ethereum node on AWS. Now that we have configured out first node, lets connect a second. Open a separate terminal tab and ssh into the same instance with the same command. From here, you can ensure that you are connected to the same Ethereum blockchain instance by viewing the log file, which you can do with the command below. As you can see, the HTTP endpoint is the same on this node.

tail -F ~//.logView Ethereum log on AWS

Create an Account

You now have an Ethereum blockchain instance running in the cloud that multiple other nodes can attach to with all parties sharing the same log file. To test this lets create a coinbase account with the following command and then enter a password of your choice and repeat it.

personal.newAccount()Create Ethereum on AWSIf you look in the other tab that has the log file displayed you can see that a new wallet event was logged and it shows that the keystore was saved in the file that you specified. It also shows that the wallet is locked, which means that you cannot transfer funds out of it. Make sure to save your coinbase address.

Keystore in Ethereum Log on AWSIf you have any questions about this Ethereum development series please contact us at hello@mlgblockchain.com.


About MLG Blockchain

MLG Blockchain is a blockchain development and consulting firm based in Toronto that is focused on building next generation applications using blockchain and smart contract technology. View all our blockchain development tutorials at www.mlgblockchain.com/learn.html.

MLG Blockchain