Introduction:
Terraform is a tool that allows you to define and manage your infrastructure as code. With Terraform, you can describe your desired infrastructure state in a configuration file using a simple language. It supports multiple cloud providers and on-premises environments, and it automatically creates, modifies, or destroys resources to match your desired state. Terraform ensures consistency, scalability, and automation in infrastructure provisioning, making it easier to manage and version your infrastructure alongside your application code.
Terraform Workflow
Steps:
Login to AWS Account and Create EC2 Instance
Open the putty application and Connect EC2-instance through Public IP
Install Terraform
sudo yum install -y yum-utils shadow-utils sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo sudo yum -y install terraform
Create a directory named terraform, Navigate into the working directory.
Enter your Access key and Secret key to connect with AWS Provider
In the working directory, create a file called main.tf and paste the following Terraform configuration into it.
provider "aws" { region="ap-south-1" } resource "aws_instance" "myec2" { ami="Enter-instance-ami" instance_type= "t2.micro" tags={ Name="my-terraform" } }
Initialize the project, which downloads a plugin called a provider that lets Terraform interact with the ec2 instance.
terraform init
To rewrite terraform configuration files to colonial format and style we use this command
terraform fmt
To validate the configuration files in a directory use this command
terraform validate
Terraform plan command lets you preview the actions
terraform plan
To execute the actions proposed in a Terraform plan used terraform apply, it is used to deploy your infrastructure
terraform apply
Check-in AWS instance my-terraform instance is launched.
This command will destroy all the resources provisioned by your Terraform configuration.
terraform destory
Other commands in Terraform:
terraform refresh
It is used to reconcile the state Terraform knows about with the real-world infrastructure. This does not modify infrastructure but modifies the state file.
terraform taint
This command manually makes a Terraform-managed resource tainted, forcing it to be destroyed and recreated on the next apply
terraform untaint
This command manually unmark a Terraform-managed resource as tainted, restoring it as the primary instance in the state.
terraform show
This Command is used to provide human-readable output from a state or plan file.
terraform plan -destroy
The behavior of any terraform destroy command can be previewed at any time with an equivalent terraform plan -destroy command.
Conclusion
By the help of Terraform, you can automate the process of provisioning and managing infrastructure, providing speed, consistency, and repeatability in your deployments. In this blog post, we walked through the process of using Terraform to launch an EC2 instance on AWS, highlighting the key steps, best practices, and advanced features along the way