So like every year, Christmas is approaching too fast, and like every year I’m late at writing Christmas cards! Like all other complex problems in life, also this can be tackled by using the open source infrastructure as code tool Terraform!
2 min read
·
By Stefan Magnus Landrø
·
December 20, 2019
Now from last year, I had the list of friends and their respective addresses in a CSV file that looks like this:
name,street,zip,city,country
Julenissen,Julenissens postkontor,1440,Drøbak,Norway
Santa Claus,Santa Claus's Main Post Office,96930,Napapiiri,Finland
Father Christmas,Santa's Grotto,XM4 5HQ,Reindeerland,United Kingdom
An den Weihnachtsmann,Weihnachtspostfiliale,16798,Himmelpfort,Germany
I then quickly put together a Christmas card template file that looks like the following:
${name}
${street}
%{ if country == "United Kingdom" }${city} ${zip}%{ else }${zip} ${city}%{ endif }
${country}
Dear ${name},
Merry Christmas and a Happy New Year!
The Brits are a bit peculiar about how they address mail compared to the rest of us, and I therefore had to add an if directive in the template.
In order to actually generate some Christmas cards, I decided to write all the cards to disk using the local_file resource in Terraform in combination with the new for_each resource meta-argument and the new for expression (these features were added to Terraform in version 0.12.0 and 0.12.6 respectively). Combine all that with the templatefile expression, and you end up with this Terraform "one-liner":
resource "local_file" "christmascard" {
for_each = { for address in csvdecode(file("${path.module}/addresses.csv")) : address.country => address }
content = templatefile("${path.module}/card.tmpl", each.value)
filename = "${path.module}/cards/${each.key}.txt"
}
Whys is this cool you might ask? Well, there a lots of features in Terraform that allow you to do stuff that you can't do with any of the other infrastructure as code tools out there. Here's a brief list of features that come in handy on a regular basis when working with Terraform:
If you're keen on running this example on your laptop, do the following:
git clone https://github.com/landro/terraform-christmas-postcards
cd terraform-christmas-postcards
terraform init
terraform apply
Loading…
Loading…