Mastering Infrastructure as Code: A Comprehensive Guide to Terraform on AWS
Note: Terraform is more compatible with Linux than Windows.
There are two ways to create this :
You can create this either by using an EC2 instance on AWS or by using a local Linux installation on your PC.
i will demonstrate using ec2 instance
First, go to the AWS console, type "EC2" in the search bar, select a region close to you, click "Launch Instance," and name the EC2 according to your needs.
Next, select the image of the EC2, which means choosing the type of OS; in my case, I will select Ubuntu, and I suggest checking if you selected the free tier, otherwise, you will be charged a lot.
and select the instance type, making sure to choose only the free tier eligible option.
Next, you need to create a key pair, which is like a key to unlock your house; similarly, you need a key pair to log in to the EC2 instance.
Give the key pair a name of your choice and select the .pem key pair type, but if you are using macOS or other Linux flavors, choose .ppk and click on create key pair.
After verifying the details and checking the image below, click on "Launch Instance."
Then, enter the EC2 instance you created, click on "Connect," go to the SSH client section, and copy the example link to log in to the EC2 instance.
To access the EC2 instance, I am using Git Bash, but you can also connect manually through the various connection options in AWS, then navigate to the key pair location where you saved it and paste the example link you copied in the previous step.
and then hit enter; you will see a step to verify the fingerprint where you should type "yes" to enter the VM, otherwise, you will be rejected.
Hurray, now we are successfully connected to the EC2.
and then you need to download Terraform to perform Terraform operations and the AWS CLI to access AWS remotely
Go to the Terraform documentation for installing Terraform on Linux.
link : https://developer.hashicorp.com/terraform/install
Create a folder to store the Terraform file. and terraform has four steps terraform init
: for initializing Terraform
terraform plan
: This command plans and communicates with AWS through the AWS CLI, and if there are no errors, it will display none; the ultimate goal of Terraform is to create or manage resources without using the AWS UI, so we will write Terraform code to create a resource here.
terraform apply
: After the plan stage, this is where the code you wrote will be executed, and the specified operation or resource will be performed or created.
terraform destroy
: This stage allows us to delete what we have created by writing the code, for example, if we created an instance through Terraform, we can delete it directly without going to the AWS UI.
Now we will create an EC2 instance using a Terraform script; let's see the images.
Terraform Script is
After the Apply phase u need to click yes if u click no the script will be not executed
At this phase, the script is successfully executed, and you can go to the AWS console UI to see that the instance has been created using the Terraform script.
Conclusion :
In conclusion, utilizing Infrastructure as Code (IaC) with Terraform offers a streamlined and efficient approach to managing and provisioning your cloud infrastructure. By following the steps outlined in this guide, you can easily set up and configure an EC2 instance on AWS, automate the deployment process, and ensure consistency across your environments. Terraform's powerful scripting capabilities allow for scalable and repeatable infrastructure management, reducing the risk of human error and enhancing operational efficiency. Whether you are a beginner or an experienced professional, mastering Terraform will significantly improve your cloud infrastructure management and deployment processes.
Subscribe to my newsletter
Read articles from Hema Sundharam Kolla directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Hema Sundharam Kolla
Hema Sundharam Kolla
I'm a passionate Computer Science student specializing in DevOps, cloud technologies, and powerlifting. I've completed several certifications, including AWS Cloud Practitioner and Google’s Generative AI badge, and I'm currently exploring both AWS and Azure to build scalable, efficient CI/CD pipelines. Through my blog posts, I share insights on cloud computing, DevOps best practices, and my learning journey in the tech space. I enjoy solving real-world problems with emerging technologies and am developing a platform to offer career advice to students. Outside of tech, I'm a competitive powerlifter, constantly striving to improve and inspire others in fitness. Always eager to connect with like-minded individuals and collaborate on projects that bridge technology and personal growth.