Getting Started with Terraform: A Step-by-Step Terraform Tutorial

Welcome to this beginner-friendly Terraform tutorial! In this guide, we will walk you through the basics of Terraform and how to use it to manage your infrastructure as code. By the end of this tutorial, you’ll be well on your way to provisioning and managing cloud resources with ease. Let’s dive in!

What is Terraform?

Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define and manage your cloud infrastructure using a declarative configuration language. With Terraform, you can define the desired state of your infrastructure, and it will automatically create, modify, or delete resources to match that state.

Prerequisites:

To follow along with this Terraform tutorial, you will need the following:

Terraform: Make sure you have Terraform installed on your local machine. You can download it from the official website: Terraform Downloads

Cloud Provider Account: For this tutorial, we’ll use AWS (Amazon Web Services) as our cloud provider. Sign up for an AWS account if you don’t have one already.

Step 1: Setting up AWS Credentials

Before you can use Terraform to manage AWS resources, you need to set up your AWS credentials. If you have the AWS CLI installed, you can run aws configure and enter your access key, secret key, and default region. Alternatively, you can set the environment variables AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_DEFAULT_REGION with your AWS credentials.

Step 2: Creating a Terraform Configuration File

Create a new directory for your Terraform project and navigate into it using the terminal or command prompt.

Inside the project directory, create a file named main.tf. This is where we will define our infrastructure configuration using the HashiCorp Configuration Language (HCL).

In main.tf, add the following code to create a basic AWS EC2 instance:

In this example, we define an AWS provider with the region set to “us-west-2”. We also declare an EC2 instance resource with the specified AMI (Amazon Machine Image) and instance type.

Step 3: Initializing Terraform

Now that we have our configuration file, we need to initialize Terraform to download the AWS provider plugin and set up the working directory. In the terminal, run the following command:

Terraform will download the necessary provider plugins and create a hidden .terraform directory in your project.

Step 4: Terraform Plan

Before applying any changes, it’s a good practice to preview the actions that Terraform will take. This can be done using the terraform plan command:

Terraform will analyze your configuration and display the changes it will make to your infrastructure. Review the output to ensure it aligns with your intentions.

Step 5: Terraform Apply

If you are satisfied with the changes proposed by the terraform plan, you can apply those changes to your cloud provider using the terraform apply command:

Terraform will prompt you to confirm that you want to apply the changes. Type yes and press Enter to proceed.

Step 6: Clean Up

Once you’re done experimenting with Terraform, it’s essential to clean up your resources to avoid incurring unnecessary costs. To destroy the resources created in the previous steps, run:

Terraform will ask for confirmation before destroying the resources. Type yes and press Enter to proceed.

Terraform Tutorial wrap up

Congratulations! You’ve successfully completed this Terraform tutorial. You’ve learned the basics of Terraform, how to create a simple configuration file, and how to apply and manage your infrastructure as code using Terraform. As you continue your Terraform journey, remember that it’s a powerful tool that can help you manage complex cloud environments with ease and efficiency.

To speed up your Terraform learning curve, Holori has the perfect tool for you, a Terraform diagram generator. Indeed visualizing your terraform can be helpful to improve your Terraform understanding. By associating Holori to your CI/CD, you can have a terraform diagram in the comments of your pull requests. By clicking on it you can access Holori Dashboard and see for each ressource, its terraform attributes.

You can sign up here to try the tool : https://app.holori.com/register

terraform diagram

Happy Terraforming!

Share This Post

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Holori

Holori Adds Scaleway to Its FinOps Platform!

Paris, December 18, 2024 — Holori is thrilled to announce that our cloud cost management platform now supports Scaleway! Building on our existing integrations with