EC2 Infrastructure creation - Terraform
Author: Ashish Kodumuru
Project Name: EC2 x Terraform [Beginners]
Difficulty level: Easy Peasy
Terraform: Terraform is an open-source tool for automating the setup and management of infrastructure (like servers, networks, databases) as code. It lets you define resources in configuration files, making deployments consistent and easy to manage.
Q) For those who have knowledge in creating infrastructure using AWS CLI might be wondering why are we using Terraform if we already have one - It’s a genuine question Tbh!
Answer: Terraform allows you to define and manage your entire infrastructure as code, making it easier to automate, version control, and reuse. Unlike AWS CLI, which handles resources individually, Terraform manages complex environments consistently and can work with multiple cloud providers.
Installing Terraform on Windows
Open search bar and type power shell
Click on Run as administrator
choco install terraform - Run this command
Then it will ask “Do you want to run this script“ - Type “A or Y“.
After installing Terraform once again open power shell but this type directly (Don’t run it as Administrator)
To Verify you can run this command terraform -help
If you can see this sort of output then you are all set
You can also visit official docs - Official Docs
Connecting Windows to AWS CLI
You need to establish authentication between AWS CLI and Windows to capture any changes to AWS Cloud. Without authenticating, you can’t do anything. Here are the steps to authenticate:
Login to your AWS console by clicking this
When you are in your account, go to your profile at the top right of your UI and click on "Security Credentials."
Scroll down, You’ll see Access key field, Create new
Now you have your access key and its password to get connected with windows terminal
Go to your terminal and run aws configure. It will prompt you for the access key and its secret ID; provide these from step 4. It will also ask for the region and the type of output format (you can leave these as default).
Now you are successfully authenticated with your AWS account, and any changes made from the CLI will be captured in your account.
Terraform implementation
Since you are connected to your AWS Account, your main goal now is to create a basic EC2 instance with minimal configuration using Terraform code.
Terraform Basically runs on these four commands
→terraform init : It initializes terraform onto your local→terraform plan : After providing the necessary code, it gathers all the information, similar to how "git add" works.→terraform apply : It makes the changes and you can observe it in the UI also.**
→terraform destroy**Open your Windows terminal and run cd Desktop to navigate to your desktop. Then, run the following commands to create the folders, subfolders, and files needed to run Terraform
→mkdir Project Terraform
→cd Project Terraform
→mkdir aws
→cd aws
→mkdir local_state
→mkdir remote_stateNow navigate to local state and initialize Terraform**
→cd local_state**
terraform init - After running this command terraform is initialized, Now run the following command→vim main.tf
Run this script
Code Explanation:
All of the code must be written inside terraform {#code} block.
required_providers
{
aws =
{
source = "hashicorp/aws"
version = "~> 4.16"
}
}
The above block of code specifies which service provider's API you want to work with in Terraform, including the version and source.provider "aws" { region = "us-west-2" }
The above block of code specifies where to launch your EC2 instance.resource "aws_instance" "app_server"
{ ami = "ami-830c94e3"
instance_type = "t2.micro"tags = { Name = "Terraform_Demo" }
The above block of code provides the tag information and configuration for the instance you want to host, as well as the required AMI.
Now run terraform plan
Before terraform apply
run terraform apply and go to EC2 service and look for changes.
Finally, we have successfully created an EC2 instance using Terraform. Please review the document carefully for a clear and comprehensive understanding. It provides step-by-step guidance to ensure ease of learning. Feel free to reach out if you have any questions or need further clarification.
All of the above code is in this repository - Github .
You can also explore official docs here.
You can connect me Linkedin.
Reach me out : kodumuruashish15012005@gmail.com