Terraform plugin caching

Terraform became a standard and tool of choice for many for Infrastructure as Code (IaC) these days. Its learning curve isn’t steep, and engineers can start using it relatively quickly.

Terraform uses provider plugins which enable interaction with the given cloud provider or a service, such as AWS, GCP, Azure or MySQL.

Using providers

All the providers used by the project have to be declared in the following way:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
    mysql = {
      source  = "terraform-providers/mysql"
      version = ">= 1.6"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}

# Demo VPC that will hold MySQL RDBMS
resource "aws_vpc" "example" {
  cidr_block = "10.0.0.0/16"
}

# …more resources…

# Configure the MySQL provider
provider "mysql" {
  endpoint = "my-database.example.com:3306"
  username = "app-user"
  password = "app-password"
}

# Create a Database
resource "mysql_database" "app" {
  name = "my_awesome_app"
}

Once declared, they have to be downloaded into the project. The terraform init command will download and store them inside of the .terraform in your project’s root directory. Pretty straightforward, innit?

The catch

The scenario presented above works well but the providers have to be downloaded for each project you’re working with and these are rather big files (hundreds of megabytes binaries). On a slower internet connection this could become a problem.

Plugins caching

Luckily Terraform allows for plugins caching. So, whenever plugin has to be downloaded and is present in the cache directory, it will be copied into the project instead. This can save some time and bandwidth.

Put this in your ~/.terraformrc and enjoy:

plugin_cache_dir   = "$HOME/.terraform.d/plugin-cache"

Then create the cache directory:

$ mkdir -p $HOME/.terraform.d/plugin-cache
$

Beyond local execution

As much as caching is useful for local development, you definitely want to run your terraform inside of some CI system. Having many pipelines and running terraform init at the beginning of each run can generate quite some traffic and take some extra time. This is really where plugins caching comes in handy.


We offer hands-on AWS training as part of our SaaS Growth subscription, to help your team make the best use of the AWS cloud. Book a free chat to find out more.

For some topics, you can also get the same training just on the topic you need - see our Terraform training and Kubernetes training pages.


This blog is written exclusively by The Scale Factory team. We do not accept external contributions.

Free Healthcheck

Get an expert review of your AWS platform, focused on your business priorities.

Book Now

Discover how we can help you.


Consulting packages

Advice, engineering, and training, solving common SaaS problems at a fixed price.

Learn more >

Growth solutions

Complete AWS solutions, tailored to the unique needs of your SaaS business.

Learn more >

Support services

An ongoing relationship, providing access to our AWS expertise at any time.

Learn more >