Terraform
Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It allows you to define and manage your cloud infrastructure in a declarative manner, using code to define and configure the resources that make up your infrastructure.
Why Terraform?
In order to deploy an application, we need infrastructure like servers, load balancers, databases, etc. If we use all of these manually, then as humans we may mistakes like how many instances we used, what type of software used, etc. So it will be difficult to restore that mistake. Also, if an employee resigns, we don't know what he has done for the infrastructure. Few of the challenges were mentioned above. Here comes Terraform which is an Infrastructure as Code tool. Everything will be managed using code.
Installation
Follow this URL for installation
Install Terraform | Terraform | HashiCorp Developer
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
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
terraform --version
Terraform is now installed π
Hands-On
Let's create a directory for terraform practice.
πͺ AIM: Want to create a file. Generally, we will use vim devopsfile.txt and then enter text as below.
Let's tell the terraform to create the same file without manual effort. π
Lets keep our automation work in one directory.
We will be writing our IAC in one file with extension "tf". In this file we use HCL (Hashocorp language) which is a terraform language.
There will be a block with { } braces. Inside block there will be parameters.
resource is a block name that expects two labels. 1. Type of the resource and 2. Name of the resource.
Inside the block we can give filename and content.
resource "local_file" "devops" {
filename = "/home/ubuntu/terraform-projects/terraform-local/automatedfile.txt"
content = " Thsi is my devops file created by terraform"
}
I am creating a resource called devops for local file type. I gave name for name to my file with the path and at the last with file name called automatedfile.txt. Also added content as above.
Running the Terraform Code
We need to initiate our resources. Everytime when we add a resource in future that file should be initiated.
Initiate with
terraform init
- Validate terraform using
terraform validate
- To view the plan of what terraform is going to perform use
terraform plan
- Apply the execution plan using
terraform apply
Give yes to tell the terraform to perform the actions. Now our file is applied. Lets check if it created file automatically with the above content added.
Go to the path as mentioned in the file.
This is how terraform automates the things using code.π
Note: Whatever terraform does, it will maintain one log file called terraform.tfstate
Multiple resources in one file
We can have multiple resources in one file. Let's see
Terraform will create a random string with 20 characters, where it allows special characters, except !#$. Then it will give the output where it displays all the characters terraform created.
resource "random_string" "rand-str" {
length = 20
special = true
override_special ="!#$"
}
output "rand-str" {
value = random_string.rand-str[*].result
}
init , validate and apply the new resource.
our output is generated
Terraform with Docker
Let's automate docker using terraform. We can install required providers under terraform block.
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.21.0"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx_ctr" {
image = docker_image.nginx.latest
name = "nginx-tf"
ports {
internal = 80
external = 80
}
}
Lets go through this code,
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.21.0"
}
}
This section specifies that the docker provider is required for this configuration to work. It also specifies the version of the provider to use, which is 2.21.0 or newer, and the source of the provider, which is kreuzwerker/docker.
provider "docker" {}
This section specifies the docker provider to use for the resources defined in this configuration file. It doesn't have any specific configuration options, so it's just an empty block.
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
This section defines a Docker image resource with the name nginx. It specifies that the image to use is nginx:latest and sets keep_locally to false, which means that the image won't be stored on the local machine after it's used.
resource "docker_container" "nginx_ctr" {
image = docker_image.nginx.latest
name = "nginx-tf"
ports {
internal = 80
external = 80
}
}
This section defines a Docker container resource with the name nginx_ctr. It specifies that the container should use the nginx image that was defined earlier, and gives the container the name nginx-tf. It also sets up port forwarding between the container and the host machine by mapping port 80 on the container to port 80 on the host machine.
When we apply the code, using terraform apply we get below error. Because we should install docker in our system.
Note : You cannot add Docker installation in the above code. Terraform is a tool for creating and managing infrastructure, and it assumes that the infrastructure it manages is already in place, including any required software or dependencies. In the case of using the Docker provider, Terraform assumes that Docker is already installed on the machine or machines where the Docker resources will be created.
If you need to install Docker as part of your infrastructure provisioning process, you would need to use a different tool or script to perform the installation, such as a shell script or configuration management tool like Ansible or Chef. Once Docker is installed, you can then use Terraform to create and manage Docker resources, as shown in the example code you provided.
--- πThats all for introduction to terraform, Let's meet in the next concepts on terraform. Thank you for your time π --