DEV Community

Cover image for Setup Terraform with AWS on Mac
James Miller
James Miller

Posted on • Originally published at jamesmiller.blog on

Setup Terraform with AWS on Mac

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
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
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)
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 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
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)"
Enter fullscreen mode Exit fullscreen mode

Then install tfswitch with the below command:

brew install warrensbox/tap/tfswitch
Enter fullscreen mode Exit fullscreen mode

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

tfswitch
Enter fullscreen mode Exit fullscreen mode

Tfswitch is a package that allows you to select a version of terraform to install, once selected - tfswitch handles the install process for you
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
Enter fullscreen mode Exit fullscreen mode

Once installed, the command 'terraform --version' displays the version of Terraform that is present on your machine
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 and install Visual Studio Code.

Visual studio download page, be sure to install the right version for your Operating System
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.
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
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
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"
}
Enter fullscreen mode Exit fullscreen mode

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
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"
}
Enter fullscreen mode Exit fullscreen mode

main.tf requires the resources that you plan on being generated to be declared in it, in this case we are creating a vpc
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
Enter fullscreen mode Exit fullscreen mode

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
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
Enter fullscreen mode Exit fullscreen mode

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
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
Enter fullscreen mode Exit fullscreen mode

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
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
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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)