/terragrunt-infrastructure-modules-example

A repo used to show examples file/folder structures you can use with Terragrunt and Terraform

Primary LanguageHCL

Example infrastructure-modules for Terragrunt

This repo, along with the terragrunt-infrastructure-live-example repo, show an example file/folder structure you can use with Terragrunt to keep your Terraform code DRY. For background information, check out the Keep your Terraform code DRY section of the Terragrunt documentation.

This repo contains the following example code:

  • asg-elb-service: An example of how to deploy an Auto Scaling Group (ASG) with an Elastic Load Balancer (ELB) in front of it. We run a dirt-simple "web server" on top of the ASG that always returns "Hello, World".

  • mysql: An example of how to deploy MySQL on top of Amazon's Relational Database Service (RDS).

Note: This code is solely for demonstration purposes. This is not production-ready code, so use at your own risk. If you are interested in battle-tested, production-ready Terraform code, check out Gruntwork.

How do you use these modules?

To use a module, create a terraform.tfvars file that specifies the module you want to use as well as values for the input variables of that module:

# Use Terragrunt to download the module code
terragrunt = {
  terraform {
    source = "git::git@github.com:gruntwork-io/terragrunt-infrastructure-modules-example.git//path/to/module?ref=v0.0.1"
  }
}

# Fill in the variables for that module
foo = "bar"
baz = 3

(Note: the double slash (//) in the source URL is intentional and required. It's part of Terraform's Git syntax for module sources.)

You then run Terragrunt, and it will download the source code specified in the source URL into a temporary folder, copy your terraform.tfvars file into that folder, and run your Terraform command in that folder:

> terragrunt apply
[terragrunt] Reading Terragrunt config file at terraform.tfvars
[terragrunt] Downloading Terraform configurations from git::git@github.com:gruntwork-io/terragrunt-infrastructure-modules-example.git//path/to/module?ref=v0.0.1
[terragrunt] Copying files from . into /tmp/terragrunt/infrastructure-modules/path/to/module
[terragrunt] Running command: terraform apply
[...]

Check out the terragrunt-infrastructure-live-example repo for examples and the Keep your Terraform code DRY documentation for more info.

How do you change a module?

Local changes

Here is how to test out changes to a module locally:

  1. git clone this repo.
  2. Update the code as necessary.
  3. Go into the folder where you have the terraform.tfvars file that uses a module from this repo (preferably for a dev or staging environment!).
  4. Run terragrunt plan --terragrunt-source <LOCAL_PATH>, where LOCAL_PATH is the path to your local checkout of the module code.
  5. If the plan looks good, run terragrunt apply --terragrunt-source <LOCAL_PATH>.

Using the --terragrunt-source parameter (or TERRAGRUNT_SOURCE environment variable) allows you to do rapid, iterative, make-a-change-and-rerun development.

Releasing a new version

When you're done testing the changes locally, here is how you release a new version:

  1. Update the code as necessary.

  2. Commit your changes to Git: git commit -m "commit message".

  3. Add a new Git tag using one of the following options:

    1. Using GitHub: Go to the releases page and click "Draft a new release".
    2. Using Git:
    git tag -a v0.0.2 -m "tag message"
    git push --follow-tags
    
  4. Now you can use the new Git tag (e.g. v0.0.2) in the ref attribute of the source URL in terraform.tfvars.

  5. Run terragrunt plan.

  6. If the plan looks good, run terragrunt apply.