Skip to the content.

Beginners Guide to Terraform

Terraform is a popular Infrastructure as Code (IaC) tool that allows you to manage and provision cloud resources using declarative configuration files. In this beginner guide, we’ll walk you through getting started with Terraform and show you how to set up and manage your first project.

Table of Contents

Prerequisites

Before we get started, make sure you have:

Installation

To install Terraform, follow the instructions for your operating system:

Verify the installation by running:

terraform -version

Setting up your first project

  1. Create a new directory for your project:
mkdir terraform-project && cd terraform-project
  1. Create a main.tf file:
touch main.tf
  1. Open the main.tf file and configure your provider. For example, if you’re using AWS, your configuration would look like this:
provider "aws" {
  region = "us-west-2"
}

Initializing Terraform

  1. In your project directory, run:
terraform init

This command initializes your Terraform project and downloads the necessary provider plugins.

Creating a resource

  1. In the main.tf file, define a resource. For example, to create an AWS S3 bucket:
resource "aws_s3_bucket" "example" {
  bucket = "my-example-bucket"
  acl = "private"
}
  1. Save the file.

Applying changes

  1. Run the following command to review the changes Terraform will apply:
terraform plan
  1. To apply the changes, run:
terraform apply
  1. Confirm the changes by typing yes and pressing Enter.

Destroying resources

  1. To destroy the resources you’ve created, run:
terraform destroy
  1. Confirm the destruction of resources by typing yes and pressing Enter. This will remove the resources you’ve created using Terraform.

Further learning

This beginner guide provided a basic introduction to Terraform, but there’s a lot more to explore! Here are some recommended resources for further learning:

  1. Terraform documentation
  2. Terraform tutorials by HashiCorp
  3. Managing variables in Terraform
  4. Creating and using Terraform modules
  5. Importing existing resources into Terraform

Happy Terraforming!