Find old-style interpolations in your Terraform code

I just recently started a new job and the codebase that I inherited is…not great. One of the main problems is that it was written for Terraform 0.11 and was never fully upgraded to 0.12 and the goodies that HCL2 and 0.13 brought with them. Of course this means that I needed to find all of the old-style interpolations in our Terraform code base and replace them with the new format. Luckily with VS Code and a little regex, this is a super easy task. This regex is PCRE so you can also use it with sed/awk too if you prefer the command line.

  1. Open the Find and Replace panel
  2. Next to the Find box, click the box that looks like: .*
  3. Search for: "\${([a-zA-Z0-9_.]+)}" (you can visit https://regex101.com/r/K8E5xc/2 for an explanation of how it works along with some examples)
  4. In the Replace box, enter: $1
  5. Now when you actually run the search and replace, it’ll pull the interpolated data out of the old-style interpolation by removing the opening "${ and closing }"

Hopefully this helps some of you who might not be as comfortable with regex clean up your code base and find those old-style interpolations in your Terraform code, since I believe 0.14 will start throwing actual errors and not just warnings on these issues.

How to debug Terraform variable content using this custom module

A lot of the time when I’m having to debug Terraform, I find that I want to quickly look at what Terraform may be storing inside a variable so that I can understand where I’ve made a mistake passing data around. After literal years of using the old TF_LOG=debug method – which can get way too unwieldy with any decently sized environment, I realized I could write a simple module to dump out what I was looking for. Hopefully this will help you too.

resource "null_resource" "terraform-debug" {
  provisioner "local-exec" {
    command = "echo $VARIABLEDATA >> debug.txt"

    environment = {
        VARIABLEDATA = var.your_variable_name
    }
  }
}

Just replace var.your_variable_name_goes_here with your actual variable reference and then run your terraform apply. A file should be spit out in the same directory that the .tf file that you inserted this resource lives in, which you can then open up and understand what might be going on.

I don’t know why it took me so long to figure out something so simple, but coming from PHP/Laravel – where you can just dd() or dump() the contents of a variable to the console – the idea of using the local-exec provisioner to debug terraform just never occurred to me.

Minimize downtime when deploying new code to your Laravel site

I deploy my Laravel site using Ploi, but I was facing a problem: since I don’t use the zero-downtime option, how can I minimize downtime when deploying new code to my website?

NOTE: This article will be updated over time with any new tricks I discover so check back to see what else you can do. They should also work with all deployment tools like Laravel Forge and Deployer; I just happen to use Ploi.

Don’t recompile JS/CSS assets if they haven’t changed

There is no sense re-running yarn run prod if there’s nothing to do, so instead of just doing

yarn install --save
yarn run production

or

npm install --save
npm run production

let’s be intelligent about it:

# Dump the list of filenames changed in the last git commit
git diff --name-only HEAD HEAD~1 > /tmp/gitdiff.txt

# First, assume there were no changes
yarn=false

# Read the /tmp/gitdiff.txt file line by line....
while read line; do
    # The regex to search for CSS/JS/Vue/.lock/JSON/PHP files in the resources folder
    # We include PHP here in case you use PurgeCSS
    positiveMatch='^(resources\/|).*(css|js|vue|lock|json|php)$'
    negativeMatch='^(composer|public)'

    # Check to see if any those filetypes are listed 
    if  [[ $line =~ $positiveMatch && ! $line =~ $negativeMatch ]]; then 
        # ...and if so, set the yarn flag to true
        yarn=true
        echo $line
    fi
done < /tmp/gitdiff.txt

# If any changes were detected, run the necessary yarn commands
if $yarn ; then
    yarn install --save
    yarn run production
fi

# Cleanup
yarn=false
rm /tmp/gitdiff.txt    

And that’s how you can minimize downtime when deploying new code to your Laravel site!

Posts navigation