Automating AWS VPC Creation with Terragrunt: A Comprehensive Guide
Introduction:-
Creating a #Virtual Private Cloud (VPC) is a fundamental step in setting up a secure and scalable #AWS infrastructure. However, the manual configuration of VPCs can be complex and time-consuming. This guide explores the automation of #AWS VPC creation using #Terragrunt, a powerful tool that simplifies infrastructure provisioning. By leveraging #Terragrunt’s capabilities, you can streamline the process, enhance repeatability, and maintain better control over your #AWS network architecture.
Pre-requestites :-
Aws account with appropriate permissions
#Terraform and #Terragrunt should be installed on your local system
A dynamo-db table and s3 bucket should be created on your aws account.
Step-1 :- Create a folder with name vpc_terragrunt on your desktop. within the vpc_terragrunt folder create a file named terragrunt.hcl and paste the below code. update the file configuration (bucket,dynamodb_table,region) with your details.
# First u need to create a s3 bucket and dynamodb table for storing your terraform configuration files and update their names in the below bucket and dynamodb_table values
remote_state {
backend = "s3"
config = {
bucket = "YOUR-TERRAGRUNT-S3-BUCKET-NAME"
key = "${path_relative_to_include()}/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "YOUR-DYNAMODB-TABLE"
}
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
}
Step-2 :- Within the vpc_terragrunt folder create one more folder named vpc and a file named terragrunt.hcl then copy the below code into the file.
#terraform.hcl
terraform {
source = "tfr:///terraform-aws-modules/vpc/aws?version=3.14.0"
}
include "root" {
path = find_in_parent_folders()
}
locals {
region = "us-east-1"
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
public_subnets = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"]
cidr = "10.0.0.0/16"
}
inputs = {
name = "mahiratechnology-vpc"
cidr = local.cidr
azs = ["${local.region}a", "${local.region}b", "${local.region}c"]
private_subnets = local.private_subnets
public_subnets = local.public_subnets
map_public_ip_on_launch = false
enable_nat_gateway = true
single_nat_gateway = true
one_nat_gateway_per_az = false
public_route_tags = {
Name = "mahiratechnology-public-route"
}
private_route_table_tags = {
Name = "mahiratechnology-private-route"
}
nat_gateway_tags = {
Name = "mahiratechnology-nat-gateway"
}
nat_eip_tags = {
Name = "mahiratechnology-nat-gateway-eip"
}
// Enable DNS support and DNS hostnames
enable_dns_support = true
enable_dns_hostnames = true
}
After pasting the code, open the terminal window and navigate to your vpc directory. Configure your aws credentials and enter the first command terragruntinit for Initializing and after Initialization, enter the second command terragruntplan when the plan executed successfully,enter the third command terragruntapply.
Step-3 :- After deploying the code,login to your aws account and navigate to vpc service to view the resources.
Source-code :- “https://github.com/MahiraTechnology/Mahira-medium.git”
Conclusion :-
Automating #AWS VPC creation with #Terragrunt provides a significant advantage in terms of efficiency, consistency, and #version control. This guide has walked you through the essential steps, from understanding #AWS VPC concepts to implementing #infrastructure as code with #Terragrunt.
As you continue to explore infrastructure automation, remember that flexibility and scalability are at the core of #cloud architecture. #Terragrunt, combined with best practices for #AWS VPCs, empowers you to adapt to evolving requirements and maintain a robust and secure foundation for your #cloud-based applications. Happy coding!