What is terraform?
Terraform enables you to automate the creation, modification, and destruction of your infrastructure resources by reducing manual work and human errors. Terraform is an open-source tool that allows you to define and manage your infrastructure as a code. You can use Terraform to provision and configure resources on various platforms, such as cloud providers, or on-premises servers.
It allows you to use a consistent and repeatable workflow to manage your infrastructure across different environments, such as development, testing, and production.
It supports multiple cloud providers and services, giving you the flexibility to choose the best platform for your needs.
It tracks the current state of your infrastructure in a file, which acts as a source of truth for your environment and helps you avoid configuration drifts.
It generates a plan before applying any changes to your infrastructure, letting you review and approve the proposed operations.
It supports modular and reusable code, enabling you to share and collaborate on infrastructure configurations with other developers.
Prerequisites to use the terraform.
AWS account to launch the instance.
If you don't have an AWS account. Search on Google "How to open aws account?" Open the first link. Create a free account.
Launching the Ubuntu Server.
Log in to aws console.
Click on instance. Launch the Instance > Select the Ubuntu Server.
Create a "key-pair" and Save it. Click on Launch instance.
Once the instance is ready. Click on the connect. Click on the SSH Client tab. Copy the "ssh command" in the example. Open your terminal window & paste the command and run it.
You will get access to the Ubuntu server.
How to Install Terraform?
Run the below commands.
apt update
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
gpg --no-default-keyring --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg --fingerprint
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-get install terraform
Verify the installation using below commands
terraform -help
terraform -v
You should see something like this:
Use terraform -h command to check Terraform's available subcommands.
Thatβs it! You have successfully installed Terraform on the Ubuntu server. You can now use Terraform to provision and manage your infrastructure as code.
Enable tab completion
touch ~/.bashrc
terraform -install-autocomplete
Once the autocomplete support is installed, restart your shell.
To use terraform
Start from basic. This will boost your confidence in using the Terraform.
1) To Create a "file" on our server using the terraform follow the below steps.
1) Command to create a folder for Terraform practice
mkdir terraform-practice
2) Command to go inside the folder (directory)
cd terraform-practice
4) Command to initialise to terraform
terraform init
5) Command to create a file with extension .tf >> file-name.tf
6) press "i" to insert and paste/write below content >>
resource "local_file" "vinayak"
{ content = "This file is created by Vinayak using terraform"
filename = "./vin-test-file.txt" }
7) Use the Terraform init command to let Terraform know you have created a resource file.
terraform init
8) Command to check you have written a file with the correct syntax
terraform validate
9) Command to check plan >> "What will get created"
terraform plan
10) Command to apply the plan
terraform apply
11) Command to in our current folder file is created.
ls
cat vin-test-file.txt
Congratulations, You have successfully created the file using Terraform.
2) To provision an NGINX server using docker & terraform.
You can provision an NGINX server using Docker. To use docker to launch server docker should already be installed on your server.
Command to install docker
sudo apt install docker.io
1) Command to create folder for Terraform practice using docker
mkdir terraform-nginx-docker
2) Command to go inside the folder (directory)
cd terraform-nginx-docker
4) Command to initialise to terraform
terraform init
5) Command to create a file called main.tf
vi main.tf
6) press "i" to insert and paste/write below content >>
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker" version = "~>3.0.1"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" { name = "nginx" keep_locally = false }
resource "docker_container" "nginx" { image = docker_image.nginx.image_id name = "tutorial"
ports {
internal = 80
external = 8000
}
}
7) Command to initialise the project, which downloads a plugin called a provider that lets Terraform interact with Docker.
terraform init
8) Command to check you have written a file with the correct syntax
terraform validate
9) Command to check plan >> "What will get created"
terraform plan
10) Command to apply the plan
terraform apply
11) Command to verify the existence of the NGINX container.
docker ps
12) To stop the container, run terraform destroy.
terraform destroy
Congratulations You have successfully provisioned and destroyed an NGINX webserver with Terraform.
Here are some terminologies you need to know.
State: (File name terraform.tfstate)
Explanation: Terraform keeps track of the current state of your infrastructure in a state file named "terraform.tfstate". This file is essential for tracking changes and managing your infrastructure's lifecycle.
Example: The state file records the attributes of resources created, their IDs, and their current configurations.
Plan and Apply:
Explanation: To make changes to your infrastructure, you first create an execution plan using a
terraform plan
. This shows what Terraform will do. Then, you apply these changes usingterraform apply
to make them happen.Example: After making changes in your configuration, run
terraform plan
to see what resources will be added, modified, or destroyed. If the plan looks good, execute it withterraform apply
to apply those changes.
Provider:
Explanation: A provider is a plugin in Terraform responsible for interacting with a specific cloud or infrastructure platform, such as AWS, Azure, or GCP.
Example: To use AWS as your cloud provider, you'd define it in your Terraform configuration like this:
provider "aws" {
region = "us-east-1"
}
Resource:
Explanation: Resources are the building blocks of your infrastructure. They represent the cloud resources you want to create, like EC2 instances, S3 buckets, or databases.
Example: Creating an AWS EC2 instance:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
Module:
Explanation: Modules allow you to represent and reuse parts of your Terraform configuration. They help in organizing code and making it more maintainable.
Example: Defining a simple module to create a VPC:
module "my_vpc" {
source = "./modules/vpc"
cidr = "10.0.0.0/16"
}
π Happy Learning π
π Thanks For Reading!π