This was motivated by switching laptops and having to setup a new local development environment. I began to explore options so that my development machine mattered less. Essentially moving my dotfiles to reproducible cloud environment.
Some options that provide similar functionality are AWS Cloud9, Visual Studio Codespaces, and GitHub Codespaces. VS Codespaces is pretty cool, but I have found it difficult to connect to a repository outside of GitHub.
Another motivating factor - Docker has been a large contributor to battery drain on my newer MBP. Docker is also the main cause of the fan kicking in and the laptop becoming very hot. Moving docker to a Linux VPS increased my battery life and eliminated the fan/heat concerns.
Lightsail is an easy-to-use cloud platform that offers you everything needed to build an application or website, plus a cost-effective, monthly plan. Whether you’re new to the cloud or looking to get on the cloud quickly with AWS infrastructure you trust, we’ve got you covered.
We can take advantage of Lightsail's easy-to-use platform and low, predictable pricing to launch, manage and scale your application, while always having the option to easily move some or all of your application to EC2, as your needs grow.
- Spin up a linux VPS (Virtual Private Server) that will run our containerized API and DB for our development environment.
- SSH into our VPS.
- Clone our GitLab API Repository (using SSH). We will use an actual API repository from an EdTech SaaS that John and Jordan have been working on.
- Install docker engine and docker compose.
- Install repository tooling.
- Start the API!
- Customize!
- Remote Development using SSH
Why? Allows us to move processor intense operations, like running a Docker Container, off our local machine. Some quicks wins are consitent and reproducible development environments, hardware comes interchangable, longer laptop battery life, and ability to use less expensive hardware (Chromebook for example).
- Log into AWS Lightsail and click Create Instance
- Select a Platform => Linux/Unix
- Select a blueprint => OS Only => Ubuntu 18.04 LTS
- Launch Script => Leave blank for now, but we want to automate this eventually!
- SSH Key => We will use the default for this lab
- Snapshots => We will leave disabled for this lab
- Choose Instance Plan => Select whichever option you prefer
- Identify Instance => Add an instance name
- Click Create Instance
This will take a few minutes to spin up.
Why? SSH will allow us to securely connect to our linux server through the command line and also later with VS Code.
- Click into our Instance and navigate to the Connect tab. Note your Public IP and User name
- Download the Default SSH Key as lightsail-dev-environment.pem
- Move lightsail-dev-environment.pem to
~/.ssh
=>$ mv /Users/<user>/Downloads/lightsail-dev-environment.pem ~/.ssh/lightsail-dev-environment.pem
- Add host to
~/.ssh/config
and link private key file~/.ssh/lightsail-dev-environment.pem
- Open
$ vim ~/.ssh/config
or Create$ touch ~/.ssh/config
Host <Public IP>
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/lightsail-dev-environment.pem
- Type yes to continue connect and to add host to
~/.ssh/known_hosts
- Ok, let us SSH! =>
$ ssh <User name>@<Public IP>
- WARNING: UNPROTECTED PRIVATE KEY FILE! =>
$ chmod 600 ~/.ssh/lightsail-dev-environment.pem
- Seriously, let us SSH! =>
$ ssh <User name>@<Public IP>
We are now connected to our Linux VPS
Why? We will use this Linux environment as a development environment, so we will need access to our repositories, just like we would on a physical laptop. We are using GitLab for this demo, but the same concept would apply to GitHub.
- Create SSH Key =>
$ ssh-keygen -t ed25519 -C "lightsail-dev-environment"
=> Press Enter as needed to select defaults - Copy Public SSH Key =>
$ cat .ssh/id_ed25519.pub
and copy/paste (note: we could have installed xclip) - Open GitLab SSH Settings and Paste / Save the public SSH Key
- Test things are working! =>
$ ssh -T [email protected]
- Type yes to continue connect and to add host to
~/.ssh/known_hosts
We are now connected to GitLab via SSH
- Create and cd into
code
directory =>$ mkdir code
$ cd code
- Clone API Repository =>
$ git clone [email protected]:student-hub/student-hub-api.git
. We are cloning a private repository, so we can demo against an actual production api. At this step, you would be able to clone any of your GitLab repos. - Check out repo =>
$ cd student-hub-api
$ ls
Why? This repo uses docker-compose for local development. We will need to install docker-engine and docker-compose in order to start the API.
- Install Docker Engine =>
$ sudo docker run hello-world
Docker engine installed!
- Install Docker Compose =>
$ docker-compose --version
Docker Compose is now installed! 😎
- Add a docker user group to run commands without sudo.
// Create the docker group.
$ sudo groupadd docker
// Add your user to the docker group.
$ sudo usermod -aG docker $USER
// Activate group changes.
$ newgrp docker
// Verify that you can run docker commands without sudo.
$ docker run hello-world
- Use docker-compose to run
yarn
and install dependencies =>$ docker-compose run hapi yarn
If docker daemon is not running..
Check docker status => $ systemctl status docker
Stop docker service => $ sudo systemctl stop docker
Start docker daemon => $ sudo systemctl start docker
Docker is good to go! 🤘
Why? In this section, we will install tooling that is required for the API project we are using in this demo. Not all projects will require the same tools, but we wanted to demonstrate a real world example.
- Install Google Cloud SDK
- Create
~/student-hub-developer-service-account.json
and paste service account. This service account allows engineers to connect to GCP and interact with Development project cloud storage, etc. - Authenticate with GCP using developer service account =>
$ gcloud auth activate-service-account <service-account-email> --key-file=./student-hub-developer-service-account.json
- Install Postgres Client Tools =>
$ sudo apt install postgresql-client-10
- Import Postgres dumpfile from cloud storage and import. This allows engineers to have a local environment with production load data. We will eventually randomize this data during import, but not necessary right now.
Why? Because we want to make sure it works!
- Start API in detached mode =>
$ docker-compose up -d
- Follow logs =>
$ docker-compose logs -f
- Demo hitting the API from Postman => 😭
- Under Networking Tab, add a rule for port 3005. You can restrict access to specific IP addresses.
Why? In this section, we will customize our Linux VPS
- Install ZSH
- Install Oh My ZSH
- Set ZSH for ubuntu user =>
$ sudo chsh -s $(which zsh) ubuntu
- Exit SSH and SSH In => We should see Oh My ZSH as the default shell
- Install starship
- Install fnm
- Install Yarn
Why? VS Code can be used to open a remote folder allowing us to develop against a VPS.
- Install VS Code Remote Development Extension Pack
- Open the Command Palette
CMD + SHIFT + P
=>Remote-SSH: Add New SSH Host...
=>ssh ubuntu@<Public IP>
- Select the appropriate SSH Config to update
- Select the appropriate directory on the VPS.
VS Code is connected! Our development environment now lives in the cloud! 🔥