Skip to content

Error in terraform: failed running terraform init

terraform init error

If you’ve ever worked with large GitHub repositories and used Terraform, you might have encountered a frustrating performance issue during the initialization phase (terraform init). GitHub download speeds can sometimes be surprisingly slow, which can lead to failures in infrastructure automation processes. In my case, I ran into the following error multiple times:

Operation failed: failed running terraform init (exit -1)

Why Does This Happen?

This issue typically arises because Terraform, during its initialization phase (terraform init), needs to download the entire repository from GitHub. If the repository is large and has a long history of commits, downloading everything can take quite a bit of time.

Things get even more complicated when the connection to GitHub is slower than usual — maybe GitHub servers are under heavy load, there are network issues, or other factors are at play. The problem becomes critical because the terraform init phase must complete within 10 minutes. If it doesn’t, the process fails, and you’re left with the error mentioned earlier.

The Solution: Use the -depth Parameter

git depth

Instead of downloading the entire repository history, you can limit the number of commits Terraform pulls by using the -depth parameter Git – git-clone Documentation (git-scm.com). This option allows you to control how much of the Git history is downloaded — you can limit it to the most recent commits, which significantly speeds up the process.

In my case, adding the -depth=100 option solved the problem. This limited the download to the last 100 commits, which was sufficient to keep Terraform running without errors. Here’s an example of how to use it:

module "module01" {
source = "git::https://github.com/example/terraform-aws.git//modules/big?ref=v7.0.345&depth=100"
}

How Does -depth Work?

The -depth option in Git restricts the number of commits that are fetched when cloning a repository. Instead of downloading the full history from the beginning of the repository, you can focus on more recent changes. This is particularly useful when you only care about the latest state and not the full commit history. Please note that detailed information about depth can be found in the git documentation.

Benefits of Using -depth:

  • Faster download times – Limiting the number of commits significantly speeds up the initialization process.
  • Less data transferred – Fewer data are downloaded, reducing the load on both your connection and GitHub’s servers.
  • Avoid init timeouts – Since the process is faster, the risk of exceeding Terraform’s 10-minute time limit decreases.

Conclusion

If you’re experiencing issues with Terraform initialization, especially with large repositories and slow GitHub connections, using the -depth parameter can be a quick and effective fix. By limiting the number of commits being downloaded, you can make the process much more efficient and avoid those frustrating errors that halt your work.

Leave a Reply

Your email address will not be published. Required fields are marked *