Setting up ephemeral environments per-pull-request with Terraform

By Jagath Vytheeswaran · November 6, 2021  ·  Follow us on Twitter

Ephemeral environments are the DevOps cheat code that help engineers at companies like Facebook and Lyft ship to production thousands of times a day. With them, engineering teams can avoid bottlenecks on staging or other fixed pre-prod environments that arise from needing to share feature previews or run intensive E2E testing workflows. In this article, you'll learn how to use Gallery to set up ephemeral environments on your own cloud in under 30 minutes, so you can get code from PR to production way faster!

Setting up a Template

Environment workflows in Gallery are centered around templates; in short, a template is a record of all of the cloud services that you want to spin up for each environment, along with the build instructions to do so.

Gallery offers a number of ways to create templates; this walkthrough will populate a template using an existing Terraform config. Don't use Terraform? No problem - Gallery supports a number of methods to get your infrastructure into templates; feel free to check out the docs, schedule a call, or message us (on the bottom-right) to learn more.

Importing Services

Setting up a template with Terraform is a simple matter of either linking a repo with your Terraform files or directly pasting in a Terraform snippet. Gallery will import the services from the Terraform code (it takes a minute or so) and prompt you to proceed to indicating the build instructions.

Linking to a repo with Terraform files

Build Instructions

There are two ways to indicate the build instructions; you can either indicate them in a YAML file within your repo, or you can designate them directly in the UI.

YAML build instructions

For this option, you'll need a file named gallery-deploy.yml within the root of your repo. The structure of this file is similar to that of build instruction files for CI tools like Github Actions; you can see the specifications here

Indicating build instructions in the UI

To manually indicate build instructions, you'll need to input the deployment commands, a Docker image with dependencies, and any necessary build services (e.g. Docker-in-Docker). You can find more details here.

Entering build instructions in the UI

When you're done with this step, you can proceed to adding the trigger to build per PR!

Linking the pull-request trigger

All that's left to do is add the PR trigger; to do so, you just need to choose a repo, (optionally) indicate a set of environment variables, and choose the right build trigger for your needs. For per-PR environments, you'll want to choose "All Pull Requests," though we also enable builds on tagged PR's/commits (tagged with gallery-build).

Linking a Git trigger

And that's it! Every time you create a PR, Gallery will comment with a link to the environment + feature preview!

Gallery commenting on a PR with the environment link

Managing the ephemeral environments

Environments linked to PR triggers will spin down immediately when the PR is merged or discarded. You can also spin down environments manually, view build logs, and access the URL for full-stack feature previews in the "Environments" tab.

Viewing all Gallery-managed environments

Want to try this flow out for yourself? Get started for free here, or schedule a call to learn more about how Gallery can work for your team!

- Gallery Team

Monoid, Inc.