This repository contains everything about Terraform, including:
- What Terraform is
- How to create and manage infrastructure
- State files
- Remote backends, and more
- Terraform is an Infrastructure management tool or Infrastructure as Code (IaC).
- It is an open-source IaC tool developed by HashiCorp.
-
Terraform helps create and manage infrastructure on cloud providers.
-
What is Infrastructure?
Think of infrastructure as building a house:- You define the number of rooms, the type of materials, and the placement of doors and windows.
- Similarly, in Terraform, you define the type of servers, databases, etc., for cloud providers like AWS, Azure, and GCP.
Here, the cloud provider is like the house, and the servers and databases are like the rooms and materials.
-
Simply but, Terraform is an IaC tool developed by HashiCorp for creating and managing infrastructure on cloud providers like AWS, Azure, and GCP.
-
Before Terraform:
- To create resources like EC2 instances or S3 buckets or any other resources on any cloud provider like (AWS, Azure, GCP) let's take AWS, we used to:
- Log into AWS and manually create the resources.
- Use AWS CLI to connect and manage the instances using their public IP addresses.
- This approach was similar for other providers like Azure and GCP.
- Managing resources became challenging in hybrid cloud environments (e.g., hosting some resources on AWS and others on Azure to reduce costs).
- To create resources like EC2 instances or S3 buckets or any other resources on any cloud provider like (AWS, Azure, GCP) let's take AWS, we used to:
-
Limitations of Manual Methods:
- For small tasks (e.g., creating 2 instances), manual methods like AWS CLI or CloudFormation templates were manageable.
- But if 100 teams request similar resources simultaneously, manual creation becomes time-consuming and error-prone.
-
Why Terraform is Better:
- With Terraform:
- You define the cloud provider (e.g., AWS, Azure, GCP) in a configuration file (e.g.,
main.tf
). - Specify the resources you want to create.
- If you need to switch cloud providers in the future, you can simply update the
main.tf
file and modify the resource configurations as needed.
- You define the cloud provider (e.g., AWS, Azure, GCP) in a configuration file (e.g.,
- Terraform automates the process, making it faster and less error-prone.
- With Terraform:
-
State Management:
- Terraform uses a state file to track everything you create and manage.
- It helps track any changes made to resources and ensures consistency across deployments.
By automating tasks that were previously time-intensive, Terraform significantly improves efficiency and scalability in managing cloud resources.
-
Create a File:
- Create a file named
main.tf
. The name can vary, but the extension must be.tf
.
- Create a file named
-
Define Cloud Provider:
- Specify the desired cloud provider (e.g., AWS, Azure, GCP).
provider "aws" { region = "us-east-1" # Set your desired AWS region }
- Specify the desired cloud provider (e.g., AWS, Azure, GCP).
-
Define Configurations:
- Add the required configurations for the resources you want to create. For example, to create an EC2 instance:
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" # Specify an appropriate AMI ID instance_type = "t2.micro" }
- Add the required configurations for the resources you want to create. For example, to create an EC2 instance:
-
Save the File:
- Save the file after defining the configurations.
Terraform works with four main commands:
-
terraform init
:- Initializes the configuration file.
- After running this command, you’ll see
Initialization successful
.
-
terraform plan
:- Performs a dry run, showing the resources to be created.
- Helps debug and reduce errors.
- Creates the
terraform.tfstate
file.
-
terraform apply
:- Applies the configuration and creates the defined resources (e.g., EC2 instances).
- To verify, log into AWS and check the resources under EC2 > Instances.
-
terraform destroy
:- Deletes or terminates the created resources.
By following these steps, you can create and manage any resource on any cloud provider using Terraform.