Stephen Hara

Elixir on GCP Cloud Run

Published on 2/3/2022

  • bearbot
  • post

Recently I've been putting some of my projects on GCP mostly just to do it more, and seeing as I had to patchwork a lot of it myself I thought I'd try my hand at a nice Easy Checklist procedure for deploying Elixir and Phoenix applications with a PostgreSQL database on Cloud Run with Github Actions.

What You Need

  • You should have an application that has a Dockerfile you can use to run the app and responds to requests on port 8080. I'll provide an example starter Dockerfile, but this won't be the main focus of the post
  • You should have a GCP account, and about $10 per month for the most minimal form of this setup
    • It'll probably be less than that for the cheapest options, especially if you don't have any existing Cloud Run free tier usage. Based on the pricing page, a shared-core db-f1-micro instance will cost about $7.70 per month
  • To do this with Github Actions as your CI/CD, you will need a Github account
  • You will need lots of patience! Driving things in CI can end up time consuming, particularly when reproducible builds are involved, and there's some trial and error to get a consistent working pipeline

The Final GCP Setup

To give you an idea of what we want to accomplish, I'll start with the resulting GCP structure, and then we can work somewhat backwards from there to come up with the process.

  1. A GCP project for your application
  2. A Cloud SQL instance with a user-created user and a database for the app
  3. A Cloud Run configuration for the application
    • the configuration must be aware of the Cloud SQL instance, and
    • must have access to the required secrets
  4. A GCP service account to deploy the application
    • The account must have "Cloud Run Admin", "Storage Admin", and "Service Account User" roles
  5. A GCP Container Registry for storing the Docker images of your app
  6. Some secrets for your application to load at runtime. In this post, we need:
    • the database password
    • the database socket connection directory path
    • and the Phoenix secret key base