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.
Table of Contents
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:
Setup AWS Permissions
Log in to AWS, go to IAM, users, add user and create a user with programmatic access.
Provide with administrator access, click next and then create user.
Download the CSV to take note of both the Access Key ID and Secret Access Key.
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.
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
Once installed, enter the below command to confirm it has been set up correctly.
terraform --version
Visual Studio Code setup
Go to https://code.visualstudio.com/download and install Visual Studio Code.
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.
Create a folder called Terraform and create two files called provider.tf and main.tf, so that you can begin creating AWS resources.
Create first AWS resource
provider "aws" { access_key = "{YOUR ACCESS KEY}" secret_key = "{YOUR SECRET KEY}" region = "eu-west-1" }
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" }
To initialise terraform so that you can begin the process of generating AWS resources, enter the below command into your terminal window.
terraform init
To prepare the code you’ve written in main.tf to be deployed, enter the below command into your terminal window:
terraform plan
Deploy your AWS resources to the cloud by entering the below ‘terraform apply’ command.
terraform apply
Checking the AWS console reveals the newly generated VPC (at the top) along with the default VPC (at the bottom).
To spin down the resources you’ve generated from your main.tf configuration file, enter ‘terraform destroy’ into your terminal window:
terraform destroy
The deletion of the resource can be validated in the AWS console, by checking that it no longer exists.
Conclusion
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.