Terraform 101

Understand the core Terraform concepts and the corresponding commands.

Our first Terraform module

We have created and initialized a new directory named ltthw_resources with hello_local_file.tf file, where we can safely work with Terraform without fear of breaking anything else. We have added content and filename in the hello_local_file.tf file.

Next, we run the commands mentioned in the lesson one by one and observe the output.

resource "local_file" "hello_local_file" {
   content = "Hello terraform local!"
   filename =  "${path.module}/hello_local_file.txt"
The ltthw_resources module

Let’s stop here and think about what we expect that file to do when Terraform runs it. Try to guess what the resource will be and what ${path.module} does.

Don’t worry about finding the answer and being right. Just get a feel for what looking things up in Terraform looks like. We’ll look at resources we want later.

Now let’s run ls -1a in the code snippet above:

ls -1a

We’ll see ., .. and hello_local_file.tf in the output of ls -1a.

The . represents the current directory, and the .. represents the parent directory. These are virtual files put in every directory in the file system so we can always refer to the parent directory or the current one. The other file is the one we just created.

We’ll check what is in the directory to show what happens when we bring Terraform in, as given below:

Press + to interact
terraform init
ls -1a

When we run terraform init, we’ll see some output as Terraform initializes the environment. If we run ls -1a again we see a lock file (.terraform.lock.hcl) and an extra .terraform directory.We can also choose to explore the directory if we want to.

Terraform downloads the local_file plug-in and places it in this directory. Now we’re ready to run this module and create the following local file:

terraform apply

Now we’ll get a prompt asking us if we are sure we want to go ahead. This prompt will appear whenever we run this command or if there is something to change. We’ll need to type yes to approve.

Before we do that, read the output carefully. It explains a phase called plan where Terraform takes our module and compares its desired state with what it thinks is the current state in the real world. We’ll cover this topic later. Finally, we get the following summary of the plan:

Plan: 1 to add, 0 to change, 0 to destroy

Since there’s something to change, we get the prompt asking us if we are sure we want to go ahead and make a change. Type yes to continue.Type yes to continue.

Enter a value: yes

We’ll see the output at the end which tells us what was changed.

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Now we are ready to run this module and create the local file.

Again, read the output carefully before running ls -1a in the terminal.

ls -1a

Now we see that there are two new files in the directory, terraform.tfstate and the hello_local_- file.txt.

Now remove the hello_local_file.txt file and rerun terraform apply -auto-approve. It will simply approve the terraform apply. What do you think will happen?

Press + to interact
rm hello_local_file.txt
ls -1a
terraform apply
ls -1a

Did it do what you expected? Let’s change the file and see what happens.

Press + to interact
echo ' I just moved in' >> hello_local_file.txt
cat hello_local_file.txt
terraform apply
cat hello_local_file.txt

Terraform vs. traditional configuration management tools

If you’ve used configuration management tools like Chef, Puppet, or Ansible before, then you may realize that Terraform can do similar things. It can examine the state of something and bring it in line with the state that was specified, as we saw above.

There are key differences between Terraform and other tools, and this is because Terraform is designed for infrastructure rather than software configuration.

When managing files, Terraform looks pretty similar to configuration management tools. However, as we move on to manage things such as load balancers, virtual private networks, or fleets of machines, it’ll become clear that Terraform behaves in a different way to those other tools.

For example, configuration management tools are usually agent-based (it runs on the machine it is managing), or ssh-based (it goes to the machine it is managing and does work on it). In other words, they are managing from within the system they are affecting. Terraform, by contrast, must manage from outside since the management takes place via a remote API.

Fundamentally, though, it is worth keeping in mind that Terraform is a configuration management tool designed for what our software runs on rather than for which software runs where.


Just run the following commands to initiate cleanup:

Press + to interact
terraform destroy -auto-approve
cd ..
rm -rf ltthw_resources

What we learned

We’ve just written and run our first Terraform module.

It might not have been what you expected. We affected a local file with our module rather than cloud infrastructure like an AWS EC2 instance. This is deliberate. The point of this course is to illustrate how Terraform works in simple ways so that you grasp the key concepts as quickly as possible. Then we can apply the same concepts to our infrastructure.


Which command sets up a Terraform module?


The terraform init


The terraform setup command


There is no setup command


The tf setup

Question 1 of 30 attempted