Skip to content

Instantly share code, notes, and snippets.

@antonbabenko
Created October 31, 2017 08:45
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save antonbabenko/2ca1225589c7c6d42f476f97d779d4ff to your computer and use it in GitHub Desktop.
Save antonbabenko/2ca1225589c7c6d42f476f97d779d4ff to your computer and use it in GitHub Desktop.
Example of using terraform-aws-modules/vpc/aws module with Terragrunt

This is an example of how to use Terraform AWS registry modules with Terragrunt.

Notes:

  1. source has to be full git URL and not Terraform Registry open issue #311
  2. File main_providers.tf is named so, because it will be copied to another local directory and merged with module's code. If such file exists in the module already then it will overwrite the one provided by the module.
provider "aws" {
region = "eu-west-1"
}
terraform {
backend "s3" {}
}
terragrunt = {
terraform {
source = "git::git@github.com:terraform-aws-modules/terraform-aws-vpc.git?ref=v1.0.4"
}
}
# MODULE PARAMETERS
name = "my-vpc"
cidr = "10.212.0.0/20"
azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
private_subnets = ["10.212.1.0/24", "10.212.2.0/24", "10.212.3.0/24"]
public_subnets = ["10.212.11.0/24", "10.212.12.0/24", "10.212.13.0/24"]
database_subnets = ["10.212.5.0/24", "10.212.6.0/24", "10.212.7.0/24"]
create_database_subnet_group = true
enable_dns_support = true
enable_dns_hostnames = true
enable_nat_gateway = true
single_nat_gateway = false
Copy link

ghost commented Oct 31, 2017

Your terraform.tfvars file points directly at the registry module github source. Wouldn't it need to point at the directory containing the main_providers.tf file? Otherwise, I don't see how terragrunt is going to pick up the provider and backend declarations.

I'd have thought that terraform.tfvars should use a github url to MY module repo, and then main_providers.tf would include a module block which would have its own source attribute that points to the module in the terraform module registry (via github, I understand)

Also, if the main_providers.tf file has a module block, would I need to set variables in it or would those automatically be transferred from the tfvars file and passed through? Otherwise, I end up with something like this, which seems like a lot of typing.

provider "aws" {
  region = "eu-west-1"
}

terraform {
  backend "s3" {}
}

module "vpc" {
  source = "git::git@github.com:terraform-aws-modules/terraform-aws-vpc.git?ref=v1.0.4"

  name = "${var.name}-vpc"
  cidr = "${var.cidr}"

  azs = ["${var.azs}"]
  private_subnets = ["${var.private_subnets}"]
  ...
}

And I assume I would have to provide variables.tf and outputs.tf, too? Or will terragrunt automatically map inputs and outputs?

@seanorama
Copy link

Where are you storing the main_providers.tf file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment