Day 60 & 61 of #90daysofdevops
Hey Techies! Welcome to this blog
In this blog, we are going to start with Terraform.
Introduction to Terraform
Terraform is an infrastructure as code tool that lets you build, change, and version cloud and on-prem resources safely and efficiently.
Terraform is one of the most popular Infrastructure-as-code (IaC) tool, used by DevOps teams to automate infrastructure tasks. It is used to automate the provisioning of your cloud resources. Terraform is an open-source, cloud-agnostic provisioning tool developed by HashiCorp and written in GO language.
Benefits of using Terraform:
Does orchestration, not just configuration management
Supports multiple providers such as AWS, Azure, Oracle, GCP, and many more
Provide immutable infrastructure where configuration changes smoothly
Uses easy to understand language, HCL (HashiCorp configuration language)
Easily portable to any other provider
Terraform Lifecycle
Terraform lifecycle consists of – init, plan, apply, and destroy.
1. Terraform init initializes the (local) Terraform environment. Usually executed only once per session.
2. Terraform plan compares the Terraform state with the as-is state in the cloud, build and display an
execution plan. This does not change the deployment (read-only).
3. Terraform apply executes the plan. This potentially changes the deployment.
4. Terraform destroy deletes all resources that are governed by this specific terraform environment.
Task 1: Installing Terraform
Follow the official documentation here, or simply use the below 3 commands to install terraform:
COPY
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
Task 2: Unveiling Terraform Concepts
1. Why do we use Terraform?
Terraform is essential for its ability to provide a standardized, version-controlled approach to managing infrastructure. It ensures repeatability, scalability, and automation, making it a cornerstone in modern DevOps practices.
2. What is Infrastructure as Code (IaC)?
IaC involves managing and provisioning infrastructure through machine-readable script files rather than traditional hardware configuration. Terraform embraces this paradigm, allowing users to declare and manage infrastructure as code.
3. What is Resource?
In Terraform, a resource is a fundamental unit representing an infrastructure component. It can be a virtual machine, network, or storage. Resources are defined in Terraform configuration files, specifying their attributes and configurations.
4. What is Provider?
A provider in Terraform acts as a bridge between Terraform and different infrastructure or cloud services. Providers are responsible for managing and manipulating resources using their respective APIs.
5. What is the State file in Terraform? What's its importance?
The Terraform state file keeps track of the current state of your infrastructure. It records resource relationships and dependencies, ensuring Terraform understands which resources are deployed and their configurations. This file is crucial for maintaining consistency between declared and actual infrastructure.
6. What is Desired and Current State?
The desired state is the configuration specified in Terraform files, indicating how the infrastructure should be. The current state is the actual state recorded in the Terraform state file. Terraform continuously works to converge the current state towards the desired state.
Task 3: Mastering Essential Terraform Commands
Now that you have a solid understanding of Terraform concepts, let's explore key commands that are instrumental in your Terraform workflow.
1. terraform init
Use terraform init
to initialize a Terraform working directory. This downloads necessary provider plugins and sets up the backend.
2. terraform init -upgrade
Upgrade installed provider plugins with terraform init -upgrade
to ensure you have the latest versions.
3. terraform plan
Run terraform plan
to generate an execution plan. It provides insights into the actions Terraform will take to achieve the desired state.
4. terraform apply
Execute terraform apply
to apply the changes proposed in the execution plan. Confirm before making any modifications to your infrastructure.
5. terraform validate
Ensure configuration correctness with terraform validate
. It checks for syntax errors and validates the configuration against Terraform's language constructs.
6. terraform fmt
Maintain consistent code style using terraform fmt
. It formats your code according to Terraform conventions.
7. terraform destroy
When it's time to decommission your infrastructure, use terraform destroy
. This command systematically tears down all resources defined in your Terraform configuration.
Here I'm attaching a cheat sheet for your good understanding about terraform different commands Terraform Cheat Sheet.
Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Happy Learning!
~kritika :)
Connect with me: LinkedIn