Deploy your own Ethereum blockchain with Swarm on AWS
In this article we’ll go over how to deploy your own Ethereum blockchain with Swarm on AWS. I highly recommend taking a look at the official swarm documentation and try to get setup locally before proceeding with this tutorial. This will give you a chance to familiarize yourself with some the common commands that will be seen throughout this tutorial.
In the end I hope this helps you deploy your local projects out to the public.
Alright, lets get started!
1. Setup AWS EC2 Instance
First, you’ll need to setup an account with AWS if you don’t have one already. From the console, navigate to EC2 Services and launch a new EC2 instance running Ubuntu.
There’s two important things to note in this process.
- Increase the storage block from the default 8gb to 30gb to ensure we have enough space for a complete sync of the blockchain. See Modifying the Size, IOPS, or Type of an EBS Volume on Linux for details.
2. Create and download the key pair file to ssh into your instance. Make sure to store this somewhere safe!
2. SSH into Instance
First we’ll need to get the public address of your newly created instance. You can find this by selecting your newly created instance and viewing the details under the “Description” tab. You can either use Public DNS (IPv4) or IPv4 Public IP.
Next, we’ll need to use the chmod command to make sure that your private key pair file isn’t publicly viewable. For example, if the name of your private key file is my-key-pair.pem
, you’d use the following command:
chmod 400 /path/my-key-pair.pem
Finally, we can ssh into the instance with the following command.
ssh -i /path/my-key-pair.pem ubuntu@[YOUR_IP]
SUCCESS!
3. Install Go lang
1. Download the latest distribution of Go
curl -O https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz
2. Unpack it to the /usr/local
sudo tar -C /usr/local -xzf go1.8.3.linux-amd64.tar.gz
3. Setup a go folder
mkdir -p ~/go; echo "export GOPATH=$HOME/go" >> ~/.bashrc
4. Update your path
echo "export PATH=$PATH:$HOME/go/bin:/usr/local/go/bin" >> ~/.bashrc
5. Read the environment variables into current session
source ~/.bashrc
6. Double check that Go was installed properly
go version
4. Install Geth and Swarm
“Geth” is the official golang implementation of the Ethereum protocol.
https://github.com/ethereum/go-ethereum
- Create a directory for source code
mkdir -p $GOPATH/src/github.com/ethereum
2. Clone repo
cd $GOPATH/src/github.com/ethereum
git clone https://github.com/ethereum/go-ethereum
3. Install geth and swarm
cd go-ethereum
go install ./cmd/geth/
go install ./cmd/swarm/
4. Check if everything installed properly
geth version && swarm version
5. Account Setup
Now that we’re setup properly, the first thing we need to do is a create an account and a directory for the data store of our instance. I’ve chosen my home directory for this example.
Note: The official docs create this in the/tmp
directory however, I recommend against this since this will cause your keystore to be wiped out after each boot, forcing you to create a new account.
We’ll be referencing this directory quite often so let’s add this to our .bashrc
echo "export DATADIR=/home/ubuntu/myDataDir" >> ~/.bashrc
source ~/.bashrc
Now we can create an account on testnet with the following command.
geth --testnet --datadir $DATADIR account new
After you’ve specified the password, you’ll receive the address of your account which will also serve as the base address of the swarm node.
Let’s save this address easy access later. Remember to exclude the curly braces.
echo "export BZZ=[YOUR_ADDRESS]" >> ~/.bashrc
source ~/.bashrc
For curiosity’s sake, let’s take a look at how our keystore looks after we’ve created a new account.
In the root of your DATADIR
folder you’ll see we now have a keystore
directory with a file containing information about our new account as a simple JSON Object.
{“address”:”8f664c203d98f964410728db393020bf9599e9e6",”crypto”:{“cipher”:”aes-128-ctr”,”ciphertext”:”c0f2ba179d453c935023d36e8ca039f40eab9cde0d5c52d3e1e2e8f0b39e8d99",”cipherparams”:{“iv”:”374873f3450560f1b31859f9210424dd”},”kdf”:”scrypt”,”kdfparams”:{“dklen”:32,”n”:262144,”p”:1,”r”:8,”salt”:”0ba2dee42672ae43d8dab462229ae3e7a3e27d9a1eed8816ea9697239b50c769"},”mac”:”9e042e1d4a6b0214ac745c7ac98670de8b3f518b1c40ded5fd5208d873e3b82b”},”id”:”1257cc78-ff87–4f21-ad5d-c9af3e622e1d”,”version”:3}
5. Starting Geth Instance
Now we you’ve got your account properly setup we can start our instance.
geth --testnet --datadir $DATADIR
In order to start swarm, we’ll need to point to the geth IPC endpoint. Note that it’s location is within our DATADIR.
6. Starting Swarm Instance
From a new terminal tab or window, create a new ssh session while geth remains running in your current one.
There’s three parameters we’ll need to pass to swarm:
- — datadir (previously set to
DATADIR
) - — bzzaccount (previously set to
BZZ
) - — ens-api (our geth IPC endpoint) - Required only for ENS resolution
swarm -bzzaccount $BZZ --datadir $DATADIR --ens-api $DATADIR/geth.ipc
7. Create startup services
Now that we have everything working end to end, we can properly manage these instances with systemd.
This avoids us having to worry about manually start/restarting our nodes and gives us some additional tools that will make managing the nodes much easier.
We’ll start by creating two unit files, one for geth (geth.service) and another for swarm (swarm.service). Put these two files in /etc/systemd/system.
Here is how our directory looks after creating the unit files.
Before we reboot our machine, we’ll need to enable these services to ensure they start on boot. For this, we use thesystemctl
command which will be your main point of interaction for service management.
sudo systemctl enable geth.service swarm.service
Now we’re ready to reboot our machine.
After machine boots back up, let’s check the status of our geth service with the following command.
sudo systemctl enable geth.service swarm.service
8. Logging
What’s great about usingsystemd
to setup our services we get log management for free! journalctl
is another utility that’s part of the systemd
ecosystem that helps us keep track of our service logs.
Let’s start by taking a look at our geth service. The following command uses two flags, -u
for the unit name and -f
for follow the logs.
sudo journalctl -u geth.service -f
And of course, we can do the same with swarm.
sudo journalctl -u swarm.service -f
For more options I recommend reading the following article: How To Use Journalctl to View and Manipulate Systemd Logs
Thanks!
Stay tuned for future articles on the different tools and technologies in the Ethereum ecosystem.