Project: Building an entire infrastructure through Terraform
#day60 of #90daysofdevops
Table of contents
- Introduction:)
- Task-1: Create a VPC (Virtual Private Cloud) with CIDR block 10.0.0.0/1
- Task-2: Create a public subnet with CIDR block 10.0.1.0/24 in the above VPC.
- Task-3: Create a private subnet with CIDR block 10.0.2.0/24 in the above VPC.
- Task-4: Create an Internet Gateway (IGW) and attach it to the VPC.
- Task-5: Create a route table for the public subnet and associate it with the public subnet. This route table should have a route to the Internet Gateway.
- Task-6: Launch an EC2 instance in the public subnet with the following details:
- AMI: ami-0557a15b87f6559cf
- Instance type: t2.micro
- Task-7: Security group: Allow SSH access from anywhere
- Task-8: User data: Use a shell script to install Apache and host a simple website
- Task-9: Create an Elastic IP and associate it with the EC2 instance.
- Connect with me:)
Introduction:)
Welcome to #Day60 of #90DaysOfDevOps journey!
Welcome back to our Terraform journey!
In the previous tasks, you've delved into the basics of Terraform, understanding its configuration files, and successfully crafted an EC2 instance. Today, we're taking it a step further as we dive deeper into Terraform's capabilities. Get ready to explore the world of Infrastructure as Code (IaC) techniques, empowering you to effortlessly build and manage multiple AWS resources. Let's embark on this Terraform adventure together!
Lets Start:
Prerequisites:)
Below are the prerequisites for building any aws infrastructure is to define providers.
Set Up Your Terraform Configuration
terraform { required_providers { aws = { source = "hashicorp/aws" version = "5.19.0" # Use the latest version } } }
Set up the Provider block
provider "aws" { region = "us-east-1" # Replace with your desired AWS region }
NOTE: I used the terraform apply in the last
Task-1: Create a VPC (Virtual Private Cloud) with CIDR block 10.0.0.0/1
Define the VPC Resource
Add the VPC resource block to your
aws_vpc.tf
file. In this example, we'll use the CIDR block 10.0.0.0/16 for the VPC.resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16" tags = { Name = "main" } }
Initialize and Apply Terraform Configuration
Run the following commands to initialize Terraform and apply the configuration:
terraform init terraform plan terraform apply
Task-2: Create a public subnet with CIDR block 10.0.1.0/24 in the above VPC.
Define the subnet resource
Add the Subnet resource block to your
aws_subnet.tf
fileresource "aws_subnet" "public_subnet" { vpc_id = aws_vpc.main.id cidr_block = "10.0.1.0/24" tags = { Name = "PublicSubnet" } }
Plan Terraform Configuration
Task-3: Create a private subnet with CIDR block 10.0.2.0/24 in the above VPC.
Update Terraform (
aws_subnet.tf
) ConfigurationOpen your existing
aws_subnet.tf
file and add the configuration for the public subnet:resource "aws_subnet" "private_subnet" { vpc_id = aws_vpc.main.id cidr_block = "10.0.2.0/24" tags = { Name = "PrivateSubnet" } }
Plan Terraform Configuration
Task-4: Create an Internet Gateway (IGW) and attach it to the VPC.
Create a internetgateway.tf file and define the internet gateway with the required configurations to attach it to VPC
resource "aws_internet_gateway" "my_igw" { vpc_id = aws_vpc.main.id tags = { Name = "MyInternetGateway" } }
Plan Terraform Configuration
Task-5: Create a route table for the public subnet and associate it with the public subnet. This route table should have a route to the Internet Gateway.
Define the Routetable resource
Create a
aws_routetable.tf
file to define the route table configuration in association with the public subnet.resource "aws_route_table" "public" { vpc_id = aws_vpc.main.id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.my_igw.id } tags = { Name = "PublicRouteTable" } } resource "aws_route_table_association" "public_subnet" { subnet_id = aws_subnet.public_subnet.id route_table_id = aws_route_table.public.id }
Plan Terraform Configuration
Task-6: Launch an EC2 instance in the public subnet with the following details:
AMI: ami-0557a15b87f6559cf
Instance type: t2.micro
resource "aws_instance" "public_instance" {
ami = "ami-0557a15b87f6559cf"
instance_type = "t2.micro"
subnet_id = aws_subnet.public_subnet.id
vpc_security_group_ids = [aws_security_group.web_server.id]
tags = {
Name = "PublicEC2Instance"
}
}
Task-7: Security group: Allow SSH access from anywhere
Define the resource block
Create a
aws_securitygroup.tf
file and define the security groupresource "aws_security_group" "web_server" { name_prefix = "web-server-sg" vpc_id = aws_vpc.main.id ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { from_port = 22 to_port = 22 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"] } }
Plan Terraform Configuration
Task-8: User data: Use a shell script to install Apache and host a simple website
Below is a simple shell script that you can use to install Apache and host a basic HTML website. You can include this script as the
user_data
when launching an EC2 instance to automate the setup.resource "aws_instance" "public_instance" { ami = "ami-0557a15b87f6559cf" instance_type = "t2.micro" subnet_id = aws_subnet.public_subnet.id vpc_security_group_ids = [aws_security_group.web_server.id] tags = { Name = "PublicEC2Instance" } user_data = <<-EOF #!/bin/bash sudo apt update -y sudo apt install apache2 -y echo "<html><body><h1>Welcome to My Website!</h1></body></html>" | sudo tee /var/www/html/index.html sudo systemctl start apache2 sudo systemctl enable apache2 EOF }
Task-9: Create an Elastic IP and associate it with the EC2 instance.
Create an Elastic IP and associate it with the EC2 instance.
resource "aws_eip" "eip" { instance = aws_instance.public_instance.id vpc = true tags = { Name = "elastic-ip" } }
Plan Terraform Configuration
Verifications:)
terraform apply
Type "yes" to confirm the changes.
Verify the VPC in AWS Console
Verify the Public Subnet
Verify the Public Subnet
Verify the Internet Gateway
Verify the Route Table
Verify the EC2 instance
Verify the Elastic IP
Verify Security Group
Open the website URL in a browser to verify that the website is hosted successfully.
Down all the infrastructure
terraform destroy
Type "yes" to confirm the changes.
Connect with me:)
Thank you for diving into this blog with me! I trust you found the information both helpful and enlightening. To stay updated on the latest in DevOps ๐, make sure to follow me. Remember, staying informed means staying ahead in the dynamic world of DevOps!
Feel free to connect with me on:
For more updates and engaging discussions on DevOps, let's connect! ๐ #DevOpsCommunity