Getting started with Terraform
Day 1 of #TerraWeek Challenge
Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define and provision infrastructure resources such as virtual machines, databases, networks, and more using code. Terraform uses a declarative configuration language to define the desired state of your infrastructure, and it automatically manages the provisioning and configuration of those resources to match the defined state. This approach makes infrastructure provisioning and management more efficient, predictable, and scalable.
Why do we need Terraform?
Traditional infrastructure provisioning methods often involve manual and error-prone processes, making it difficult to replicate and maintain infrastructure across different environments. Terraform addresses these challenges and simplifies infrastructure provisioning in several ways:
Infrastructure as Code (IaC): Terraform allows you to define your infrastructure as code, which means your infrastructure's configuration is stored in version-controlled files. This enables better collaboration, versioning, and automation.
Declarative Configuration: Terraform uses a declarative syntax, where you specify the desired end state of your infrastructure. Terraform then figures out how to make the actual infrastructure match this desired state.
Multi-Cloud and Multi-Provider Support: Terraform supports multiple cloud providers (e.g., AWS, Azure, GCP) and other infrastructure platforms (e.g., VMware, Docker). This allows you to manage resources across different providers with a consistent workflow.
Resource Dependency Management: Terraform automatically manages resource dependencies, ensuring that resources are created or updated in the correct order to avoid configuration errors.
Change Management: Terraform provides a way to plan and preview changes before applying them, reducing the risk of unintended modifications to your infrastructure.
Terraform Workflow
Terraform lifecycle consists of โ init, validate, plan, apply, and destroy
Terraform init: initializes the working directory (local) which consists of all the configuration files. (.terraform)
Terraform validate: checks the code for syntax errors and ensures that it is internally consistent. It only looks at the configuration files (*.tf) in the current working directory
Terraform plan: compares the Terraform state with the as-is state in the cloud, builds and displays an execution plan. This does not change the deployment (read-only).
Terraform apply: executes the plan. This potentially changes the deployment.
Terraform destroy: deletes all resources that are governed by this specific Terraform environment.
Setting up Terraform Environment
To get started with Terraform, you need to:
Install Terraform:
Download the Terraform binary suitable for your operating system from the official Terraform website (https://www.terraform.io/downloads.html).
Ensure that your system is up to date and you have installed the
gnupg
,software-properties-common
, andcurl
packages installed. You will use these packages to verify HashiCorp's GPG signature and install HashiCorp's Debian package repository.sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
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
Verify the installation using
terraform --version
.Configure Providers: Terraform requires provider configurations for your target cloud or infrastructure platform. You can set up configurations for providers like AWS, Azure, or GCP by providing API keys, authentication tokens, and other required credentials.
Write providers.tf file which defines the region we want the infrastructure to be created
Create Terraform Configuration Files: Write Terraform configuration files (usually with a
.tf
extension) that defines the infrastructure resources and their configurations.For example- if we want to configure a AWS cloud provider we create a file called terraform.tf and write the source and version. You can use the link to know the latest version -https://registry.terraform.io/providers/hashicorp/aws/latest/docs
Initialize the Working Directory: Run
terraform init
in your project directory to initialize the working directory and download any necessary plugins or modules.Terraform plan :- preview the changes that Terraform will make to your infrastructure based on your configuration. It helps you understand what Terraform intends to do before you apply those changes.
Review the output and ensure it matches your intentions.
If everything looks good, execute the following command to apply the changes and create the infrastructure:
Terraform apply
This shows the instance is created , up and running
- Destroy Infrastructure:
terraform destroy
With one single command, you can destroy your whole infrastructure.๐ค
If you recheck the EC2 dashboard, you will see the instance got terminated.
Conclusion of Terraform and Its Significance in Tech Terms:
Terraform is a vital Infrastructure as Code (IaC) tool that streamlines the management of tech infrastructure. It empowers automation, supports multi-cloud architecture, employs declarative syntax, offers change preview, ensuring efficient, scalable, and consistent infrastructure provisioning. Terraform serves as a cornerstone for modern DevOps practices and facilitates seamless cloud adoption.
Happy Learning peeps :-)
Subscribe to my newsletter
Read articles from Krishan Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by