# Easy Steps to Set Up Terraform on Mac

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](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735874356231/eb9feb39-7ba5-470a-8918-a6d778dc4dbc.png align="center")

Follow the steps based on your operating system.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735874448332/b81dc401-f7a9-4ab6-bc62-af0f83b3fc5e.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735874557201/613d7aa0-8010-4fbb-9f63-df2a626dafba.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735874603488/60b16975-7b99-48e8-b129-53b569a745f9.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735874970687/aca7fbec-9b74-4a1a-a050-582a62afe9bb.png align="center")

## 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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735874811944/09bbd18a-4147-4225-b191-9a9e32409e8e.png align="center")

## 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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735875299816/5a4f1ae5-ec86-47f3-8845-6ba7955b2c06.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735875355845/fa1c97ba-484c-4143-9afe-4253c04de41b.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735953694208/71808ac6-8047-4d33-a0b5-7b63e092cbad.png align="center")

Click on `Users` and then click on `Create User`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735953784507/0c29e0a1-3548-4136-9a1c-3f4a7ac5aeab.png align="center")

Enter a `User name` then click on `Next`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735953846662/0c96cd20-dd45-4b70-8504-55fa0761063f.png align="center")

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`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735954070878/176f0ecd-136e-45ee-babf-5f70affcf792.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735954167085/baf027ad-25bd-41a8-9339-0278737eccb9.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735954268525/94e82135-922d-495a-9d37-b03fb4a18964.png align="center")

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`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735954431979/5e68eb2f-da2d-4c55-9595-1c73b77e1122.png align="center")

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`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735954621889/634f9330-8905-4904-9d0d-caf60bbdec46.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735954671014/227e9661-b5d3-483e-b497-4e17531c60aa.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735954815498/50b8d0dc-1b47-42a3-92b5-8642fca7d836.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735956406538/545964d0-1b65-42ed-81e6-29f9560ea0ed.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735956597361/8906bc8b-e0f8-495d-8f26-ca2fb860358b.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735956724980/0401f15c-edf6-4c0f-8467-c07f2fb61ab0.png align="center")

## 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`](http://main.tf) and copy the following configuration code.

```bash
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735957525080/ccf9d27c-b0ac-4e92-9848-dd38ce556f45.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735957607768/572a83b9-7ec3-40e8-b079-3bc05f309ee1.png align="center")

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](https://github.com/frttnk/Easy-Steps-to-Set-Up-Terraform-on-Mac/tree/main).
