Structuring VPC By Using Terragrunt — Part I

Lidor Ettinger
NI Tech Blog
Published in
2 min readOct 23, 2022

Terragrunt is a thin wrapper that provides extra tools for keeping your configurations DRY, working with multiple Terraform modules, and managing remote state. In this blog post I will show you how you can use Terragrunt to easily structure and configure your VPC environment on top of AWS.

This module assumes that you know the AWS VPC fundamentals. It will provision three tiers of subnets, public & private, and Elastic IPs for the VPC’s NAT Gateways.

In this post we will provision a VPC by using Terragrunt in which later we will use to provision an EKS cluster and a few services such as ALB Ingress, EFS etc.

The following infrastructure contains:

  • Environment variables → the variables to be used along the provisioning
  • Module → the VPC resource
  • Infra → the variables to be set to the module

Configure the AWS credentials

The first thing to be done is the AWS credentials. Before you do anything, make sure to check the aws CLI configure command.
The following example shows sample values:

$ aws configure
AWS Access Key ID [None]: YOUR-AWS-ACCESS-KEY-ID
AWS Secret Access Key [None]: YOUR-AWS-SECRET-KEY
Default region name [None]: us-west-2
Default output format [None]: json

Make a storage bucket

Create a unique Cloud Storage Bucket in order to store the infrastructure state that is made by Terragrunt:

export BUCKET="devops-workshop-$(uuidgen | tr "[:upper:]" "[:lower:]")"aws s3api create-bucket --bucket "${BUCKET}"

Define the environment variables module

To deploy the VPC module, create an environment variables of your infrastructure:

terragrunt
terragrunt.hcl

Inside of terragrunt.hcl, configure your AWS provider, the remote state and the Infrastructure settings:

Replace the <ACCOUNT_ID> with your account ID:

export ACCOUNT_ID="<ACCOUNT_ID>"

Define the VPC module

Configure the VPC resource that you are using:

terragrunt
modules
vpc
terragrunt.hcl

For example:

Setup the VPC resource of the infrastructure

Configure the variables of your VPC resource:

terragrunt
infra
└ vpc
terragrunt.hcl

For example:

Deploy the infrastructure

Full example can be found Here:

git clone https://github.com/naturalett/terragrunt.git
cd terragrunt
terragrunt init
cd infra/vpc
terragrunt apply

Congratulations, your VPC just got deployed. If you find yourself in a situation where Terraform is the engine of choice, and you want Infrastructure as a code, you cannot go wrong with Terragrunt.

What we will do next…

There are more resources and services to provision. In the next post I will share more infrastructure examples, such as:

  1. EKS & nodeGroups
  2. EFS & Metrics-server & Kubernetes-dashboard
  3. Airflow
  4. NLB & Ingress & ALB

--

--