In our previous blog post, we explored the basics of creating a Terraform script using Blocks and Resources. Today, we will delve deeper into Terraform resources and understand how they play a vital role in defining and managing components of your infrastructure.
Understanding Terraform Resources
Terraform resources represent key components of your infrastructure, such as physical servers, virtual machines, DNS records, S3 buckets, and more. These resources possess attributes that define their properties and behaviors, such as size, location, or domain name.
When defining a resource in Terraform, you specify its type, a unique name, and the attributes that characterize it. Terraform utilizes the resource block to define these resources within your Terraform configuration.
Setting Up main.tf
File and Provider Block
Before we start creating resources, let's set up the main.tf
file and define the Terraform block along with the provider block for AWS. ๐๏ธ
Step 1: Create main.tf
Create a file named main.tf
and open it in your preferred text editor.
Step 2: Define Terraform Block
Add the Terraform block to specify the required version of Terraform:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.19.0"
}
}
}
Step 3: Define Provider Block for AWS
Next, define the provider block for AWS. If you've configured AWS CLI using aws configure
on your system, Terraform will automatically use those credentials. Note: It's a best practice to use IAM roles or environment variables for your credentials rather than hardcoding them in your configuration files.
provider "aws" {
region = "ap-south-1"
}
Replace "ap-south-1"
with your desired AWS region.
Now that we have set up our Terraform configuration file and provider block, let's proceed to create a security group and an EC2 instance using Terraform. ๐ ๏ธ
Task 1: Create a Security Group
To allow traffic to the EC2 instance, a security group needs to be created. Let's follow these steps:
Step 1: Add Security Group Configuration
In your main.tf
file, add 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"]
}
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"]
}
}
Step 2: Initialize and Apply
Run terraform init
to initialize the Terraform project. Then, execute terraform apply
to create the security group. ๐ ๏ธ
Task 2: Create an EC2 Instance
Now, let's proceed to create an EC2 instance using Terraform:
Step 1: Add EC2 Instance Configuration
In your main.tf
file, append the following code to create an EC2 instance:
resource "aws_instance" "web_server" {
ami = "ami-0557a15b87f6559cf"
instance_type = "t2.micro"
key_name = "my-key-pair"
security_groups = [
aws_security_group.web_server.name
]
user_data = <<-EOF
bin/bash
sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
EOF
}
Note: Replace the ami
and key_name
values with your own. Refer to the AWS documentation for a list of available AMIs.
Step 2: Apply Configuration
Run terraform apply
to create the EC2 instance. ๐
Stay tuned for more exciting Terraform tutorials and insights on managing your infrastructure effortlessly! ๐