Getting Started with Terraform – First Terraform Configuration (Part 2)

The first step in using Terraform is to create a configuration file. This file defines the resources you want to create, configure, or delete. We’ll walk you through the basics of creating a simple configuration file and running a Terraform plan.

Step 1: Installing Terraform

The first step is to install Terraform on your machine. Follow the installation instructions in the previous blog post to install Terraform on your operating system.

Step 2: Writing your first Terraform configuration

Once you have installed Terraform, the next step is to write your first Terraform configuration file. A Terraform configuration file is written in HashiCorp Configuration Language (HCL) and describes the desired state of your infrastructure.

Create a new directory for your Terraform project and create a new file named main.tf inside the directory. This file will contain your Terraform configuration.

In this example, we will create an AWS EC2 instance. Here’s what your main.tf file should look like:

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

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

This configuration file specifies that we want to use the aws provider with the us-east-1 region, and we want to create an EC2 instance with the ami-0c55b159cbfafe1f0 Amazon Machine Image (AMI) and the t2.micro instance type.

Step 3: Initializing your Terraform configuration

Before you can apply your Terraform configuration, you need to initialize your Terraform workspace. This is done by running the terraform init command in your project directory.

$ cd my-terraform-project
$ terraform init

This command will download the necessary provider plugins and initialize your Terraform workspace.

Step 4: Planning your Terraform configuration

Once your workspace is initialized, you can plan your Terraform configuration by running the terraform plan command. This will show you a preview of the changes that Terraform will make to your infrastructure.

$ terraform plan

This command will show you a preview of the resources that will be created, updated, or deleted.

Step 5: Applying your Terraform configuration

After you have reviewed the Terraform plan and are satisfied with the changes, you can apply the changes to your infrastructure by running the terraform apply command.

$ terraform apply

This command will create or update the resources specified in your Terraform configuration.

Step 6: Destroying your Terraform resources

When you’re done with your Terraform resources, you can destroy them by running the terraform destroy command.

$ terraform destroy

This command will delete all the resources that were created by your Terraform configuration.

Congratulations! You have successfully created your first Terraform configuration and applied it to your infrastructure. You can continue learning Terraform by exploring more advanced topics such as modules, remote state, and Terraform Cloud.