Roll your own Godmode Ganache CLI server on AWS
Godmode Ganache CLI is an open-source DeFi testing tool that lets you spin up a copy of the Ethereum mainnet and take control of third party contracts. Godmode Ganache CLI enables Ethereum developers to test what could happen to their product when things change elsewhere.
Interactions between contracts on Ethereum are programmatic, allowing us enormous power to test what-if scenarios concretely. Such a level of real-world integration testing is unprecedented. However, we’re only just beginning to explore the possibilities.
What we’re going to do
This article will explain how you can spin up an instance of Godmode Ganache CLI in a Docker container running on EC2 in AWS. You can then use CURL commands from your command line or a tool like Postman to run RPC commands on your Godmode Ganache CLI over the internet.
Pre-requisites
We assume you have an AWS account and some prior knowledge of AWS compute resources (EC2 and ENI in particular). You’ll need an SSH key pair ready for use to allow you to connect to the EC2 instance you create. Check out this tutorial if you need to create a key pair.
You’ll also need an Infura account and an Infura project endpoint ready to go since we’ll use this to create our ephemeral copy of Ethereum’s mainnet. Check out this tutorial if you need help setting up Infura.
1. Create your Security Group
You will need a security group with two Inbound rules:
- Accept HTTP connections on port 8545 from Anywhere. This is the port used by GodMode Ganache CLI.
- Accept SSH connections on port 22 from Anywhere. This allows you to connect to the box and run GodMode Ganache CLI.
Create this in the Security Groups section on the AWS EC2 console screen. You’ll find it on the left under Network & Security.
You can choose any name you want, but we recommend godmode-ganache-cli. As we’re simply creating this instance for demo purposes, you can permit traffic from Anywhere for both of these rules.
2. Spin up your EC2.
Go to the AWS console and then to the EC2 dashboard in the EC2 section. Choose Launch Instance > Launch Instance.
For your AMI, choose Amazon Linux 2.
Choose an Instance Type of t2.micro (this will be enough for our purposes). Click Next: Configure Instance Details and then Next: Add Storage. No need to change anything on the Storage screen.
Click Next: Add Tags. Adding tags is not mandatory, but as a base, you should give it a Name tag. In my example here, I’ve called my EC2 instance ggc-t2micro.
Click Next: Configure Security Group.
On the following screen, choose to Select an existing security group from the two radio buttons. Select your godmode-ganache-cli group from among the list before clicking Review and Launch and then Launch.
Your final step will be to select the SSH key pair you are using (see the Pre-requisites section above if you need to). Check the box acknowledging you have access to the key and click Launch Instances.
3. SSH onto the box
Once your instance is up and in the Running instance state, you can connect to it via SSH. In the EC2 Dashboard on the AWS Console, select the Instance ID of your EC2 instance.
Click the handy little copy icon next to the Public IPv4 address. This copies your IP address to the clipboard.
Open a command prompt at the same directory you have your .pem key file and enter the following, substituting the name of your key file and the IP address you just copied accordingly.
$ ssh -i <your-key>.pem ec2-user@<your-public-ipv4-address>
You should then have access to the box. If you are prompted, type ‘y’ to accept validate the IP address. You should then see something like this:
3. Install Docker
Next, you’ll install Docker. At the command line type
$ sudo yum update -y
then install Docker via
$ sudo amazon-linux-extras install docker
Start the Docker service via
$ sudo service docker start
You can add the ec2-user to the docker group to no longer require sudo.
$ sudo usermod -a -G docker ec2-user
Verify that Docker is running via
$ docker info
4. Pull Docker image
You can pull the godmode-ganache-cli image from Docker Hub just like you would with a GitHub repo.
$ docker pull xgmstudio/godmode-ganache-cli
That’s it! You’re ready to run your instance of GodMode Ganache CLI.
5. Run in screen passing in your Infura Key
For the instance of godmode-ganache-cli to persist after you disconnect your SSH session, you’ll need to run the Docker container in screen. Firstly, start a new screen via
$ screen
Then you can finally run the Docker container and spin up your GodMode Ganache CLI. You’ll need your Infura endpoint ID, as mentioned in the pre-requisites section.
- We’re running the container in interactive mode (-it), with automatic removal once we’re done ( — rm).
- We map port 8545 on the EC2 instance to the same in our container (-p), which is the default port that GodMode Ganache CLI is listening on.
- We pass in our Infura project ID as an environment variable ( — env), allowing us to use Infura to get a copy of Ethereum’s mainnet
- We name the running container ‘GGC’ ( — name), as xgmstudio/godmode-ganache-cli is rather a bit too long.
$ docker run -it --rm -p 8545:8545 --env INFURA_PROJECT_ID=<your-infura-project-id> --name GGC xgmstudio/godmode-ganache-cli
And voila, as we say here in Paris. Your Ethereum mainnet copy with GodMode features is now available for you to send HTTP requests to it. Your output should look something like this:
Testing
Now to test. In a second command prompt window, you can run the following command using CURL. You’ll need the Public IPv4 address of your EC2 instance, as we noted above.
The following command should give you a list of accounts currently available on your GodMode Ganache CLI instance:
$ curl -X POST http://<your-EC2-public-IPv4-address>:8545 — data ‘{“jsonrpc”:”2.0",”method”:”eth_accounts”,”params”:[],”id”:1}'
Alternately, you can use Postman to test or even start launching calls directly from your application’s testing suite.
Limitations of this approach
This is just a basic introduction to rolling your own EC2 instance serving GodMode Ganache CLI. It would be best not to use the above configuration for enterprise-grade testing or anything else with sensitive testing data. There are several important limitations:
If your EC2 goes down, there is no auto-healing. Without any mechanism to restore the instance, you will lose your server. A more robust version would require an AWS Elastic Network Interface (ENI) to lock in the IP address. Otherwise, the IP address would change when AWS created a new instance. You would also require an Auto Scaling Group (ASG) to ensure the instance of EC2 was recreated automatically (by setting the Desired Instance value of 1).
You’re not using HTTPS. Currently, anyone on the internet can intercept your data, giving them access to any contracts you put onto the GodMode Ganache CLI and any subsequent testing traffic. The remedy is to put either an AWS API Gateway or Application Load Balancer (ALB) in front of your EC2.
Only the last 128 blocks (e.g. around 27 minutes of mainnet) are available on an Infura Mainnet fork. So, if your testing run goes beyond 27 minutes, you will no longer have access to blocks mined when you first span up the GodMode Ganache CLI instance.
Here at GodMode, we’re working on solutions to deliver a more robust developer experience that solves all three of these limitations by offering GodMode Ganache CLI as a service on demand. Watch this space for further developments!