When you may’t code in your most popular language
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
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:
- - Dockerfile
I can take a jab and attempt to clarify the gorgeous code in
add-to-docker-registry-via-terraform.tf 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!).
$trimsuffix(every.worth, "/Dockerfile"): We take away the
/Dockerfilefrom 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")): 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"))): Take away any main or ending whitespaces.
cut up(" ", trimspace(cut up("n", file("$var.images_root_directory/$every.worth")))): We cut up the primary line of the Dockerfile by the empty house and choose the
VERSION=alpinehalf from it.
trimspace(cut up(" ", trimspace(cut up("n", file("$var.images_root_directory/$every.worth"))))): We take away any main or ending whitespaces.
$cut up("=", trimspace(cut up(" ", trimspace(cut up("n", file("$var.images_root_directory/$every.worth")))))): We cut up
=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.