Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terragrunt examples #39

Open
wants to merge 2 commits into
base: feature/terragrunt
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/terragrunt-example/terraform/accountname/account.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Set account-wide variables. These are automatically pulled in to configure the remote state bucket in the root
# terragrunt.hcl configuration.
locals {
account_name = "resource"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
locals {
stage = "prod"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
locals {
# Automatically load environment-level variables
global_vars = read_terragrunt_config(find_in_parent_folders("global.hcl"))
environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
}

terraform {
source = "git@github.com:terraform-aws-modules/terraform-aws-iam.git//modules/iam-assumable-roles?ref=v2.21.0"
}

include {
path = find_in_parent_folders()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Set common variables for the region. This is automatically pulled in in the root terragrunt.hcl configuration to
# configure the remote state bucket and pass forward to the child modules as inputs.
locals {
aws_region = "eu-west-1"
}
3 changes: 3 additions & 0 deletions examples/terragrunt-example/terraform/global.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
global_name = "test-terraform-modules"
}
64 changes: 64 additions & 0 deletions examples/terragrunt-example/terraform/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# ---------------------------------------------------------------------------------------------------------------------
# TERRAGRUNT CONFIGURATION
# Terragrunt is a thin wrapper for Terraform that provides extra tools for working with multiple Terraform modules,
# remote state, and locking: https://github.com/gruntwork-io/terragrunt
# ---------------------------------------------------------------------------------------------------------------------

locals {
# Global variables which are used throughout the infrastructure
global_vars = read_terragrunt_config(find_in_parent_folders("global.hcl"))

# Automatically load account-level variables
account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))

# Automatically load region-level variables
region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))

# Automatically load environment-level variables
environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
}

# Generate an AWS provider block
generate "provider" {
path = "provider.tf"
if_exists = "overwrite_terragrunt"
contents = <<EOF
provider "aws" {
region = "${local.aws_region}"

# Only these AWS Account IDs may be operated on by this template
allowed_account_ids = ["${local.account_id}"]
}
EOF
}

# Configure Terragrunt to automatically store tfstate files in an S3 bucket
remote_state {
backend = "s3"
config = {
encrypt = true
bucket = "${get_env("TG_BUCKET_PREFIX", "")}-tf-state-${local.aws_region}"
key = "${path_relative_to_include()}/terraform.tfstate"
region = local.aws_region
dynamodb_table = "terraform-locks"
}
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
}

# ---------------------------------------------------------------------------------------------------------------------
# GLOBAL PARAMETERS
# These variables apply to all configurations in this subfolder. These are automatically merged into the child
# `terragrunt.hcl` config via the include block.
# ---------------------------------------------------------------------------------------------------------------------

# Configure root level variables that all resources can inherit. This is especially helpful with multi-account configs
# where terraform_remote_state data sources are placed directly into the modules.
inputs = merge(
local.account_vars.locals,
local.region_vars.locals,
local.environment_vars.locals,
local.global_vars.locals,
)
Loading