Introduction
Welcome to the world of Infrastructure as Code (IaC).
Imagine you are a head chef in a busy restaurant. Traditionally, you might cook every meal by hand, step-by-step. In the IT world, this is like manually configuring servers one by one—it is slow and prone to mistakes.
Instead of configuring infrastructure manually through repeated commands or cloud console clicks, IaC lets you describe infrastructure in files that can be versioned, reviewed, and reused.
Terraform is one of the most widely used IaC tools. It is declarative: you describe the desired end state, and Terraform works out how to reach it.
That declarative model is one of Terraform's defining ideas. You describe what infrastructure should exist, not the full imperative script for each low-level step needed to create it.
In this lab, you will set up your "kitchen" and teach your robot its first skill. You will learn about Providers, which are like the specific cooking tools the robot needs to access different ingredients (like AWS, Google Cloud, or Docker).
By the end of this lab, you will have installed Terraform, created your first "recipe" file, and prepared the environment to start building.
Install Terraform
We need to install the terraform program. Think of this as downloading the robot's brain. We will do this manually so you can clearly see where the program comes from and where it is stored.
First, let's verify where we are currently working. We want to be inside the project folder.
pwd
Next, we will download the Terraform package from the internet. We use the wget command to fetch a specific version (1.14.3) to ensure everything works perfectly for this lab.
wget https://releases.hashicorp.com/terraform/1.14.3/terraform_1.14.3_linux_amd64.zip
The file we downloaded is a compressed "zip" archive, like a suitcase containing our tool. We need to open it using the unzip command.
unzip terraform_1.14.3_linux_amd64.zip
Now we have the terraform program, but it is currently stuck in this specific folder. We want to be able to run it from anywhere in the system, just like standard commands such as ls or cd.
To do this, we move it to a special system folder called /usr/local/bin. This directory is like a public toolbox that the system always checks when you type a command.
sudo mv terraform /usr/local/bin/
Finally, verify the installation by checking the version.
terraform -version
You should see output indicating Terraform v1.14.3.
Clean up the downloaded zip file to keep your workspace tidy:
rm terraform_1.14.3_linux_amd64.zip
Create the Blueprint
Terraform needs a blueprint to know what to build. These blueprints are written in text files that end with the .tf extension.
In this step, you will create the main configuration file. By convention, the primary file is usually named main.tf. Think of this as the main page of your recipe book.
Create a new file named main.tf in your ~/project directory. We can use the touch command to create an empty file first.
touch main.tf
Verify that the file has been created:
ls -l main.tf
At this stage, the file is empty. In the next step, we will add content to it. This file will serve as the blueprint for your infrastructure.
Configure a Provider
Now that we have our main.tf file, we need to add instructions to it.
Terraform is a general-purpose tool. It doesn't know how to talk to Docker, AWS, or Azure by default. We need to introduce a Provider, which acts like a translator. It teaches Terraform the specific language of the platform we want to manage. In our case, we want to manage Docker containers, so we need the Docker provider.
This separation between core engine and provider plugins is what lets the same Terraform workflow pattern manage many different infrastructure systems.
Open main.tf using the text editor nano.
nano main.tf
Copy and paste the following code into the file. The first part, the terraform block, tells Terraform exactly which translator plugin to download. The second part, the provider block, activates that plugin.
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0.1"
}
}
}
provider "docker" {}
After pasting the code, save your changes and exit the editor. Press Ctrl + O to write the file, hit Enter to confirm the filename, and then press Ctrl + X to exit.
This configuration tells Terraform: "I intend to use the Docker provider, specifically version 3.0.1 or compatible."
Initialize the Project
You have the tool installed and the blueprint written, but Terraform isn't quite ready yet. It noticed in your main.tf that you need the "Docker" translator (plugin), but it hasn't downloaded it yet.
We need to run the init command. Think of this as "preparing the workshop". Terraform will read your blueprint, see what tools it is missing, and automatically download them into a hidden folder.
terraform init is therefore a project bootstrap step, not an infrastructure-change step. It prepares the working directory so later commands can understand the configuration correctly.
Run the initialization command:
terraform init
You should see output similar to this:
Initializing the backend...
Initializing provider plugins...
- Finding kreuzwerker/docker versions matching "~> 3.0.1"...
- Installing kreuzwerker/docker v3.0.2...
- Installed kreuzwerker/docker v3.0.2 (self-signed, key ID 24E54F214539BC63)
Terraform has been successfully initialized!
This command created a hidden directory named .terraform in your project folder. This is where Terraform stores the downloaded provider plugins.
You can verify this directory exists by listing all files, including hidden ones:
ls -a
You should see .terraform and .terraform.lock.hcl. The lock file ensures that future runs use the exact same provider versions, ensuring consistency.
Summary
Congratulations! You have successfully set up your Terraform environment.
In this lab, you learned:
- Installation: How to put the Terraform binary in the system path (
/usr/local/bin). - Configuration: How to create a
main.tffile, which serves as your infrastructure blueprint. - Providers: Unique "translators" that allow Terraform to talk to different platforms like Docker.
- Initialization: Using
terraform initto download the necessary plugins before starting work.
You are now ready to start defining actual resources and building infrastructure in the next lab.



