Maintaining a clean and standardized Terraform code

Everyone has coding styles, but enforcing a standardized and commonly readable style is the key for a smooth collaborative team work. That's why Terraform has a command to ensure both format and style are all right.

I encourage readers to use it extensively, and even integrate it in Continuous Integration (CI) systems and in Makefiles.

Getting ready

To step through this recipe, you will need the following:

  • A working Terraform installation
  • An Internet connection

How to do it…

We'll intentionally write a simple Terraform code with non-standard style and with an error (a missing variable). This will help us manipulate the various tools Terraform offers to ensure the most consistent and homogenous code, so we can achieve more quickly a better quality and a higher level of standardization of our code.

Let's write a provider for AWS like this in provider.tf (deliberately on one line):

provider "aws" { region = "${var.aws_region}" }

Syntax validation

Try to validate that file, and it will notify us that we're missing a variable:

$ terraform validate
Error validating: 1 error(s) occurred:

* provider config 'aws': unknown variable referenced: 'aws_region'. define it with 'variable' blocks

The validation fails, and the return code is 1:

$ echo $?
1

Let's add this variable to a variables.tf file:

variable "aws_region" { default = "eu-west-1" }

Hooray! A terraform validate is now happy:

$ terraform validate
$ echo $?
0

Style validation

This thing is, we solved the obvious problem (a missing variable), but what about style? The preceding style perfectly works, but style might not be canonical.

Let's use the fmt option to check for styling issues, displaying the diff onscreen, but not writing the files automatically:

$ terraform fmt -write=false -diff=true
provider.tf
diff a/provider.tf b/provider.tf
--- /var/folders/zn/bx_20cp90bq5_fqqmlvx3tq40000gn/T/598506546  2016-09-10 22:40:35.000000000 +0200
+++ /var/folders/zn/bx_20cp90bq5_fqqmlvx3tq40000gn/T/407676393  2016-09-10 22:40:35.000000000 +0200
@@ -1 +1,3 @@
-provider "aws" { region = "${var.aws_region}" }
+provider "aws" {
+  region = "${var.aws_region}"
+}
variables.tf
diff a/variables.tf b/variables.tf
--- /var/folders/zn/bx_20cp90bq5_fqqmlvx3tq40000gn/T/743564340  2016-09-10 22:40:35.000000000 +0200
+++ /var/folders/zn/bx_20cp90bq5_fqqmlvx3tq40000gn/T/095288323  2016-09-10 22:40:35.000000000 +0200
@@ -1 +1,3 @@
-variable "aws_region" { default = "eu-west-1" }
+variable "aws_region" {
+  default = "eu-west-1"
+}

We see our style was quite far away from the guidelines. Let's fix this and automatically format our files correctly:

$ terraform fmt
provider.tf
variables.tf

Our two files are now correctly formatted!

I highly recommend putting those two commands in your CI tests (you are running infrastructure code tests in CI, aren't you?), and even before reaching the CI, it's even better if it's in the project's Makefile.

Here's a simple Makefile example:

.DEFAULT_GOAL := all

all:
  terraform validate
  terraform fmt

Now you can just type make in the Terraform directory and you're sure your code both validates and is coherently styled.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.232.189