One of the main benefits of using Terraform is that it enables you to version your infrastructure, just like you would with code. This makes it easy to track changes and roll back if needed. It also makes it easier to collaborate with your team, as you can share and review configuration files in the same way you would with code.
In this blog, we'll cover the basics of Terraform and walk you through the process of getting started. We'll also provide tips and best practices along the way to help you get the most out of Terraform.
So let's dive in and start using Terraform to manage your infrastructure!
1. Install Terraform
The first step in getting started with Terraform is to install it on your local machine. Terraform is a command-line tool, so you can download the appropriate binary for your operating system from the Terraform website. Once you have it downloaded, make sure it is in your system's PATH so that you can run it from the command line. For macOS, you can use the below command to install terraformbrew tap hashicorp/tap
brew install hashicorp/tap/terraformwget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /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 terraform2. Initialize a Terraform project
Once you have Terraform installed, you can initialize a new Terraform project by running theterraform init
command. This command will create a new folder called ".terraform" in your current working directory, where Terraform will store all of its state files.Prerequisites
To follow this tutorial you will need the following:- The Terraform CLI is installed.
- The AWS CLI is installed.
- AWS account and associated credentials that allow you to create resources.
terraform init //assuming AWS and Terraform CLI is installed and configured3. Write your Terraform Configuration
Next, you'll need to write your Terraform configuration. The configuration is written in the HashiCorp Configuration Language (HCL), and it describes the resources you want to create. For example, if you want to create an AWS EC2 instance, you would write the following configuration:resource "aws_s3_bucket" "gallery" {
bucket = "mygalleryapptrt"
tags = {
Name = "GalleryApp"
Environment = "Dev"
}
}
4. Validate your Terraform Configuration
You'll need to validate your Terraform configuration. This can be done using the "terraform plan" command. The "terraform plan" command is used to create an execution plan for your Terraform configuration. It allows you to preview the changes that Terraform will make before applying them to your infrastructure, making it easier to avoid costly mistakes.terraform planTerraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_s3_bucket.gallery will be created
+ resource "aws_s3_bucket" "gallery" {
+ acceleration_status = (known after apply)
+ acl = (known after apply)
+ arn = (known after apply)
+ bucket = "mygalleryapp"
+ bucket_domain_name = (known after apply)
+ bucket_regional_domain_name = (known after apply)
+ force_destroy = false
+ hosted_zone_id = (known after apply)
+ id = (known after apply)
+ object_lock_enabled = (known after apply)
+ policy = (known after apply)
+ region = (known after apply)
+ request_payer = (known after apply)
+ tags = {
+ "Environment" = "Dev"
+ "Name" = "GalleryApp"
}
+ tags_all = {
+ "Environment" = "Dev"
+ "Name" = "GalleryApp"
}
...........
Plan: 1 to add, 0 to change, 0 to destroy.5. Apply the Configuration
Once you are satisfied with the changes that Terraform will make, you can apply the configuration by running the "terraform apply" command. This will create or update all the resources defined in your configuration, and Terraform will keep track of all the resources it creates in its state files, allowing you to manage and update them in the future.terraform applyPlan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value:aws_s3_bucket.gallery: Creating...
aws_s3_bucket.gallery: Still creating... [10s elapsed]
aws_s3_bucket.gallery: Creation complete after 10s [id=mygalleryapptrt]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.- Cross-platform compatibility: Terraform supports a wide range of cloud providers and on-premises environments, including AWS, Azure, Google Cloud, and more. This allows users to manage their entire infrastructure using a single tool, regardless of where their resources are located.
- State management: Terraform keeps track of the current state of your infrastructure, allowing you to easily see what resources are currently running, and making it easy to roll back changes if necessary.
- Modularity: Terraform's use of modules makes it easy to organize and reuse infrastructure code. By breaking your infrastructure down into smaller, reusable modules, you can reduce duplication, and increase scalability and maintainability.
- Versioning: Terraform's ability to version control your infrastructure code and state files, makes it easy to collaborate and track changes over time.
- Previewing changes: Terraform's "plan" command allows you to preview the changes that will be made to your infrastructure before they're applied. This makes it easy to catch errors before they happen and ensure that your infrastructure stays in the desired state
- Automation: With Terraform, you can automate the process of provisioning, updating, and deleting resources, which can save you a lot of time and effort.
- Open source tool: Terraform is an open-source tool and is actively maintained by a community of developers and contributors around the world which makes it more reliable and efficient to use.


