Table of contents
Hello Learners, you guys are doing every task by creating an ec2 instance (mostly). Today let’s automate this process. How to do it? Well, Terraform is the solution.
What is Terraform?
Terraform is an infrastructure as code (IaC) tool that allows you to create, manage, and update infrastructure resources such as virtual machines, networks, and storage in a repeatable, scalable, and automated way.
Task-01: Install Terraform on your system
- Create an EC2 instance in the AWS management console.
- Download the appropriate package for your system from the official Terraform website (https://www.terraform.io/downloads.html).
- Copy the commands and execute them on the server.
- Let’s check the version of Terraform.
Task-02:
Q1:- Why do we use terraform?
Ans:- We use Terraform for a few main reasons:
Infrastructure as Code: Terraform allows us to define our infrastructure in code. This makes it version-controlled, reusable, and easy to modify.
Declarative: Terraform configurations declare what our infrastructure should be. Terraform will determine the steps to reach that desired state.
Easy to Learn: Terraform has a simple language based on HCL (HashiCorp Configuration Language). It's easy for developers to pick up.
Manage Multiple Providers: Terraform can manage infrastructure across multiple cloud providers (AWS, GCP, Azure, etc.) and even on-premises.
Idempotent: Terraform ensures our configurations match the desired state, even after multiple runs. It only makes changes where needed.
Modular: We can split Terraform configurations into modules to reuse across multiple environments.
Workflow: Terraform has built-in commands to plan changes, apply changes, and destroy resources.
Q2:- What is Infrastructure as Code (IaC)?
Ans:- Infrastructure as Code is the practice of managing data centre infrastructure in a software-driven, automated way. It provides many benefits around consistency, auditability, and reproducibility.
Q3:- What is Resource?
Ans:- A resource is anything in your infrastructure that needs to be managed or configured - things like virtual machines, load balancers, databases, storage, etc.
When using Infrastructure as Code tools like Terraform, resources are defined in configuration files. This allows the tools to provision and manage those resources in an automated, reproducible way.
For example, in Terraform a resource block might look like this:
resource "aws_instance" "web" {
ami = "ami-abc123"
instance_type = "t2.micro"
}
Here aws_instance
is the resource type, and web
is the resource name. This defines an Amazon EC2 instance resource, specifying the AMI and instance type.
When you run terraform apply
, Terraform will:
Determine if an EC2 instance named "web" already exists
If not, it will create a new EC2 instance with the specified AMI and instance type
If it does exist, it will compare the configuration and only make changes if needed
Q4:- What is Provider?
Ans:- A provider is an Infrastructure as Code plugin that allows Terraform to manage resources within a particular cloud or system.
For example, some of the most common providers are:
AWS Provider - Allows Terraform to create and manage AWS resources
Azure Provider - For managing Azure resources
Google Cloud Provider - For creating and managing GCP resources
Q5:- What is the State file in Terraform? What’s the importance of it?
Ans:- The state file in Terraform is an important file that tracks the resources that Terraform manages. It stores:
The IDs of the resources
Attributes of the resources
The dependency graph of the resources
The state file is important for a few reasons:
It allows Terraform to identify which resources already exist, so it only creates new resources when needed. This ensures consistency and avoids duplicate resources.
It tracks the configuration of existing resources, so Terraform can determine what changes need to be made when configurations are updated.
It allows Terraform to destroy resources when removing them from configuration. Without the state file, Terraform would not know which resources it created.
It builds the dependency graph of resources, so Terraform knows the order to create and destroy resources.
It allows Terraform to resume after interruptions (like system failure) by remembering what resources already exist.
Q6:- What is the Desired and Current State?
Ans:- Desired state and current state are two important concepts in infrastructure as code tools like Terraform.
Desired state refers to the infrastructure configuration that you have defined in your Terraform configuration files. It represents the state that you want your infrastructure to be in.
Current state refers to the actual current state of the infrastructure that Terraform is managing. This is represented by the state file.
When you run a Terraform plan, it will compare the desired state (from your config files) to the current state (from the state file). Based on the difference between these two states, Terraform determines what changes need to be made to infrastructure to match the desired state.
"Thank you for enjoying my DevOps blog! Your positive response fuels my passion to dive deeper into technology and innovation.
Stay tuned for more captivating DevOps articles, where we'll explore this dynamic field together. Follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/som-shanker-pandey/) for the latest updates and discussions.