AWS VPC Creation with Terraform Modules

Maha Lakshmi

--

In this blog, we will first explore how to create a Virtual Private Cloud (VPC) in AWS using Terraform. This will help you configure various network components such as VPC, subnets, Internet Gateway, NAT Gateway, route tables, and associations.

  • We’ll create a directory structure for the VPC module, allowing reusable Terraform components for easier configuration.
  • Here, terraform-aws-vpc is a module
  • Directory Structure of the Module — terraform-aws-vpc

Step 1: Define variables (variables.tf)

To make our Terraform configuration reusable, we define variables for dynamic values, such as VPC CIDR blocks, subnet CIDRs, and environment names.

  • The VPC CIDR (10.0.0.0/16) defines the IP range for your VPC.
  • Public, private, and database subnets are expected to have 2 CIDRs each.

Step 2: Data Sources (data.tf)

  • In data.tf, you retrieve data from AWS to use within the Terraform configuration.
  • For example, fetching available availability zones (AZs) dynamically.

Step 3: Local Variables (locals.tf)

locals.tf helps simplify complex configurations by consolidating variables and calculated values.

Step-4: Resource creation

4.1: Creation of VPC
Create the VPC resource in main.tf

4.2: Creation of Internet Gateway
To allow public access, create an Internet gateway

4.3: Creation of subnets
We create three types of subnets: public, private, and database subnets.
4.3.1: public subnet

4.3.2: private subnet

4.3.3: database subnet

4.3.4: Database Subnet Group (for RDS)

4.4: Elastic IP Creation
Create an elastic IP for the NAT gateway

4.5: NAT Gateway Creation
A NAT Gateway allows instances in private subnets to access the internet

4.6: Route Table Creation
We create separate route tables for each subnet type.
4.6.1: public route table

4.6.2: private route table

4.6.3: database route table

4.7: Adding Rules for Route Tables

4.7.1: Public Route Table: Adds a route for internet access via the Internet Gateway.

4.7.2: Private Route Table: Adds a route for internet access via the NAT Gateway.

4.7.3: Database Route Table: Adds a route for internet access via the NAT Gateway.

4.8: Route Table Associations
Associate route tables with their corresponding subnets.

4.8.1: public route table association

4.8.2: private route table association

4.8.3: database route table association

4.9: outputs.tf
Define output values for the VPC module

What is module deployment?

It means breaking down your infrastructure setup into reusable pieces of code called modules. Instead of writing the same code repeatedly, you can create a module for a specific task (like creating a VPC or an EC2 instance) and then use that module whenever needed.

What’s the use of modules?

  1. Reusability: You can use the same module multiple times for different environments (like dev, prod).
  2. Simplicity: Modules help you manage complex infrastructure setups by dividing them into smaller, easier-to-manage parts.
  3. Consistency: Using modules ensures that your infrastructure is set up the same way every time.

Here, we are using the terraform-aws-vpc module
Create a separate directory for testing your terraform-aws-vpc module and name it vpc-module-test.
Directory Structure for vpc-module-test

Step-1: provider.tf

  • Configure the provider in provider.tf
  • Here, I am using backend state S3, AWS provider and region us-east-1

Step-2: Define variables

Define any variables needed for the test in variables.tf, similar to the module.

Step-3: vpc.tf

  • In vpc.tf, invoke the VPC module
  • In this, source can be from git repository Or else you can take from the local machine. But the best approach is using the git repository source

Step-4: outputs.tf

Define output values in outputs.tf to display results of the created VPC

For more details, please refer to my GitHub account:
https://github.com/lakshmimungara/terraform-aws-vpc

Conclusion

By following these steps, you can successfully create a modular VPC in AWS using Terraform. This approach not only promotes reusability but also enhances the maintainability of your infrastructure code. You can customize the parameters as needed to fit your specific requirements.

It’s possible when you believe in yourself! You can do it.

Happy learning!!!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Maha Lakshmi
Maha Lakshmi

Written by Maha Lakshmi

I'm Maha Lakshmi. I am passionate about DevOps, cloud computing, and automation. I share insights on Medium and GitHub with hands-on examples and concepts.

No responses yet

Write a response