DevOps Challenges Series #1 - Terraform resources
This challenge uses Terraform to build your own AWS resources
Hey DevOps enthusiasts! 🚀 If you're anything like me, you're always on the hunt for ways to automate and streamline cloud operations. One tool that I think is a game-changer for this is Terraform. In today's article, we're diving deep into three hands-on exercises to master AWS resource management using Terraform. We're going to play around with S3 buckets, set up our own VPC, and even launch an EC2 instance. So, gear up, folks!
Here is the task from Tech-Vault. Here is the link to the question
https://github.com/moabukar/tech-vault/tree/main/hands-on#6-terraform-test
## Terraform Exercise 1: Create an AWS S3 Bucket
### Objective
Your task is to write a Terraform script that does the following:
- **Step 1**: Create an AWS S3 bucket named `tf-test-bucket-yourname`.
- **Step 2**: Enable versioning on the S3 bucket.
- **Step 3**: Create a folder within that S3 bucket and name it `uploads`.
---
## Terraform Exercise 2: Set up a VPC
### Objective
Write a Terraform script to set up a Virtual Private Cloud (VPC) in AWS with the following:
- **Step 1**: Create a VPC with CIDR block `10.0.0.0/16`.
- **Step 2**: Create a subnet in that VPC with CIDR block `10.0.1.0/24`.
- **Step 3**: Attach an internet gateway to the VPC.
## Terraform Exercise 3: Managing EC2 Instances
### Objective
Your objective is to create a Terraform script that automates the following:
- **Step 1**: Launch an EC2 instance with type `t2.micro`.
- **Step 2**: Make sure to use an Amazon Linux 2 AMI.
- **Step 3**: Tag the instance with "Environment: Dev".
Now, let’s solve it!
Exercise 1: The S3 Adventure
Objective
Let's kick things off by creating an S3 bucket. An S3 bucket is basically your personal storage space on AWS where you can store files, images, and more.
Your Tasks:
Create an S3 bucket.
Enable versioning on the bucket.
Add an
uploads
folder.
Steps
Write the Script: Open your text editor and create a new file called
s3_bucket.tf
. Here's a simple Terraform script to meet the objectives.
resource "aws_s3_bucket" "my_bucket" {
bucket = "tf-test-bucket-yourname"
acl = "private"
versioning {
enabled = true
}
}
resource "aws_s3_bucket_object" "my_folder" {
bucket = aws_s3_bucket.my_bucket.id
key = "uploads/"
source = "/dev/null"
}
Run the Script: Navigate to
/home/ubuntu/q6
and runterraform apply
. This should execute your Terraform script and create the S3 bucket.
Insights
Enabling versioning on an S3 bucket is a neat trick to keep track of changes made to the objects. It's like having a time machine for your S3 bucket.
Exercise 2: Building Your Own VPC
Objective
A VPC (Virtual Private Cloud) gives you a private slice of the AWS cloud where you can launch resources. Your mission is to set up your own VPC.
Your Tasks:
Create a VPC.
Add a subnet.
Attach an internet gateway.
Steps
Write the Script: In your text editor, create a file named
aws_vpc.tf
.
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "my_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
}
resource "aws_internet_gateway" "my_gw" {
vpc_id = aws_vpc.my_vpc.id
}
Run the Script: Navigate to /home/ubuntu/q6
and execute terraform apply
.
Creating a VPC lets you have a private playground in AWS, sort of like your own private room in a big mansion!
Exercise 3: EC2 for the Win
Objective
EC2 (Elastic Compute Cloud) instances are basically virtual servers. You'll be launching your own EC2 instance.
Your Tasks:
Launch an EC2 instance.
Use Amazon Linux 2 AMI.
Add a tag "Environment: Dev".
Steps
Write the Script: Create a file named
ec2_instance.tf
.
resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Environment = "Dev"
}
}
Run the Script: Move to /home/ubuntu/terraform-tech-vault
and run terraform apply
.
Conclusion
Alright, folks, that's a wrap for today! If you've been following along, you now have a new S3 bucket, your very own VPC, and a shiny EC2 instance running in AWS—all thanks to Terraform! Isn't automation just fantastic?