How to use driftctl with Terragrunt

Let's see how easy it is for Terragrunt users to use driftctl, so they can aggregate all the generated states and track their drifted or unmanaged resources!


You can use driftctl with Terragrunt by using the glob pattern in the --from option: driftctl scan --from tfstate://path/to/states/**/*.tfstate!

Here are all the details.

A Terragrunt (Sample) Project

If you already have a Terragrunt project available: good, use it, and move to the next section!

If you don’t, you can find a simple one in this git repo:  cloudskiff/driftctl-howto-terragrunt.

The sample project uses Terragrunt to store in a single directory (states/) all the Terraform states with predictable subdirectories. The directory base/ simply generates a random string, to be used to build a dynamic name for the S3 bucket in the s3/ directory.

It looks like this:

Our goal after this quick Terragrunt introduction is to observe a structure like this one (see the TF states and directories under ./states/:

Simply run terragrunt init in each folder:

Confirm that a file was correctly generated by Terragrunt:

This ensures that a Terraform state will be stored under ../states/<service>/, as expected.

Now simply apply using Terragrunt as you would with Terraform:

Confirm that a Terraform state now exists under states/base/:

Now let’s do the same steps for the s3/ directory (remember to setup an authentication mechanism for AWS if you didn’t, like AWS_PROFILE):

As an alternative, you could have used terragrunt run-all apply from the root directory, which would have applied it all at once.

Now, we have all our Terraform states stored under states/ within named subfolders:

Driftctl With a Terragrunt State Folder Structure

How can we use driftctl with such a Terraform state folder structure? States aren’t located at the same place, and no one wants to append dozens or hundreds of --from arguments to a CLI.

It’s very simple: by using a glob pattern in the --from option of the tool! Here’s a simple example if you want to load all the files with extension “*.tfstate” in any subdirectory found under ./states/:

If you’re using S3 as a Terraform state storage backend, it works the same way:

If you use Terragrunt to split your environments or any other kind of file/dir structure, it will work the same. Take a look at to get examples and ideas of patterns!

Key Takeaways & Next Steps

In this article, we quickly demonstrated how driftctl can use the many Terraform states Terragrunt can generate in slightly different locations than the original Terraform, using patterns to aggregate the right Terraform state files!

Now you know the right command to get the correct results for your use case, you might want to explore the next steps:

Stay in touch

Get product updates and occasional news.