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.
Adding a user called ‘Terraform’ with programmatic access in IAM settings within AWSProvide with administrator access, click next and then create user.
Add Administrator access to that user so that it has permissions to create any AWS resourceDownload 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.
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 Create first AWS resource
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 withAs 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 VPCTo 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 commandsTo 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 outDeploy 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 executeChecking the AWS console reveals the newly generated VPC (at the top) along with the default VPC (at the bottom).
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 configurationTo 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 executedThe 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.