Day 65: Working with Terraform Resources 🚀
Day#65 Of 90 Days Of DevOps Challenge

Experienced QA professional with a passion for manual and automation testing. Proficient in DevOps practices, ensuring seamless integration and continuous delivery. Dedicated to ensuring top-notch software quality and efficiency. Eager to contribute my skills to Hashnode's vibrant tech community. Let's collaborate and create exceptional experiences!
👋Introduction
In the world of infrastructure as code (IAC), Terraform stands as a titan.
Its power and flexibility allow users to define and manage infrastructure in a declarative way.
At the core of Terraform's strength lies its concept of "resources."
In this blog post, we'll delve into what Terraform resources are, and we'll take you through the process of creating an Amazon Web Services (AWS) security group and an EC2 instance as practical examples.
❄Terraform Resources: The Building Blocks of Infrastructure
In Terraform, a resource is a fundamental unit that represents a component of your infrastructure.
These components can range from a physical server, a virtual machine, a DNS record, to an S3 bucket.
Resources are the core entities that Terraform can create, update, or destroy.
Every resource comes with attributes that define its properties and behaviors. For instance, a virtual machine resource may have attributes specifying its size, location, and other configuration details.
Similarly, a DNS record resource will have attributes like domain name and record type.
To define a resource in Terraform, you use the resource block in your Terraform configuration.
It allows you to specify the type of resource, provide a unique name for it, and set the attributes that define the resource. Let's explore this with a practical example.
📜Task 1: Creating a Security Group
Step 1: Define the Security Group Resource
For our example, we'll create an AWS security group to allow traffic to an EC2 instance.
In your main.tf file, you can add the following Terraform code snippet:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "ap-south-1"
}
resource "aws_security_group" "ec2_web_server" {
name_prefix = "web-server-sg"
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"]
}
}
This code defines an AWS security group resource named day65_web_server. It allows incoming traffic on port 80 (HTTP) from any source IP (0.0.0.0/0).
Step 2: Initialization and Creation
Once your main.tf is ready, you can initialize the Terraform project by running:
terraform init
terraform plan
This command downloads the necessary plugins and sets up the project.



To create the security group, run:
terraform apply
This will prompt you to confirm the changes you are about to make. After confirming, Terraform will create the security group based on your configuration.


Step 3: Verification
To verify that the security group has been created successfully, you can check your AWS console or use Terraform commands to inspect the resources it manages.

📃Task 2: Creating an EC2 Instance
With the security group in place, let's proceed to create an EC2 instance.
Step 1: Define the EC2 Instance Resource
In your main.tf file, add the following Terraform code to create an EC2 instance:
resource "aws_instance" "web_server" {
ami = "ami-0f5ee92e2d63afc18"
instance_type = "t2.micro"
key_name = "aws-keys"
security_groups = [
aws_security_group.ec2_web_server.name
]
tags = {
Name= "day65terraforminstance"
}
user_data = <<-EOF
#!/bin/bash
sudo apt update -y &&
sudo apt install -y nginx
echo "Hello World" > /var/www/html/index.html
EOF
}
This code defines an AWS EC2 instance resource named web_server.
It specifies attributes such as the Amazon Machine Image (AMI), instance type, key pair for SSH access, associated security group, and user data to configure the instance.
Note: You should replace the
amiandkey_namevalues with appropriate values from your AWS environment.
Step 2: Creating the EC2 Instance
To create the EC2 instance, run:
terraform apply
Terraform will execute the plan and provision the EC2 instance based on your configuration.



📑Task 3: Accessing Your Website
Now that your EC2 instance is up and running, you can access the website you've hosted on it. Here's how:
Open a web browser and enter the public IP or DNS name of your EC2 instance.
You should see your website's content displayed in your browser.

💥Conclusion
In this blog post, we've explored the core concept of Terraform resources and walked through the process of creating an AWS security group and an EC2 instance.
With Terraform, you can easily define, manage, and scale your infrastructure by leveraging these powerful building blocks.
The possibilities are limitless, and your infrastructure-as-code journey has only just begun. Happy Terraforming!
Happy learning & may your DevOps journey be filled with exciting discoveries🎊
Thank you for joining us on this exciting Day 65 of the 90 Days of DevOps challenge. I hope you found the information helpful and insightful.💥
So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :
Hashnode: vishaltoyou.hashnode.dev
LinkedIn: linkedin.com/in/vishalphadnis




