How to Write Beautiful Code in Terraform | by Ali Abbas Jaffri | May, 2022

When you may’t code in your most popular language

Picture by writer. Logos’ copyright belongs to Terraform and Azure

This write-up is under no circumstances about writing stunning code however somewhat how a easy requirement can get fairly (pun supposed!) horrific in Terraform.

All of us have written code we aren’t happy with. Typically, this piece of code is the most effective ‘momentary’ resolution we have now — and lives on for eternity in git — as an indication of what number of guidelines and logic we have now to bend to make issues work. I can proudly say that I, too, have contributed code that’s compelled me to embark on the stroll of disgrace!

In my defence, it was onerous to develop higher options to get the identical activity accomplished.

We fear about cybersecurity and have a default whitelist policy, together with famend container registries. That led to the requirement of getting some base containers in our registries, which might then be handed to our totally different environments utilizing terragrunt configuration.

We now have to — manually — add them to the Azure registry for the primary time in order that we will embrace them in several environments utilizing terraform, grunt.

A nginx minimal alpine dockerfile situated within the root listing of the challenge
Sure, please preserve scrolling to the best for disgrace factors!
The stunning terragrunt configuration

We import the picture into the atmosphere utilizing an Azure CLI command for it to be current within the ACR registry. Subsequent, we outline a Dockerfile (with the picture and its model) in a Dockerfile argument and tag it in our registry. Sooner or later, if different functions need entry to the Nginx container, they will merely pull it from our Azure registry.

Dockerfiles are organised within the following order:

- nginx
- - Dockerfile

I can take a jab and attempt to clarify the gorgeous code in file. We iterate over the docker photos current within the docker-images folder as per the terragrunt configuration. Subsequent, we choose up recordsdata with the title Dockerfile, add the ACR handle to the title, after which search for the title of the picture folder (which is the title of the picture itself!).

Lastly, we use trimsuffix, trimspace, and split to complete up. The method might be divided into the next elements:

  • $trimsuffix(every.worth, "/Dockerfile"): We take away the /Dockerfile from the trail of the file.
  • file("$var.images_root_directory/$every.worth"): We learn the dockerfile by the whole path.
  • cut up("n", file("$var.images_root_directory/$every.worth"))[0]: We cut up the file by line breaks and picked the primary half that matches the model.
  • trimspace(cut up("n", file("$var.images_root_directory/$every.worth"))[0]): Take away any main or ending whitespaces.
  • cut up(" ", trimspace(cut up("n", file("$var.images_root_directory/$every.worth"))[0]))[1]: We cut up the primary line of the Dockerfile by the empty house and choose the VERSION=alpine half from it.
  • trimspace(cut up(" ", trimspace(cut up("n", file("$var.images_root_directory/$every.worth"))[0]))[1]): We take away any main or ending whitespaces.
  • $cut up("=", trimspace(cut up(" ", trimspace(cut up("n", file("$var.images_root_directory/$every.worth"))[0]))[1]))[1] : We cut up VERSION=alpine by the = operator, and extract the tag from the model and append it on the finish of the picture handle.

Phew! It wasn’t that arduous.

It does make one suppose this code may have been written higher, or this example may have had a greater resolution.

Relating to the primary half, the syntactical restriction of Terraform’s configuration language makes it onerous to think about a greater approach to do it (for that matter, is there a greater approach?).

Specializing in the second half, I want to know from the consultants on the market if I may have accomplished it higher or how you’d handle this subject given the state of affairs.

I’ve discovered it fairly onerous to write down good code in Terraform. The syntactical sugar that comes with crucial languages, equivalent to Python, is lacking with HCL. It does clear up the aim it was constructed for, however there are higher options today, equivalent to pulumi or terraform cdk. They allow you to code within the language of your alternative so you may write good, legible code.

More Posts