In this tutorial, I will use Terraform to deploy an EC2 Instance. Terraform is an infrastructure as code software tool created by Hashicorp. We will refer to Hashicorp's AWS documentation throughout this tutorial.
Find the code and steps for this Lab at my github page linked here.
Terraform can build, modify, and version infrastructure safely and efficiently. Terraform can manage existing and popular service providers like AWS, Azure, Google Cloud. It can also handle custom in-house solutions.
Terraform is similar to Cloudformation, but they have some significant differenes.
- Download the terraform binary file
- If you are a Mac user with
homebrewinstalled:- type the following on your terminal
brew install terraform - Skip to step
5
- type the following on your terminal
- Extract the zip file.
- Identify Terraform's binary executable file
- Ensure that the terraform binary file is available on the PATH.
Type the following on shell or terminal in folder where terraform binary is extracted
echo $"export PATH=\$PATH:$(pwd)" >> ~/.bash_profile
source ~/.bash_profileFollow this tutorial to add Terraform to PATH
- Create a new directory, (it can be named anything) and inside the directory, type the following command:
mkdir terraform-july && cd terraform-july- Paste the following code to a file called ec2.tf
- Terraform filenames end in .tf, the best practice is to have descriptive name floowed by ".tf".
provider "aws" {
access_key = "ACCESS_KEY_HERE"
secret_key = "SECRET_KEY_HERE"
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-2757f631"
instance_type = "t2.micro"
}Replace the access_key and secret_access with your AWS IAM user credentials with enough permissions attached.
You can go to IAM console on AWS to do this.
First, go to the IAM management console

Then Click on the user's name and navigate to the security credentials tab. Click create access keys

Either download the csv file or, click show keys. Now you have both the access_key and secret_key required for the terraform code above.

If you've setup the AWS CLI and have credentials stored, you may skip the credential portion. Hashicorp recommends: "If you simply leave out AWS credentials, Terraform will automatically search for saved API credentials (for example, in ~/.aws/credentials) or IAM instance profile credentials. This option is much cleaner for situations where tf files are checked into source control."
- Initialize the working directory for terraform:
terraform init"The terraform init command is used to initialize a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times."
- Provision the ec2 with this command:
terraform applyLogin to the AWS management console and navigate to the EC2 management console. Verify if an instance got provisioned. Once that's done, from your terminal, command prompt or shell, destory the resources:
terraform destroyThat's it! You just installed Terraform and used it to provison an EC2 instance.