Creating a Virtual Private Cloud (VPC) using Terraform allows you to define a logically isolated section of the AWS cloud where you can launch AWS resources. Below is a step-by-step guide on how to create a VPC using Terraform.
Step-by-Step Guide to Creating a VPC Using Terraform
Prerequisites
AWS Account: You need an AWS account with the necessary permissions to create VPCs.
Terraform Installed: Make sure Terraform is installed on your local machine. You can download it from the Terraform website.
AWS CLI Configured: Configure your AWS CLI with your credentials by running
aws configure
.
Step 1: Create a Directory for Your Terraform Project
Open your terminal and create a new directory for your Terraform project:
mkdir terraform-vpc
cd terraform-vpc
Step 2: Create a Terraform Configuration File
Create a file named main.tf
in your project directory. This file will contain the Terraform configuration to create a VPC. Here’s an example configuration:
# Specify the AWS provider
provider "aws" {
region = "us-east-1" # Change this to your preferred region
}
# Create a VPC
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16" # Change this to your desired CIDR block
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "MyVPC"
}
}
# Create an Internet Gateway
resource "aws_internet_gateway" "my_igw" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "MyInternetGateway"
}
}
# Create a public subnet
resource "aws_subnet" "my_public_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24" # Change this to your desired subnet CIDR block
availability_zone = "us-east-1a" # Change this to your preferred AZ
map_public_ip_on_launch = true
tags = {
Name = "MyPublicSubnet"
}
}
# Create a route table for the public subnet
resource "aws_route_table" "my_public_route_table" {
vpc_id = aws_vpc.my_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_igw.id
}
tags = {
Name = "MyPublicRouteTable"
}
}
# Associate the public subnet with the route table
resource "aws_route_table_association" "my_public_subnet_association" {
subnet_id = aws_subnet.my_public_subnet.id
route_table_id = aws_route_table.my_public_route_table.id
}
# Output the VPC ID
output "vpc_id" {
value = aws_vpc.my_vpc.id
}
# Output the public subnet ID
output "public_subnet_id" {
value = aws_subnet.my_public_subnet.id
}
Step 3: Initialize Terraform
In your terminal, run the following command to initialize Terraform:
terraform init
This command will download the necessary provider plugins.
Step 4: Review the Configuration
You can run the following command to see the execution plan and verify the resources that Terraform will create:
terraform plan
Step 5: Apply the Configuration
Once you’re satisfied with the plan, apply the configuration to create the VPC:
terraform apply
You will be prompted to confirm the action. Type yes
and hit Enter.
Step 6: Verify the VPC
After applying the configuration, you can verify that the VPC and its associated resources (internet gateway, public subnet, route table) have been created in the AWS Management Console under the VPC section.
Step 7: Clean Up Resources
To avoid incurring charges for the resources created, you can destroy the VPC and its associated resources by running:
terraform destroy
You will be prompted to confirm the destruction. Type yes
to proceed.
Conclusion
Creating a VPC using Terraform simplifies the process of managing network resources in AWS. This approach allows you to automate the provisioning of network infrastructure as part of your infrastructure as code strategy, ensuring that your environments are consistent and reproducible. As you become more familiar with Terraform, you can explore adding more complex configurations, such as private subnets, NAT gateways, and security groups, to suit your application needs.