Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define, manage, and provision infrastructure resources across multiple cloud platforms and on-premises environments
1 Manage resources across providers like AWS, Azure, Google Cloud, and more.
2 Simplify and automate repetitive tasks related to infrastructure management
3 Focus on the desired outcome rather than the steps to achieve it
4 Keep track of infrastructure states with a state file, ensuring consistency across deployments.
How to Set Up Terraform and Create Resources
Prerequisites
Install Terraform from HashiCorp’s website.
Have access to a cloud provider (e.g., AWS, Azure, GCP) with credentials set up.
A text editor (e.g., VS Code) and a terminal or command-line interface.
Here we can use Linux :
1 sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
2 wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
3 gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
To add the HashiCorp repository to your system, execute the following command:
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
Update System using: - sudo apt-get update
master@Burhan:~$ sudo apt-get update
https://download.docker.com/linux/ubuntu bionic InRelease [64.4 kB] Hit:2 apt.releases.hashicorp.com focal InRelease Hit:3
Install Terraform : sudo apt-get install terraform
After the installation check Version : Terraform —version
Create First Terraform file : vim Main.tf
its create on local Machine
Steps : How to Run Terraform File
1 Terraform init : Initializing the backend... Terraform has been successfully initialized!
2 Terraform plan : Review the plan to see the changes Terraform will make:
3 Terraform apply : Apply the changes to provision the defined resources:
Check your DevOps.txt File is create : ls
Confirm the operation when prompted. Terraform will provision the resources.
4 Verify the Resources
Check your cloud provider’s dashboard to confirm the resources have been created.
5 Clean Up Resources
To remove all resources managed by Terraform, use: terraform destroy
To recreate resources using terraform apply
, you can follow these steps
6 Steps to Recreate Resources with Terraform Apply
Edit Configuration (if needed):
1 Update your .tf
file with the desired configuration changes.
2 Save the file in your working directory.
7 AWS Part :
1 Login AWS Account
2 Search IAM :
3 Create a user
Give a User Name & Click on Next
Give a Access
Create a User
8 After creating a user we will have to install AWS CLI. Link
To install the AWS CLI, run the following commands.
curl "
https://awscli.amazonaws.com/awscli-exe-linux-x86_64-2.0.30.zip
" -o "
awscliv2.zip
" unzip
awscliv2.zip
sudo ./aws/install
After a installing AWS CLI : check using aws -- version
9 Binding Local System with AWS : aws configure
Open IAM user Securities Group :
Create access key :
1 Check Command Line Interface (CLI)
2 I understand the above recommendation and want to proceed to create an access key….
3 click on Next
4 Create Access key
5 Copy Access Key & Secret access key Paste on Terminal
6 Default region name [None] : Enter
7 Default output format [None]: Enter
10 AWS Terraform Provider
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. Link
To install this provider, copy and paste this code into your Terraform configuration. Then, run terraform init
.
terraform { required_providers { aws = { source = "hashicorp/aws" version = "5.78.0" } } } provider "aws" { region = "us-west-2" }
Run terraform init
ubuntu@DESKTOP-U27GP0N:~/terraform$ terraform init Initializing the backend... Initializing provider plugins...
Finding hashicorp/aws versions matching "5.78.0"...
Reusing previous version of hashicorp/local from the dependency lock file
Installing hashicorp/aws v5.78.0...
after a finished this step need to add one more file : s3.tf
resource "aws_s3_bucket" "my_bucket" { bucket = "burhantws" tags = { Name = "burhantws" Environment = "Dev" } }
Save & apply
Following a Terraform application, we must verify that your S3 bucket has been generated and check the AWS S3 bucket.
The next step will be to use a Terraform file to build an AWS instance and provide some instance information.
resource "aws_instance" "my_instance" {
instance_type = "t2.micro"
ami = "ami-04dd23e62ed04"
tags = {
Name = "BurhanTerraTws"
}
}
After a change need to save this file & run command terraform plan
then terraform apply
Please wait for complete this process
Instance has been created
Use the Terraform destruct function if you wish to remove this instance using Terraform. terraform destroy