IPFS Nodes Setup

Steps to set up IPFS nodes in AWS EC2 instance

Oct 01, 2022

Home

Steps to set up IPFS NODES in AWS e2 instance, the same can be replicated with a local ubuntu 20.04 Linux instance.

References

Deploy a private IPFS network on Ubuntu in 5 steps

Building Private IPFS Network with IPFS-Cluster for Data Replication

Creating a private IPFS Network

Prerequisite

  • SSH into AWS e2 Instances
  • Familiar with bash (Ubuntu 20.04)
  • Familiar with containerizations and VM (docker)
  • Familiar with network protocols and security groups of E2 instances, mainly related to port forwarding and Firewall Inbound rules.

Steps

STEP 1 - Install docker

curl -fsSL https://get.docker.com -o get-docker.sh 
## Remove sudo from the command if working as a root user (to confirm use "whoami")
sudo sh get-docker.sh 

STEP 2 - (Only if the nodes need to persist long-term)(ONLY for AWS)

Assign Static Elastic IP to AWS e2 instance

If the Router IP changes, the nodes will stop communicating with each other

STEP 3 - Configure Inbound Traffic to accept TCP connection on port 4001 and all outbound connections (ONLY for AWS)

STEP 4 (optional) - to make it more secure in production, only allow connections from particular IP’s in the firewall.

STEP 5 (Only if this is the first node in the IPFS network)

## Generating swarm file DO NOT SHARE THIS FILE
## (ANYONE WITH THIS FILE CAN ACCESS IPFS PRIVATE NETWORK)

# Make sure to Save this file locally for future reference

echo -e "/key/swarm/psk/1.0.0/\n/base16/\n`tr -dc 'a-f0-9' \
< /dev/urandom | head -c64`" > ~/swarm.key

STEP 6 (If connecting a new node to the network)

copy swarm file properly (very properly, make sure to check newline characters again after copying) to “~/swarm.key”

STEP 7

## Start go-ipfs docker instance with
## persistent volumes - home, staging, and data
## publish port 4001 TCP/UDP, 5001 TCP, and 8080 TCP.
## SET environment variable LIBP2P_FORCE_PNET=1 to force IPFS into private mode
## SET the env variable for IPFS_SWARM_KEY_FILE to the location of file 
		## which is /home/ubuntu/swarm.key

cd ~/

sudo docker run --name ipfs_node_1 \
-e IPFS_SWARM_KEY_FILE=/home/ubuntu/swarm.key \
-v ~/:/home/ubuntu \
-v ~/ipfs/node1/staging:/export \
-v ~/ipfs/node1/data:/data/ipfs \
-p 4001:4001 -p 4001:4001/udp -p 8080:8080 -p 5001:5001 \
-d -e LIBP2P_FORCE_PNET=1 \
ipfs/go-ipfs:latest
## IGNORE THIS COMMAND if working on Linux.
## if you're working with a local windows machine use this instead to run the 
## docker instance and edit the command 

docker run --name ipfs_node -e IPFS_SWARM_KEY_FILE=/home/ubuntu/swarm.key \
-v C:\Users\SWR\ipfs:/home/ubuntu \
-v C:\Users\SWR\ipfs\staging:/export \
-v C:\Users\SWR\ipfs\data:/data/ipfs \
-p 4001:4001 -p 4001:4001/udp -p 8080:8080 -p 5001:5001 \
-d -e LIBP2P_FORCE_PNET=1 ipfs/go-ipfs:latest

STEP 8 Remove all public ipfs nodes.

sudo docker container exec ipfs_node_1 ipfs bootstrap rm all

## check the result to see if the bootstrap is empty
sudo docker container exec ipfs_node_1 ipfs config show

STEP 9

Node setup is finished but we still need to connect this node to the network

## List the ipfs id of this node
sudo docker container exec ipfs_node_1 ipfs id

## you'll get some output like this
# /ip4/172.17.0.3/tcp/4001/p2p/12D3KooWNx6rULP39RaEB2pJmFqDo6XhbDQhqsVDz95gTcXx1vH5

## pick the tcp/4001/p2p one
# /ip4/<PUBLIC ELASTIC IP>/tcp/4001/p2p/12D3KooWNx6rULP39RaEB2pJmFqDo6XhbDQhqsVDz95gTcXx1vH5

STEP 10

Go to the old node and run this command (if this is the first node run this command on the node where you wanna add new nodes to the network)

sudo docker exec ipfs_node_1 ipfs bootstrap add <ipfs id with correct ip>

## SAMPLE COMMAND
# sudo docker exec ipfs_node_1 ipfs bootstrap add /ip4/<PUBLIC ELASTIC IP>/tcp/4001/p2p/<NODE ID>

STEP 11

Check the nodes by adding a test file

mkdir test
cd test
echo "This is a test file" > test.txt
sudo docker exec ipfs_node_1 ipfs add test.txt
## this will return a CID

## Use the CID from the previous command to fetch test file on the second node using command

sudo docker exec ipfs_node_1 ipfs get <CID>