Create AWS EC2 Instance using Terraform.

Greetings, fellow tech enthusiasts! I'm Shubham Rasal, but you might know me better as the SREngineered.
Over the last three years, I have immersed myself deeply in the intricate universe of DevOps and SRE. Along the way, I've brought life to various technologies and projects, utilizing everything from bash script automation to Kubernetes production clusters. My credentials include the Certified Kubernetes Administrator (CKA) and Ansible Certified Engineer titles, standing testament to my unwavering commitment to and understanding of our craft.
This blog is designed for kindred spirits who are passionate about engineering excellence. My goal is to share my experience, knowledge, and wisdom through tutorials, case studies, and Golang code examples that offer valuable solutions for navigating the continually evolving world of DevOps and SRE.
No matter where your interests lie within the vast landscape of DevOps/SRE, Automation, and Golang, this blog aims to offer valuable insights to help you elevate your skills and understanding. So, buckle in and join me on this journey into the thrilling world of cutting-edge engineering solutions!
When I'm not busy conjuring up elegant engineering solutions, I enjoy getting lost in the immersive worlds of movies and books. Feel free to connect with me on LinkedIn, Twitter, or through email.
Create AWS EC2 Instance using Terraform
What is Terraform ?
“Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.”
Now let’s discuss where it is used?
There are many use-cases but let’s consider one scenario.
Consider an organization that has a project and they have multiple teams working on that project. After a certain level, it becomes very challenging to manage a large and growing infrastructure for the centralized operations team. Manually handling infrastructure and shifting to another account without having to document what you have done is very hard.
Using Terraform, the knowledge of how to build can be codified in a configuration. Additionally, infrastructure can be shared and re-used. i.e Infrastructure as a Code.
and my favorite one is Terraform lets you build one-click infrastructure and one-click destroy infrastructure.
1.Download Terraform
You can download terraform according to your Operating System.
https://www.terraform.io/downloads.html
2. Extract and Set the environment path variable.
You can extract anywhere but I recommend you make a folder into C:/Program file and extract there. Then update the environment path variable.
3.Check terraform Version.
terraform -v
Configure AWS
1. Create an IAM user that has an EC2 creation policy.
I recommend you to create PowerAccessPolicy for Practice.
For more details you can refer this:How to create IAM user
2. You will get ACCESS KEY and SECRET KEY using that you can now configure to aws on cli.
aws configure — profile “name”

Build EC2 instance
For Terraform we have to use(HCL) HashiCorp Configuration Language.
As now we have already signed up into CLI.
we need to configure into Terraform.
Configure to terraform.
Open file and give any name. Save with .tf extension. Copy below code into that file.
provider "aws" {
region = "ap-south-1"
profile = "shubham"
}
Now to install aws plugin we need to run below command
$ terraform init
Create EC2 instance
`resource "aws_instance" "myFirstInstance" {
ami = "`ami-0447a12f28fddb066`"
instance_type = "t2.micro"
tags = {
Name = "server"
}
}`
add above code to file. Here we are using “aws_instance” resource and we named it as “MyFirstInstance”. We used ami of Amazon linux so it will lanuch Amazon Linux os with t2.micro type and name will be “server”.
Output
output "Server_instance"{
value=aws_instance.myFirstInstance
}
add above code to get the details of instance on terminal.
How to run code?
$ terraform validate myinstance.tf
$ terraform apply
first command will validate the syntax is correct or not. Even it is optional but using this command is good practice.
terraform apply will execute the code on aws. type “yes” when it prompted.
Output: We will see the details of above instance. You can also do recheck on https://ap-south-1.console.aws.amazon.com/ec2 .

How to delete all infrastructure?
To delete everything you created using terraform code.
$ terraform destroy
above command will destroy everything that is managed by terraform.
Conclusion
This is the just HelloWorld program of AWS using Terraform. Here we just created EC2 instance without key.
We will do many practicals. Don’t hesitate to ask queries. For further articles follow and stay connected.




