Day 65 of 90 Days of DevOps Challenge- Working with Terraform Resources 🚀
Today, we’re diving deeper into Terraform resources. Understanding how to effectively use resources is crucial for building and managing your infrastructure efficiently.
Understanding Terraform Resources
In Terraform, a resource represents a component of your infrastructure, such as:
Physical servers
Virtual machines
DNS records
S3 buckets
Each resource has attributes that define its properties and behaviors, such as size, location, and other configurations. When you define a resource in Terraform, you specify:
Type of resource (e.g.,
aws_instance
,aws_security_group
)Unique name for the resource
Attributes that define the resource (like AMI ID, instance type, etc.)
Terraform uses the resource block to define these resources in your configuration.
Task 1: Create a Security Group
To allow traffic to your EC2 instance, you’ll first need to create a security group. Here’s how you can do it:
Add the Security Group Resource
In yourmain.tf
file, include the following code 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"] } }
name_prefix: Specifies a prefix for the security group name.
ingress: Defines the inbound traffic rules. Here, we’re allowing HTTP traffic (port 80) from any IP address (
0.0.0.0/0
).
Initialize the Terraform Project
Run the following command to initialize your Terraform project:terraform init
Apply the Configuration
Execute the following command to create the security group:terraform apply
Task 2: Create an EC2 Instance
With the security group in place, you can now create an EC2 instance. Follow these steps:
Add the EC2 Instance Resource
In yourmain.tf
file, add the following code to create an EC2 instance:resource "aws_instance" "web_server" { ami = "ami-0557a15b87f6559cf" # Replace with your desired AMI instance_type = "t2.micro" # Instance type key_name = "my-key-pair" # Replace with your key pair name security_groups = [ aws_security_group.web_server.name ] user_data = <<-EOF #!/bin/bash echo "<html><body><h1>Welcome to my website!</h1></body></html>" > index.html nohup python -m SimpleHTTPServer 80 & EOF }
ami: Amazon Machine Image ID for the instance. Replace this with your desired AMI ID.
instance_type: Specifies the type of EC2 instance. Here, we're using
t2.micro
, which is eligible for the AWS free tier.key_name: Name of the key pair you will use to access the instance.
security_groups: Associates the security group created earlier with this EC2 instance.
user_data: Contains the script that will run on instance initialization. In this example, it creates a simple HTML page served by a Python HTTP server.
Apply the Configuration
Run the following command to create the EC2 instance:terraform apply
Task 3: Access Your Website
Once your EC2 instance is up and running, you can access the website hosted on it.
Obtain the Public IP Address
After runningterraform apply
, note the public IP address of your EC2 instance from the output.Open a Web Browser
Enter the public IP address into your web browser:
http://<your-ec2-public-ip>
You should see a simple webpage with the message: "Welcome to my website!"
Conclusion
In this lesson, you learned how to create and manage resources using Terraform. By creating a security group and an EC2 instance, you can host a simple website on AWS. This foundational knowledge of Terraform resources will allow you to build more complex infrastructures in future tasks. Keep exploring and building!