How to setup and run TeslaMate for free on Google Cloud

** GCP working again as of 11th October 2020 – alternatively use this guide for a Digital Ocean ‘Droplet’ **

Having had a few issues running TeslaMate on my Raspberry Pi (mainly very stable on ethernet only, but still bombed out for no reason on occasion), I decided to look into getting TeslaMate running in Google Cloud.

Fortunately, being a light touch application you can run TeslaMate on the free tier of GCP.

You will however need your own domain name to be able to do this following the guide from TeslaMate.

Creating a Micro instance in GCP

The first thing we need to do, is build our micro instance on Google Cloud. I followed this guide: How to set up a free micro VPS on Google Cloud Platform

The only difference to make this work, is to make sure you select http and https and also change the Linux version to Debian 9 (Debian 10 would not work with the auto generated ssl cert for me).

Setting a static IP for your GCP instance

Once your GCP instance is live, you’ll now want to set a static IP address, this should be done to ensure that if you reboot/upscale/rebuild your box you’ll have the same IP address that you are going to point your subdomains to.

Click the ‘hamburger’ (three stacked lines) menu top left, this will pop a left hand menu out. Scroll down the menu items on the left hand side until you get to the Networking section, and then hover over VPC Networks, from the menu that pops out on the right hand side, select External IP addresses

You will now see the VM you have created, in the details of the server, you will see text that says Ephemeral with a down arrow icon. Click the down arrow and instead choose Static. A pop out window will appear asking for a Name, enter the name you wish to give this (I imaginatively choose teslamate), then click Reserve.

This will now fix the currently assigned IP address to your VM.

Create a subdomain

TeslaMate instructions now recommend creating a single teslamate subdomain, it can be simply teslamate.mydomain.com (replacing mydomain.com with your domain name) or something else, this is completely up to you, you’ll just need to make changes in the .env file later on to make sure this works for you.

For this subdomain, you need to change the DNS setup so that the subdomain has an A records that point to your public facing IP address for your GCP micro VM instance.

Wait for the DNS changes to propagate

Before proceeding, I’d advise waiting until your DNS changes have been reflected. Ping your new subdomain URL (teslamate.MYDOMAN.COM) when you get a positive result that matches the public IP of your Micro VM you are good to proceed. This won’t be instant and will depend on the DNS management service you are using.

Docker install

Open SSH (SSH via the browser from the Google Cloud console is fine).

Run the following commands one after the other:
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg2
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce
sudo usermod -aG docker $USER

Docker Compose install

Within the same SSH session, we can now install Docker Compose

Run the following commands one after the other:
sudo curl -L https://github.com/docker/compose/releases/download/1.26.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Creating a swap file

Due to the micro instance having a small amount of memory, we will create a 1gb swap file to help deal with the load.

Run the following 2 commands one after the other:
sudo fallocate -l 1G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile
echo "/swapfile swap swap defaults 0 0" | sudo tee -a /etc/fstab && sudo reboot

Once the box has restarted, continue to the next step.

Installing TeslaMate

You can now follow this guide: Advanced Traefik TeslaMate install

For those unfamiliar with how to create the files in the above guide, I use the nano editor. For example, to create the docker-compose.yml file, type the following into your SSH session: nano docker-compose.yml this will load up the nano editor, allowing you to use your familiar PC/Mac copy/paste commands to get the content in.

Once you’ve finished editing the file(s), Ctrl+X to exit, choosing Y to save the file

I’d advise when first starting up TeslaMate, you run the following just so you can check for any errors: sudo docker-compose up

With the above, you’ll see it getting everything installed, hopefully with no errors. Check your new URL, if you’re a new user login to the teslamate URL, if you’re an existing user moving over to cloud from local you should do a restore of a previous backup before signing in.

Your two URL’s to access Teslamate will be (replacing mydomain.com with whatever you’ve chosen, if you’ve replaced teslamate with something else, you’ll need to substitute this too;
Teslamate control panel (login, settings, geofences etc..): teslamate.mydomain.com
Grafana dashboards (all of the charts etc..): teslamate.mydomain.com/grafana

To access the dashboards once you’ve signed into Grafana, click Home near the top left, the dashboards are then within the Teslamate, Reports and Internal folders.

Once you’re happy everything works, in the SSH window, you can quit the docker-compose session with Ctrl+C, then restart it with sudo docker-compose up -d

What next?

Now TeslaMate is running, you might want to setup some backups, import Supercharger and destination charger locations (so they show up when you charge)

How to do an automated backup of TeslaMate to Google Drive (make sure when creating the script you adjust the script paths accordingly).
Bulk import UK Supercharger locations into TeslaMate
How to bulk import UK destination charger locations into TeslaMate
Setup monitoring to ensure your TeslaMate Google Cloud instances are up and running

Alternative to Google Cloud

Should you wish to host TeslaMate outside of GCP, this guide should help: How to setup and run TeslaMate on a Digital Ocean Droplet server