Ben Force
Ben Force - 5x AWS Certified Senior Software Engineer.
Published 2 min read

Easy Test Deployments with Terraform

Easy Test Deployments with Terraform
Cover Photo by Marvin Meyer on Unsplash

Recently I was forced into using Terraform to define infrastructure. I’m so used to CDK and being able to adjust everything through context settings that I quickly ran into a problem: how do I deploy a copy of my project to test it?

Editing Files Directly

All I needed to update was the backend and my AWS profile. So the first way I tried was pretty obvious, edit the main terraform file where the backend and AWS provider is defined. The obvious problem is that I make frequent commits and usually just do git commit -a -m .... So I knew those changes would run in a pipeline and blow it up.

What About Variables?

My next idea was to use variables that would work for the AWS profile. The problem is the backend block needed to be set to local, and you can’t pass variables into block labels.

Override Files

It turns out that HashiCorp has a solution for this: Override Files. They essentially work the same as my first method of editing the terraform file, but it’s done in a special file to keep it out of the repository.

Add *override.tf to your .gitignore file, then add a file next to the one you want to update with the same name, but add _override to the end. For example, I created the file terraform_override.tf to override settings in terraform.tf.

When creating the file, you only have to define the properties you want to be updated. Continuing my example, I created the following override file:

terraform {
  backend "local" {}
}
		  
provider "aws" {
  profile = "my-aws-profile"
}

Summary

Override files allow you to update terraform settings for local development easily. Since they’re stored in files that can be easily added to .gitignore, you don’t have to worry about them ending up in your repository and running on a pipeline.


Related Posts

CDK Integration Testing

CDK Integration Testing
Published 9 min read

Learn how to run integration tests as part of a stack deployment, and automatically roll-back changes when things don't go right.

Dynamic CDK Dashboards

Dynamic CDK Dashboards
Published 5 min read

Manually created dashboards can take up a lot of time to create and maintain. Learn how to automatically create CloudWatch dashboards in the AWS CDK using Aspects.