Building a VPC and provisioning an EC2 instance using Modules!
Journey: 📊 Community Builder 📊
Subject matter: Building on AWS
Task: Building a VPC and provisioning an EC2 instance using Modules!
This week, I used Terraform to create a simple VPC with a single EC2 instance provisioned inside it using Modules.
In this scenario, I configured the VPC using a Terraform Module. This was stacked with a root module and a child module. The child module generated the VPC and the EC2 instance when the root called for it.
Resource credit: This architecture was created using guidance from Joseph Peter Here.
What did I use to build this environment?
- Visual Studio Code platform
- Terraform
- AWS CLI
- AWS Management Console
What is built?
- A single VPC.
- A Public Subnet.
- Custom Route Tables.
- NACLs and Security Groups.
- An EC2 instance.
In this task, I used Terraform modules. Modules are a great way to create reusable blocks of config and in this project, I used a child module to create a simple VPC with an EC2 instance provisioned inside it.
When the root module calls the child module, the VPC is built out from the handlers and variables in the config. In more complex Terraform configurations, multiple pre-configured modules can be called to build the environment. Once you have the modules setup and ready, all your root configuration needs to do is call the child modules in order and your setup is built accordingly.
In this scenario, the root module called the child EC2 module and the result was a VPC with an instance inside it.
Modules are the main ingredient in writing reusable, maintainable, and testable Terraform code and I have been enjoying learning about how to put them together.
Some of the highlights…
Terraform structure:
Terraform init:
Terraform module code:
Terraform completed:
VPC resource map:
Subnet:
EC2 instance:
Terraform destroy:
My interpretation of the architecture:
I hope you have enjoyed the article!