What is Terraform?
Terraform is a Infrastructure as Code tool for provisioning infarstructure .Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It enables you to define, provision, and manage infrastructure resources across multiple cloud providers (like AWS, Azure, Google Cloud) and on-premises environments using a declarative configuration language.
Provisioning:
Provisioning refers to the process of creating and configuring infrastructure resources. For example, if you want to create an EC2 instance in AWS, you write the desired configuration (like instance type, AMI, and region) in a Terraform file. Then, when you execute the Terraform commands, it provisions the resources as specified in the code.
Key Features of Terraform:
Declarative Approach:
You describe the desired state of your infrastructure, and Terraform figures out how to achieve it.
This simplifies infrastructure management by allowing you to focus on "what" you want instead of "how" to do it.
State Management:
Terraform maintains a state file that tracks the current state of your infrastructure.
This helps Terraform determine the changes needed to align your actual infrastructure with the desired state defined in your code.
Multi-Cloud Support:
- Terraform supports a wide range of providers, allowing you to manage resources across AWS, Azure, Google Cloud, Kubernetes, and more.
Infrastructure Automation:
Terraform automates the process of creating, updating, and destroying infrastructure.
This reduces manual effort and ensures consistency across environments.
Reusability and Modularity:
- Terraform allows you to write modular code, making it easy to reuse and manage configurations for different projects or teams.
Challenges of IAC
Slow Deployment.
Expensive.
Limited Automation.
Human Error.
Wasted Resources.
Inconsistency.
Ex : Suppose you need to deploy an EC2 instance:
Write a configuration file (
.tf
file) specifying the instance type, region, and AMI.Run
terraform init
to initialize the project.Use
terraform plan
to preview the changes.Apply the changes with
terraform apply
to provision the EC2 instance.
So lets see the above example by doing practical.
Step 1: Firstly we have to create a EC2 instances in AWS and then connect it through ssh and update your instances.
Step 2: To install the terraform follow the below images.๐
Step 3: In 3rd step we have create a main.tf (tf is a extension name for terraform) , in main.tf file region,ami id,instance type, name for the instances will be written ,these are the provider and resource.
Step 4: In this step we have installed awscli for access key where system will understand the AWS credentials and identify the account.
Step 5 : Here in variable.tf we have to give the ami id which will be linked to the main.tf.
Step 6 : In terraform.tf we have to give required providers and source , version of hashicorp.
Before writing a ec2_vpc.tf we have to copy the key pair which is downloaded in our local system to our ec2 instances by Scp (Secure Copy Protocol) scp /path/to/local/file username@remote_host:/path/to/remote/destination.
Step 7 : in ec2_vpc.tf we are giving the key pair path which is copied and and allow ssh and give the ingress that is nothing but port 22 and cidr blocks .
Step 8 : After writing all the configuration files we have to initialize the terraform by terraform init.
Step 9 : Then terraform plan whether the EC2 instances is created or not it will be displayed.
Step 10 : Then after terraform plan give terraform apply and the EC2 instances will be created successfully.
Step 11 : Now we can see the instance running in AWS without opening the aws account we have created the EC2 instance using terraform by writing configuration files.
Step 12 : When we connect this page will appear.
THANK YOU FOR READING!