Introduction to Infrastructure as Code(IaC) with Terraform
Infrastructure as Code (IaC) is a way to manage and provision IT infrastructure through machine-readable files rather than manual processes. With IaC, you can treat your infrastructure like any other code, storing it in a version control system, testing it, and deploying it automatically.
Terraform is one of the most popular IaC tools available today. It allows you to define infrastructure in a declarative way and apply changes in a safe and predictable manner. In this article, we will go through the basics of Terraform, its benefits, and a simple example of how to use it to create an EC2 instance in AWS.
Benefits of using Terraform
Getting Started with Terraform
To get started with Terraform, you will need to follow these basic steps:
Step 1: Install Terraform
The first step is to install Terraform on your machine. You can download the latest version of Terraform for your operating system from the official website at https://www.terraform.io/downloads.html. Once you have downloaded and installed Terraform, you can move on to the next step.
Step 2: Create a Terraform Configuration File
The next step is to create a Terraform configuration file. This file will define the infrastructure that you want to create using Terraform. The configuration file is written in a language called HashiCorp Configuration Language (HCL).
Use the Amazon Web Services (AWS) provider to interact with the many resources supported by AWS. You must configure the provider with the proper credentials before you can use it. Get more details from below link.
Here is an example of a simple Terraform configuration file that creates an Amazon Web Services (AWS) EC2 instance:
rovider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-009c5f630e96948cb"
instance_type = "t2.micro"
}
In this configuration file, we first specify the AWS provider and the region we want to use. Then, we define an EC2 instance resource and specify the AMI (Amazon Machine Image) and instance type we want to use.
Step 3: Initialise the Terraform Configuration
Before we can apply our Terraform configuration, we need to initialise it. This will download the necessary provider plugins and set up the backend for storing the Terraform state.
To initialise the configuration, open a command prompt or terminal window, navigate to the directory where your configuration file is saved, and run the following command:
Recommended by LinkedIn
terraform init
Step 4: Preview the Terraform Changes
Once the configuration is initialized, we can preview the changes that Terraform will make to our infrastructure. This step is optional but highly recommended, as it allows us to review the changes before we apply them.
To preview the changes, run the following command:
terraform plan
This will show a summary of the resources that Terraform will create, modify, or delete.
Step 5: Apply the Terraform Changes
Once we are satisfied with the changes, we can apply them using the following command:
terraform apply
Terraform will prompt us to confirm the changes before applying them. If we enter "yes", Terraform will create or modify the resources as specified in our configuration file.
Step 6: Verify the Terraform Changes
After applying the Terraform changes, we can verify that our infrastructure has been created or modified as expected. For example, we can use the AWS Management Console to confirm that the EC2 instance has been created.
Step 7: Destroy the Terraform Resources
Finally, when we are done with our infrastructure, we can destroy the Terraform resources using the following command:
terraform destroy
This will remove all of the resources that were created by Terraform. Be careful when running this command, as it will delete any data stored on the resources.
Conclusion
Terraform is a powerful tool for managing and provisioning infrastructure. With Terraform, you can define your infrastructure in a declarative way and apply changes in a safe and predictable manner. By following the steps outlined above, you can easily get started with Terraform and create your own infrastructure.
Reference:
https://developer.hashicorp.com/terraform/intro
htps://registry.terraform.io/providers/hashicorp/aws/latest/docs