GithubHelp home page GithubHelp logo

magnetikonline / terraform-github-action-cache-example Goto Github PK

View Code? Open in Web Editor NEW
39.0 6.0 4.0 15 KB

Caching Terraform providers within a GitHub Actions Workflow run to improve execution times.

License: MIT License

HCL 100.00%
terraform github-workflow caching

terraform-github-action-cache-example's Introduction

Terraform GitHub Actions caching example

Terraform provider cache example

Summary

An implementation of caching Terraform providers via actions/cache within a workflow run in an attempt to improve terraform init|plan|apply execution times.

Why?

  • Providers are external to Terraform itself and require download during terraform init operations.
  • Common use providers can often be very large in size. For example, the AWS Provider at time of writing weighs in around 360MB uncompressed.
  • By caching these provider binaries between GitHub Action runs, we hope to have required configuration providers available to terraform sooner!

Example

See: .github/workflows/example.yaml

Breakdown of the key workflow steps:

  • Git source is fetched and Terraform setup via actions/checkout and hashicorp/setup-terraform respectively.

  • Terraform plugin (provider) cache path is configured:

    • By default Terraform downloads providers to individual .terraform/ directories alongside a configuration. By enabling a system wide cache, terraform downloads each provider once to a central location and symlink back into each .terraform/ directory - avoiding repeated downloads. More details here.
    • The plugin cache path is set via the TF_PLUGIN_CACHE_DIR environment variable.
    • With a global plugin cache location enabled, we've now got the perfect candidate for workflow run caching.
  • Finally, actions/cache is setup to save/restore the plugin cache location we've set at ~/.terraform.d/plugin-cache. The cache key is composed using a hash of dependency lock files (.terraform.lock.hcl), introduced in Terraform 0.14.

    Configuration lock file can be created/updated via the following:

     $ terraform providers lock -platform=darwin_amd64 -platform=darwin_arm64 -platform=linux_amd64
  • Finally a terraform init is run, which sets up a trivial Terraform configuration of main.tf.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.