Skip to content

Lesson 1 of 6

Set up Terraform and choose a provider

Install Terraform, create a project folder, write a provider block, and run terraform init to download the provider.

6 minInfrastructure

After this lesson you will be able to install Terraform, set up a project folder, write a provider block, and run terraform init to download the provider.

Install Terraform

Terraform is a tool that lets you describe cloud resources in text files and then create or change those resources. You write what you want, and Terraform works out how to make it happen.

To install Terraform, go to the Terraform downloads page and download the package for your operating system. Unzip the package and put the terraform binary somewhere on your PATH, such as /usr/local/bin on Linux or macOS, or a folder that is in your PATH on Windows.

Check that Terraform is installed by opening a terminal and running:

terraform version

You should see a version number and a message that Terraform is ready.

Create a project folder

Create a new folder for this course. You can name it anything you like, for example web-server.

mkdir web-server
cd web-server

All the Terraform files for this course will go in this folder.

Write a provider block

Terraform needs to know which cloud provider you want to use. A provider is a plugin that lets Terraform talk to a cloud service, such as AWS, Azure, or Google Cloud. In this course we will use AWS, but the ideas are the same for other providers.

Create a new file called main.tf in your project folder. This file will hold the Terraform configuration.

Open main.tf in a text editor and add the following lines:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

Let's go through this file. The terraform block tells Terraform which providers this project needs. The required_providers block lists the AWS provider and says where to get it from. The source is the address of the provider in the Terraform registry, and the version constraint says to use any version in the 5.x series. The provider block configures the AWS provider. Here we set the region to us-east-1, which is one of the AWS regions. You can change this to a region closer to you.

Run terraform init

Before Terraform can use the provider, it needs to download it. The command terraform init reads the configuration and installs the providers it needs.

Run this command in your project folder:

terraform init

You will see output like this:

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 5.0"...
- Installing hashicorp/aws v5.0.0...
- Installed hashicorp/aws v5.0.0 (signed by HashiCorp)

Terraform has been successfully initialized!

The exact version number may be different, but the message should say that initialization was successful. Terraform has downloaded the AWS provider and stored it in a hidden folder called .terraform inside your project folder. You do not need to do anything with that folder.

Understand the provider version constraint

The version constraint ~> 5.0 means any version from 5.0.0 up to but not including 6.0.0. This is useful because providers often release new versions with changes. By pinning to a major version, you allow minor updates and patches but avoid breaking changes that might come with a new major version.

If you want to use a specific version, you can write version = "5.12.0". Terraform will then download exactly that version. For this course, the ~> 5.0 constraint is fine.

Check what terraform init created

After running terraform init, you will see a new file called .terraform.lock.hcl in your project folder. This file records the exact provider versions that were installed. You should keep this file in your project so that everyone working on it uses the same provider versions. Terraform also creates the .terraform directory, which holds the downloaded provider plugins. You do not need to edit or commit that directory.

Now your project is ready. In the next lesson you will add a security group to allow web traffic.

Key takeaways

  • Terraform is a tool that manages cloud resources using text files.
  • A provider is a plugin that lets Terraform talk to a cloud service.
  • The terraform block lists required providers and their versions.
  • The provider block configures a specific provider, such as setting the region.
  • terraform init downloads the providers needed for the project.

Try it

Create a new folder and write a Terraform configuration that uses the AWS provider with region eu-west-1. Run terraform init and confirm that it downloads the provider. Then change the region to ap-southeast-2 and run terraform init again. Notice that Terraform does not need to download the provider again because it is already installed. A good result is that both terraform init commands finish successfully and you see the provider version in the output.

Official documentation

Ready to use this in a job?

Rolebound applies to matching roles for you.

Start now

Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.