Garden & Terraform: Your End-to-End Kubernetes Workflow
By Eythor Magnusson
September 11, 2019
We’re proud to announce that Garden now integrates with Terraform — an open-source tool that enables users to create, change, and improve infrastructure.
With the Terraform plugin, Garden can ensure that your infrastructure is provisioned and up to date before deploying the rest of your stack.
How does it work?
Garden is built around a core concept: A pluggable Stack Graph. The Stack Graph is a graph data structure where each node represents an action — build, deploy, run, test — and the edges between them represent dependencies.
This allows you to codify information like service A must be deployed before service B.
Thanks to the Stack Graph, Garden knows that it can build both services in parallel but that it must then queue the deployment of service B until service A is ready and responding to health checks.
Pluggability means that Garden can ask different plugins to execute these actions.
With these two powerful concepts, it’s straightforward to integrate Garden with a tool like Terraform. Garden ensures that the right action gets executed by the right plugin at the right time.
Or put differently, Garden ensures that the Terraform plugin provisions your infrastructure before the Kubernetes plugin deploys your services.
First we run a command to initialise the environment. This particular example creates a GKE cluster using Terraform. Next we deploy the project to the cluster we just created. Finally we use kubectl to verify that the project was successfully deployed. (The GIF has been sped up.)
In the example above, we start with all of the code but none of the infrastructure. When we initialise the environment, Garden will read our Terraform configuration files, check whether our infrastructure is up to date and apply the configuration if needed. Once our cluster has been created, we deploy the project as usual.
Another common use case is to use Terraform to provision individual cloud services such as databases or message queues. With the Terraform plugin, these actions become nodes in the Stack Graph.
This allows you to queue the deployment of your own services until the cloud services they depend on are ready. You can also reference the outputs from Terraform inside your Garden configuration files. For example, Terraform can output the URI of a cloud database which you can set as an environment variable in your own services.
A complete workflow that combines the two use cases could look something like this:
When you start working, Garden ensures that you have an up to date Kubernetes cluster. It creates one if it needs to. Outputs from the cluster configuration are shared across your project.
Garden builds all your services and deploys them to the cluster. It also runs tests and tasks such as database migrations. Thanks to the Stack Graph, Garden executes these steps in parallel.
If your stack depends on a cloud service, Garden makes sure to first provision it via the Terraform plugin before deploying any services that depend on it.
You can use the outputs from the Terraform module to share the URI of the cloud service with your own services
With everything up and running, Garden now watches your codebase and efficiently re-builds and re-deploys your services as you develop.
All the heavy lifting in the steps above is executed in the cloud —