First Terraform Script
0 198
๐ Introduction to Your First Terraform Script
Terraform is a popular Infrastructure as Code (IaC) tool developed by HashiCorp. It enables you to define and provision infrastructure using a high-level configuration language called HCL (HashiCorp Configuration Language). In this blog, weโll walk through creating your first Terraform script to provision a basic EC2 instance on AWS. If youโre just starting out, this will give you hands-on experience with the Terraform workflow.
๐ ๏ธ Prerequisites
- Terraform installed on your local machine (
terraform -v
to verify) - A configured AWS CLI or AWS credentials in
~/.aws/credentials
- Basic knowledge of AWS (specifically EC2)
๐ Step 1: Create the Project Directory
Start by creating a new directory for your project and navigate into it:
mkdir first-terraform-script
cd first-terraform-script
๐ Step 2: Write Your First Terraform Script
Create a file named main.tf
. This will contain the core configuration for provisioning an EC2 instance:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI (Change if needed)
instance_type = "t2.micro"
tags = {
Name = "MyFirstEC2"
}
}
๐ What Does This Script Do?
- provider โ Specifies AWS as the cloud provider and defines the region.
- aws_instance โ Provisions an EC2 instance using the specified AMI and instance type.
- tags โ Adds a tag to the instance for easy identification.
๐งช Step 3: Initialize the Project
Before Terraform can be used, it needs to initialize the working directory to download provider plugins.
terraform init
๐ Step 4: Preview the Execution Plan
Run the following command to see what Terraform plans to do:
terraform plan
This step is crucial because it allows you to verify the resources that will be created before actually applying the changes.
โ๏ธ Step 5: Apply the Terraform Script
Once you're satisfied with the plan, apply it to create the resources:
terraform apply
Terraform will prompt for confirmation. Type yes
to proceed.
๐งน Step 6: Clean Up Resources
To avoid incurring AWS charges, you should destroy the resources once you're done testing:
terraform destroy
๐ง Tips for Beginners
- Always run
terraform plan
beforeapply
. - Use
terraform fmt
to format your code. - Use version control (like Git) to manage your configuration files.
๐ Security Note
Avoid hardcoding sensitive information like AWS access keys into your Terraform files. Use environment variables, the AWS CLI, or AWS Vault for secure credential management.
๐ Summary
Congratulations! You've successfully written and executed your first Terraform script. You've learned how to set up a basic configuration, initialize a Terraform project, preview changes, apply infrastructure, and clean up afterward. As you continue exploring Terraform, try expanding your script by adding resources like VPCs, security groups, or S3 buckets. The more you practice, the more powerful and efficient your cloud infrastructure management will become.
If youโre passionate about building a successful blogging website, check out this helpful guide at Coding Tag โ How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments