Setup Terraform with AWS on Mac
Tags: terraform, aws, devops, iac
Published: 2021-12-20 19:10:50

---
title: Setup Terraform with AWS on Mac
published: true
date: 2020-08-25 19:06:00 UTC
tags: terraform,aws,devops,iac
canonical_url: https://jamesmiller.blog/terraform-aws-mac-setup/
cover_image: https://i1.wp.com/jamesmiller.blog/wp-content/uploads/2020/08/impact3.jpg
---

This tutorial will show you how to setup Terraform with AWS on Mac OS.

Terraform is a Infrastructure-as-Code framework that allows you to write a configuration file for your cloud infrastructure, that you can then instantise for different deployments.

Once a Terraform configuration file is written, you can create and delete your cloud infrastructure in a matter of seconds.

**To get started with Terraform, follow these four steps:**

### 1. Create Access and Secret Keys

Log in to AWS, go to IAM, users, add user and create a user with programmatic access.

![Adding a user called 'Terraform' with programmatic access in IAM settings within AWS](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-11.08.01.png?fit=640%2C468&ssl=1)
_Adding a user called ‘Terraform’ with programmatic access in IAM settings within AWS_

Provide with administrator access, click next and then create user.

![Add Administrator access to that user so that it has permissions to create any AWS resource](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-11.08.47.png?fit=640%2C476&ssl=1)
_Add Administrator access to that user so that it has permissions to create any AWS resource_

Download the CSV to take note of both the Access Key ID and Secret Access Key.

