Skip to content

Instantly share code, notes, and snippets.

@AlainODea
Last active June 6, 2024 02:17
Show Gist options
  • Save AlainODea/61ef3da56b27d8cc81490a5e70db2b64 to your computer and use it in GitHub Desktop.
Save AlainODea/61ef3da56b27d8cc81490a5e70db2b64 to your computer and use it in GitHub Desktop.
Terragrunt config to auto-generate provider and backend config so you can apply library modules directly in infrastructure-live without an adapter module in infrastructure-modules

Using Terragrunt generate for extra DRY Terraform

Terragrunt config to auto-generate provider and backend config so you can apply library modules directly in infrastructure-live without an adapter module in infrastructure-modules.

# ------------------------------------------------------------------------------
# ENVIRONMENT VARIABLES
# Define these secrets as environment variables
# ------------------------------------------------------------------------------
# AWS_ACCESS_KEY_ID
# AWS_SECRET_ACCESS_KEY
# ------------------------------------------------------------------------------
# TERRAGRUNT CONFIGURATION
# This is the configuration for Terragrunt, a thin wrapper for Terraform that supports locking and enforces best
# practices: https://github.com/gruntwork-io/terragrunt
# ------------------------------------------------------------------------------
# Terragrunt will copy the Terraform configurations specified by the source parameter, along with any files in the
# working directory, into a temporary folder, and execute your Terraform commands in that folder.
terraform {
source = "git::[email protected]:gruntwork-io/package-messaging.git//modules/sqs?ref=v0.3.1"
}
# Include all settings from the root terragrunt.hcl file
include {
path = find_in_parent_folders()
}
#dependency "kms_master_key" {
# config_path = "../../../_global/kms-master-key"
#}
inputs = {
name = "worker"
visibility_timeout_seconds = 60
message_retention_seconds = 86400
max_message_size = 131072
delay_seconds = 10
receive_wait_time_seconds = 20
dead_letter_queue = true
# kms_master_key_id = dependency.kms_master_key.outputs.remote_state.key_id
}
# These variables apply to this entire environment. They are automatically pulled in using the extra_arguments
# setting in the root terraform.tfvars file's Terragrunt configuration.
vpc_name: "dev"
# These variables apply to this entire AWS region. They are automatically pulled in using the extra_arguments
# setting in the root terraform.tfvars file's Terragrunt configuration.
aws_region: "ca-central-1"
# This is intentionally empty object. This YAML file is used as a catch all to return an empty map when expected yaml
# vars do not exist. Note that this file can not be completely empty because terragrunt's yamldecode function expects an
# object, and it doesn't treat empty files as an empty object (you get a syntax error).
{}
# ---------------------------------------------------------------------------------------------------------------------
# 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
# ---------------------------------------------------------------------------------------------------------------------
# Configure Terragrunt to automatically store tfstate files in an S3 bucket
remote_state {
backend = "s3"
config = {
encrypt = true
bucket = local.terraform_state_s3_bucket
key = "${path_relative_to_include()}/terraform.tfstate"
region = local.aws_region
dynamodb_table = "terraform-locks"
}
}
# ---------------------------------------------------------------------------------------------------------------------
# LOCAL PARAMETERS (just within this file)
# ---------------------------------------------------------------------------------------------------------------------
locals {
aws_region = "ca-central-1"
aws_account_id = "000000000000"
account_alias = "dev"
terraform_state_s3_bucket = "mycompany-${local.account_alias}-${local.aws_account_id}-terraform-state"
}
# ---------------------------------------------------------------------------------------------------------------------
# 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.
# ---------------------------------------------------------------------------------------------------------------------
inputs = merge(
# Configure Terragrunt to use common vars encoded as yaml to help you keep often-repeated variables (e.g., account ID)
# DRY. We use yamldecode to merge the maps into the inputs, as opposed to using varfiles due to a restriction in
# Terraform >=0.12 that all vars must be defined as variable blocks in modules. Terragrunt inputs are not affected by
# this restriction.
yamldecode(
file("${get_terragrunt_dir()}/${find_in_parent_folders("region.yaml", "${path_relative_from_include()}/empty.yaml")}"),
),
yamldecode(
file("${get_terragrunt_dir()}/${find_in_parent_folders("env.yaml", "${path_relative_from_include()}/empty.yaml")}"),
),
# Additional global inputs to pass to all modules called in this directory tree.
{
aws_account_id = local.aws_account_id
terraform_state_s3_bucket = local.terraform_state_s3_bucket
terraform_state_aws_region = local.aws_region
},
)
# When using this terragrunt config, terragrunt will generate the file "provider.tf" with the aws provider block before
# calling to terraform. Note that this will overwrite the `provider.tf` file if it already exists.
generate "provider" {
path = "provider.tf"
if_exists = "skip" # Allow modules to override provider settings
contents = <<EOF
provider "aws" {
# The AWS region in which all resources will be created
region = "${local.aws_region}"
# Only these AWS Account IDs may be operated on by this template
allowed_account_ids = ["${local.aws_account_id}"]
version = "~> 2.50.0"
}
provider "external" {
version = "~> 1.2"
}
provider "null" {
version = "~> 2.1"
}
provider "template" {
version = "~> 2.1"
}
EOF
}
generate "backend" {
path = "backend.tf"
if_exists = "overwrite"
contents = <<EOF
terraform {
backend "s3" {}
}
EOF
}
@kosperera
Copy link

The outputs of the dependency block doesn't seem to work for terragrunt run-all plan command. How did you workaround / manage to get it to work?

@kosperera probably better to ask Gruntwork that question. I do not use run-all plan myself.

Looks like there is already an open conversation about outputs at gruntwork-io/terragrunt#1330

I tried terragrunt plan and the error seems to be the same. Do you have a public repo on this example that works?

@AlainODea
Copy link
Author

The outputs of the dependency block doesn't seem to work for terragrunt run-all plan command. How did you workaround / manage to get it to work?

@kosperera probably better to ask Gruntwork that question. I do not use run-all plan myself.

Looks like there is already an open conversation about outputs at gruntwork-io/terragrunt#1330

I tried terragrunt plan and the error seems to be the same. Do you have a public repo on this example that works?

The kms_master_key dependency is missing. There's no terragrunt.hcl for it. Best bet for now is to exclude it and the kms_master_key_id.

@AlainODea
Copy link
Author

@kosperera I've commented out the kms_master_key dependency and the kms_master_key_id input. Hopefully it will plan for you now.

@AlainODea
Copy link
Author

This still depends on private code you need a Gruntwork subscription to use so it's not likely to be practical to use other than a conceptual example.

@kosperera
Copy link

@kosperera I've commented out the kms_master_key dependency and the kms_master_key_id input. Hopefully it will plan for you now.

Thanks, @AlainODea. I was looking for a decent example that works with the dependency block. So far terragrunt plan doesn't work as expected outputs for me, so wondered whether it's just my code.

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