In this project, we will create a basic AWS infrastructure using Terraform. This project will help you understand how to apply the concepts learned in the previous modules to a real-world scenario. We will cover the following steps:
- Setting Up AWS Credentials
- Creating a VPC
- Creating Subnets
- Setting Up an Internet Gateway
- Creating Security Groups
- Launching EC2 Instances
- Outputting Important Information
- Setting Up AWS Credentials
Before we start, ensure you have your AWS credentials configured. You can set up your credentials using the AWS CLI or by creating a ~/.aws/credentials file.
Alternatively, you can create a ~/.aws/credentials file:
- Creating a VPC
First, let's create a new directory for our project and initialize it with Terraform.
Create a main.tf file and add the following code to define a VPC:
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "main-vpc"
}
}
- Creating Subnets
Next, we will create two subnets within our VPC.
resource "aws_subnet" "subnet1" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
tags = {
Name = "subnet1"
}
}
resource "aws_subnet" "subnet2" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
availability_zone = "us-west-2b"
tags = {
Name = "subnet2"
}
}
- Setting Up an Internet Gateway
We need an Internet Gateway to allow our instances to access the internet.
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "main-igw"
}
}
resource "aws_route_table" "rt" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "main-route-table"
}
}
resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.subnet1.id
route_table_id = aws_route_table.rt.id
}
resource "aws_route_table_association" "b" {
subnet_id = aws_subnet.subnet2.id
route_table_id = aws_route_table.rt.id
}
- Creating Security Groups
We will create a security group to allow SSH and HTTP access.
resource "aws_security_group" "web_sg" {
vpc_id = aws_vpc.main.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "web-sg"
}
}
- Launching EC2 Instances
Now, let's launch two EC2 instances in our subnets.
resource "aws_instance" "web1" {
ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI
instance_type = "t2.micro"
subnet_id = aws_subnet.subnet1.id
security_groups = [aws_security_group.web_sg.name]
tags = {
Name = "web1"
}
}
resource "aws_instance" "web2" {
ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI
instance_type = "t2.micro"
subnet_id = aws_subnet.subnet2.id
security_groups = [aws_security_group.web_sg.name]
tags = {
Name = "web2"
}
}
- Outputting Important Information
Finally, let's output the public IP addresses of our instances.
output "web1_public_ip" {
value = aws_instance.web1.public_ip
}
output "web2_public_ip" {
value = aws_instance.web2.public_ip
}Applying the Configuration
To apply the configuration, run the following commands:
Conclusion
In this project, we created a basic AWS infrastructure using Terraform. We set up a VPC, subnets, an Internet Gateway, security groups, and launched EC2 instances. This project demonstrated how to use Terraform to manage AWS resources and provided a foundation for more complex infrastructure setups.
Next Steps
- Experiment with adding more resources, such as RDS instances or S3 buckets.
- Explore using Terraform modules to organize your code better.
- Try deploying a simple web application on the EC2 instances.
By completing this project, you have gained practical experience in using Terraform to manage AWS infrastructure, which is a valuable skill for any cloud engineer or DevOps professional.
Terraform Course
Module 1: Introduction to Terraform
Module 2: Terraform Configuration Language
Module 3: State Management
Module 4: Terraform Modules
Module 5: Provisioning Resources
- Provisioning Basics
- Provisioning AWS Resources
- Provisioning Azure Resources
- Provisioning GCP Resources
Module 6: Advanced Terraform Features
Module 7: Terraform Best Practices
Module 8: Terraform in CI/CD
- Integrating Terraform with CI/CD
- Automating Terraform with Jenkins
- Using Terraform with GitHub Actions
- Terraform Cloud and Enterprise
