Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Deploying Infrastructure with Terraform | Automating Infrastructure in DevOps
Introduction to DevOps

bookDeploying Infrastructure with Terraform

You will create an environment using Terraform where you can run and configure applications. One of the environments you will use is Ubuntu.

Note
Definition

Ubuntu is one of the most popular operating systems based on Linux. It is widely used for servers and development environments. Ubuntu is lightweight, stable, and has a large community, which makes it an ideal choice for someone who is just starting out.

In this example, Ubuntu will act as a container "mini-computer", where you can safely experiment, install programs, run services, and connect automation tools such as Ansible.

Terraform, in this scenario, functions as a builder of the environment according to instructions. It creates the Ubuntu container completely from code, without any manual setup. You can imagine it as ordering a small pre-assembled computer online: it arrives ready to use, and you can immediately install and configure all the software and applications that you need.

Creating a Terraform Project

The first step is to create a folder for your Terraform project. This folder will store all the configuration files, keeping them organized and separate from other files on your computer.

Open a terminal (Linux or macOS) or Command Prompt (Windows) and enter the following commands:

This command creates a folder called terraform-ubuntu and then navigates into it.

Next, create the main configuration file where you will write your Terraform instructions. This file will be called main.tf. Use the following command:

Windows:

macOS/Linux:

The main.tf file is where you define everything Terraform should do, including downloading the Ubuntu image and creating a container.

Writing the Ubuntu Configuration

Open the main.tf file in a text editor and paste the following code:

main.tf

main.tf

copy
Note
Note

This file is written in HashiCorp Configuration Language (HCL), which is Terraform's own language for defining infrastructure. You can find the official documentation here: Terraform HCL Documentation.

Once this configuration is applied, you will have a live Ubuntu container that you can use for experiments or to connect with Ansible for automation.

Running Terraform

Once your configuration is ready, run the following three commands to create the container.

Initialize the project – this downloads the Docker provider and prepares Terraform:

Preview the plan – this shows what Terraform will create before actually doing it:

Apply the configuration – this creates the Ubuntu container:

At the end of the Terraform setup, it generates an action plan showing what will be created, changed, or destroyed. You need to type yes to confirm before it proceeds. After that, Terraform applies the changes and reports when it's done.

Verifying the Result

To enter the container and interact with it like a normal Ubuntu system, use:

This means you are currently inside the container, which acts like a small, isolated environment. You can manually install packages, run services, and experiment with applications here.

However, if you have many containers, doing all of this by hand would be slow and tedious. In the next chapter, you'll use Ansible to automate setup and installations across all containers at once.

1. What is the purpose of the docker_image resource in Terraform?

2. What is the role of terraform init?

question mark

What is the purpose of the docker_image resource in Terraform?

Select the correct answer

question mark

What is the role of terraform init?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

What should I do if I encounter errors during the Terraform setup?

Can you explain how to customize the Ubuntu container configuration?

How do I automate the setup inside the container using Ansible?

Awesome!

Completion rate improved to 3.7

bookDeploying Infrastructure with Terraform

Swipe to show menu

You will create an environment using Terraform where you can run and configure applications. One of the environments you will use is Ubuntu.

Note
Definition

Ubuntu is one of the most popular operating systems based on Linux. It is widely used for servers and development environments. Ubuntu is lightweight, stable, and has a large community, which makes it an ideal choice for someone who is just starting out.

In this example, Ubuntu will act as a container "mini-computer", where you can safely experiment, install programs, run services, and connect automation tools such as Ansible.

Terraform, in this scenario, functions as a builder of the environment according to instructions. It creates the Ubuntu container completely from code, without any manual setup. You can imagine it as ordering a small pre-assembled computer online: it arrives ready to use, and you can immediately install and configure all the software and applications that you need.

Creating a Terraform Project

The first step is to create a folder for your Terraform project. This folder will store all the configuration files, keeping them organized and separate from other files on your computer.

Open a terminal (Linux or macOS) or Command Prompt (Windows) and enter the following commands:

This command creates a folder called terraform-ubuntu and then navigates into it.

Next, create the main configuration file where you will write your Terraform instructions. This file will be called main.tf. Use the following command:

Windows:

macOS/Linux:

The main.tf file is where you define everything Terraform should do, including downloading the Ubuntu image and creating a container.

Writing the Ubuntu Configuration

Open the main.tf file in a text editor and paste the following code:

main.tf

main.tf

copy
Note
Note

This file is written in HashiCorp Configuration Language (HCL), which is Terraform's own language for defining infrastructure. You can find the official documentation here: Terraform HCL Documentation.

Once this configuration is applied, you will have a live Ubuntu container that you can use for experiments or to connect with Ansible for automation.

Running Terraform

Once your configuration is ready, run the following three commands to create the container.

Initialize the project – this downloads the Docker provider and prepares Terraform:

Preview the plan – this shows what Terraform will create before actually doing it:

Apply the configuration – this creates the Ubuntu container:

At the end of the Terraform setup, it generates an action plan showing what will be created, changed, or destroyed. You need to type yes to confirm before it proceeds. After that, Terraform applies the changes and reports when it's done.

Verifying the Result

To enter the container and interact with it like a normal Ubuntu system, use:

This means you are currently inside the container, which acts like a small, isolated environment. You can manually install packages, run services, and experiment with applications here.

However, if you have many containers, doing all of this by hand would be slow and tedious. In the next chapter, you'll use Ansible to automate setup and installations across all containers at once.

1. What is the purpose of the docker_image resource in Terraform?

2. What is the role of terraform init?

question mark

What is the purpose of the docker_image resource in Terraform?

Select the correct answer

question mark

What is the role of terraform init?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
some-alt