Easy Steps to Set Up Terraform on Mac

Fırat TONAKFırat TONAK
5 min read

This article will teach you about Terraform and how to install it on macOS. You can start using Terraform once you are done with all the steps.

This guide is created to serve as a reference for future articles, and it focuses on the installation and setup.

What is Terraform?

Terraform is an infrastructure as code (IaC) tool that allows you to manage your resources through code. With Terraform, you can create and modify your infrastructure easily and efficiently.

Terraform provides:

  1. An infrastructure-as-code approach that helps you create, modify, and delete your resources through code.

  2. Automation of resource creation, modification, and destruction, which reduces direct human interaction with resources and minimizes the risk of human error.

  3. Support for multiple cloud providers.

  4. The ability to collaborate with your team by sharing Terraform files.

  5. State management features that allow you to make and revert changes to your resources easily.

  6. The capability to handle complex and large infrastructures.

Installing Terraform

Installing Terraform is a straightforward process that requires you to visit their website.

Follow the steps based on your operating system.

Since I use macOS, I selected Homebrew on macOS. Open a terminal and execute the command brew tap hashicorp/tap.

Then execute the command brew install hashicorp/tap/terraform.

After the installation is complete, run the command brew update, and then finally, execute brew upgrade hashicorp/tap/terraform.

Testing Terraform

After following all the steps, we should test Terraform to verify it’s working correctly. To do this, execute the command terraform -help. You should see the following output if Terraform is installed on your machine.

Configuring AWS for Terraform

We need to make sure that aws cli is installed on our computer before we start managing AWS resources. Let's install the aws cli by executing the command brew install awscli. You should see a similar output at the end of the process.

Then, let's verify the installation by executing the command aws --version.

Since we can see the version of the aws cli, we can run aws configure to configure AWS credentials. First, go to the AWS Management Console and create an Access Key and Secret Access Key. Search for IAM and select the first result.

Click on Users and then click on Create User

Enter a User name then click on Next

On the Set Permissions page, select Attach policies directly and search for AdministratorAccess. Assign it to the user, as we will need admin access to test our Terraform configuration. Then click on Next.

Review all the details and click on Create User if everything looks good.

If the process is successful, you should see the user in the list.

Now, we need to create an Access Key. To do this, click on the User Name, select Security Credentials, and then click on Create Access Key.

On the next page, select Command Line Interface (CLI) because we want to use the AWS CLI. Check the Confirmation box, then click on Next.

You can enter a description if you want, as it is optional, and then click on Create Access Key.

On the next page, you will see your Access Key and Secret Access Key. You must save these details because you will not see Secret Access Key again after clicking on Done. You can either download them or save them to a notepad.

Now, go back to the terminal and create a folder by executing the command mkdir <folder_name>. Navigate to the folder you just created and run code . to open it in VS Code.

Open a Terminal and execute the command aws configure, then enter your AWS credentials.

Let's go to Extensions in VS Code and install the HashiCorp Terraform extension.

Testing Terraform Configuration

Now, we can create a Terraform file and test our configuration to verify it's working. Create a file named main.tf and copy the following configuration code.

data "aws_ami" "linux"{
    most_recent = true

    filter{
        name = "name"
        values = ["amzn2-ami-hvm-*-x86_64-gp2"]
    }

    filter{
        name = "owner-id"
        values = ["137112412989"] # Amazon's official AMI owner ID
    }
}

resource "aws_instance" "ecommerce_server" {
  ami           = data.aws_ami.linux.id
  instance_type = "t2.micro"

    tags = {
    Name = "Terraform Test"
  }
}

Next, we need to execute these commands in sequence:

  1. terraform init to initialize the configuration requirements

  2. terraform plan to preview what will be created

  3. terraform apply -auto-approve to create the EC2 instances

The -auto-approve flag allows us to skip the approval step after executing terraform apply. Without this flag, you would need to type yes when Terraform asks for approval.

Go to the AWS Management Console and check the EC2 instance list to verify the deployment.

If you can see Terraform Test in the instance list, it means our configuration is working, and we can use Terraform from now on to create our infrastructure on AWS.

We need to run the command terraform destroy -auto-approve to avoid additional costs.

In this article, we covered how to install and configure Terraform on macOS to manage AWS infrastructure. As a result, following these simple steps will help you set up Terraform and start managing AWS resources.

You can access the code here.

0
Subscribe to my newsletter

Read articles from Fırat TONAK directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Fırat TONAK
Fırat TONAK