๐ Unleashing the Power of Terraform Resources ๐
Day 65 : #90DaysOfDevOps Challange
Welcome back to our Terraform journey! In the previous blog post, we learned about Terraform blocks and resources. Today, we will take a deeper dive into working with Terraform resources and continue building our infrastructure. So, grab your cup of โ and let's get started!
Understanding Terraform Resources ๐
In the world of Terraform, a resource represents a crucial component of your infrastructure. It could be anything from a physical server to a virtual machine, a DNS record, or even an S3 bucket. These resources come with their unique set of attributes that define their properties and behaviors. For instance, a virtual machine resource might have attributes like size and location, while a DNS record resource could have attributes like domain name.
To define a resource in Terraform, you need to specify the resource type, a unique name, and the attributes that characterize it. In your Terraform configuration, you'll utilize the resource block to define these resources and bring your infrastructure to life.
Task 1: Create a security group ๐ฅ
To kick things off, we need to create a security group to allow traffic to our EC2 instance. Here's a step-by-step guide:
- Open your
main.tf
file and add the following code snippet to create a security group:
resource "aws_security_group" "web_server" {
name_prefix = "web-server-sg"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
Run
terraform init
in your terminal to initialize the Terraform project.Execute
terraform apply
to create the security group.
Task 2: Create an EC2 instance ๐ป
Now, let's move on to creating an EC2 instance using Terraform. Follow these steps:
- Open your
main.tf
file and add the following code snippet to create an EC2 instance:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
// main.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_security_group" "web_server" {
name_prefix = "web-server-sg"
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"]
}
ingress {
from_port = 443
to_port = 443
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"]
}
}
resource "aws_instance" "web_server" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
key_name = "myself"
security_groups = [
aws_security_group.web_server.name
]
tags = {
Name = "web_server"
}
user_data = <<-EOF
#!/bin/bash
sudo apt update -y
sudo apt install python3 -y
sudo apt-get install -y apache2
sudo systemctl start apache2
sudo systemctl enable apache2
sudo apt
touch /home/ubuntu/index.html
echo "<html><body><h1>Welcome to my website!</h1></body></html>" > /home/ubuntu/index.html
nohup python3 -m SimpleHTTPServer 80 &
sudo chmod -R 755 /var/www/html
EOF
provisioner "file" {
source = "/home/ubuntu/index.html"
destination = "/var/www/html/index.html"
}
}
Note: Replace the ami
and key_name
values with your own. You can find a list of available AMIs in the AWS documentation.
- Run
terraform apply
to create the EC2 instance.
Task 3: Access your website ๐
With the EC2 instance up and running, it's time to access the website hosted on it. Follow these simple steps:
Open your favorite web browser and enter the public IP address of your EC2 instance.
Voilร ! You should see your website's homepage with the message "Welcome to my website!" displayed prominently.
Congratulations! You have successfully created a security group, launched an EC2 instance, and accessed your website using Terraform. ๐
In today's blog post, we explored the power of Terraform resources and witnessed firsthand how they shape our infrastructure. Stay tuned for the next article, where we'll delve into managing dependencies between resources and further enhance our infrastructure. Until then, happy coding! ๐ปโจ