Search code examples
deploymentautomationterraforminfrastructure

How to Cache Terraform init


We have terraform templates that provision infrastructure and also kick off shell scripts that in turn invoke ansible playbooks to deploy an application.

Both terraform templates are packaged and get invoked from a UI application in an automated way - the intention is provide self-service to developers where they get the entire stack - infra and application deployed with single click.

This automation gets invoked every time a developer raises a request and in the code, we are running terraform init every time to ensure we don't run into any non-availability of plugins down the line. With every run of terraform init, it's loading the plugin binaries over and over into memory and we are running out of memory. Is there a possible solution to prevent terraform init from loading plugin binaries into memory over and over and reuse existing binaries if they are already in memory?


Solution

  • Here I suggest you a bit easier way how to resolve your request. I took it from official Terraform Documentation

    Only 3 steps required:

    1. Create .terraformrc file in $HOME directory;

    2. Paste this code there:

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

    3. Create directory which you specified in the step above (bolded)

    Then, your plugins will be stored like this: plugins cached