![Make note of both the Access key ID and the Secret access key (both are hidden in this image)](https://i0.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-11.09.38.png?resize=640%2C282&ssl=1)
_Make note of both the Access Key ID and the Secret Access Key (both are hidden in this image)_

### 2. Installing Terraform on Mac

Install Homebrew on your Mac by going to the url [https://brew.sh](https://brew.sh) and run the curl script within your terminal window.

![Install Homebrew - a package manager for Mac OS that helps making software installs quicker and easier](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-11.32.49.png?resize=640%2C434&ssl=1)
_Install Homebrew – a package manager for Mac OS that helps with installing software quicker and easier_

Open terminal and enter the below command to install Homebrew:

```
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
```

Then install tfswitch with the below command:

```
brew install warrensbox/tap/tfswitch
```

Then once it is installed run the below command and select the latest version of terraform:

```
tfswitch
```

![Tfswitch is a package that allows you to select a version of terraform to install, once selected - tfswitch handles the install process for you](https://i1.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-11.36.28.png?fit=640%2C124&ssl=1)
_Tfswitch is a package that allows you to select a version of terraform to install, once selected – tfswitch handles the install process for you_

Once installed, enter the below command to confirm it has been set up correctly.

```
terraform --version
```

![Once installed, the command 'terraform --version' displays the version of Terraform that is present on your machine](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-11.38.50.png?resize=640%2C52&ssl=1)
_Once installed, the command ‘terraform –version’ displays the version of Terraform that is present on your machine_

### 3. Visual Studio Code setup

Go to [https://code.visualstudio.com/download](https://code.visualstudio.com/download) and install Visual Studio Code.

![Visual studio download page, be sure to install the right version for your Operating System](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-22.54.55.png?resize=640%2C462&ssl=1)
_Visual studio download page, be sure to install the right version for your Operating System_

Open Visual Studio Code, click the gear icon at the bottom left hand side of the page, select ‘Extensions’ and install the ‘Terraform’ and ‘Terraform doc snippets’ plugins.

![The Terraform plugin allows Visual Studio Code to recognise a .tf file extension as a Terraform script, it also allows syntax colouring for your code.](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-11.47.20.png?fit=640%2C243&ssl=1)
_The Terraform plugin allows Visual Studio Code to recognise a .tf file extension as a Terraform script, it also allows syntax colouring for your code._

![The Terraform doc snippets plugin provides Visual Studio with the ability to provide you with examples of Terraform code snippets as you're writing your script](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-12.00.30.png?fit=640%2C230&ssl=1)
_The Terraform doc snippets plugin provides Visual Studio with the ability to provide you with examples of Terraform code snippets as you’re writing your script_

Create a folder called Terraform and create two files called provider.tf and main.tf, so that you can begin creating AWS resources.

![Project set up is now complete: provider.tf is ready to recieve AWS details and main.tf is ready to receive instructions on what resources it will be generating](https://i0.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-11.50.24.png?fit=640%2C211&ssl=1)
_Project set up is now complete: provider.tf is ready to recieve AWS details and main.tf is ready to receive instructions on what resources it will be generating_

### 4. Create first AWS resource

Enter Access and Secret Keys, as well as region – into provider.tf.

```
provider "aws" { 
   access_key = "{YOUR ACCESS KEY}" 
   secret_key = "{YOUR SECRET KEY}"
   region = "eu-west-1"
}
```

![Provider.tf requires your access + secret key's from your AWS user, as well as your selected region - so that Terraform has AWS account details to generate resources with](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-12.07.36.png?fit=640%2C145&ssl=1)
_Provider.tf requires your Access + Secret Key’s from your AWS user, as well as your selected region – so that Terraform has AWS account details to generate resources with_

As a basic test to see if Terraform has been set up correctly, write the below code in main.tf that creates a VPC.

```
resource "aws_vpc" "myfirstvpc" {
   cidr_block = "10.0.0.0/16"
}
```

![main.tf requires the resources that you plan on being generated to be declared in it, in this case we are creating a vpc](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-11.58.31.png?resize=640%2C174&ssl=1)
_main.tf requires the resources that you plan on being generated to be declared in it, in this case we are creating a VPC_

To initialise terraform so that you can begin the process of generating AWS resources, enter the below command into your terminal window.

```
terraform init
```

![The 'terraform init' command initialises the terraform environment (e.g the aws settings and local plugins) so that it is ready to receive further commands](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-12.01.13.png?fit=640%2C337&ssl=1)
_The ‘terraform init’ command initialises the terraform environment (e.g the AWS settings and local plugins) so that it is ready to receive further commands_

To prepare the code you’ve written in main.tf to be deployed, enter the below command into your terminal window:

```
terraform plan
```

![The 'terraform plan' command makes Terraform analyse your code so that you can confirm you're happy with the deployment it is has been programmed to carry out](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-12.07.58.png?fit=640%2C641&ssl=1)
_The ‘terraform plan’ command makes Terraform analyse your code so that you can confirm you’re happy with the deployment it is has been programmed to carry out_

Deploy your AWS resources to the cloud by entering the below ‘terraform apply’ command.

```
terraform apply
```

![Once the 'terraform apply' command has been entered, it will not execute until you have confirmed you're happy with the resources it is about to create - type 'yes' to allow the deployment to execute](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-12.10.13.png?fit=640%2C680&ssl=1)
_Once the ‘terraform apply’ command has been entered, it will not execute until you have confirmed you’re happy with the resources it is about to create – type ‘yes’ to allow the deployment to execute_

Checking the AWS console reveals the newly generated VPC (at the top) along with the default VPC (at the bottom).

![The top vpc si the one that has just been generated - this can be confirmed by checking it's CIDR Block, which matches our main.tf file's configuration](https://i1.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-12.11.30.png?resize=640%2C153&ssl=1)
_The top VPC is the one that has just been generated – this can be confirmed by checking it’s CIDR Block, which matches our main.tf file’s configuration_

To spin down the resources you’ve generated from your main.tf configuration file, enter ‘terraform destroy’ into your terminal window:

```
terraform destroy
```

![By entering the 'terraform destroy' command, it will attempt to remove all resources that it generated from your main.tf configuration file, enter 'yes' to confirm you're happy and the command will be executed](https://i1.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-12.12.36.png?fit=640%2C444&ssl=1)
_By entering the ‘terraform destroy’ command, it will attempt to remove all resources that it generated from your main.tf configuration file, enter ‘yes’ to confirm you’re happy and the command will be executed_

The deletion of the resource can be validated in the AWS console, by checking that it no longer exists.

![The deletion of new VPC has worked and it can no longer be seen in the AWS web console](https://i1.wp.com/jamesmiller.blog/wp-content/uploads/2020/06/Screen-Shot-2020-06-20-at-12.12.48.png?resize=640%2C134&ssl=1)
_The deletion of new VPC has worked and it can no longer be seen in the AWS web console_

### Conclusion

This is a really exciting technology that I’m keen to learn more about. It could have easily made it in to [the post about my favourite technologies](https://jamesmiller.blog/my-ideal-creative-technology-stack/) if I’d written it a little later.

I’ll probably follow up on this post with next steps on how to use Terraform, in the meantime I hope this was helpful in showing you how to setup Terraform with AWS on Mac.