Deploying an EC2 Instance in AWS using Terraform - A Step-by-Step Guide

Deploying an EC2 Instance in AWS using Terraform - A Step-by-Step Guide

In this guide, I've been using one of the most popular Infrastructure as code  tools used today in the cloud. This article runs through the steps I’ve taken to build an EC2 instance in AWS using the Terraform workflow.


Prerequisites

Before proceeding with the guide, there are a few pieces of Software we need to have in place:

The installations are straight forward. With Terraform, when I ran the .exe I was expecting installation steps but a window quickly appears and then disappears. Terraform is installed though but it isn't clear at the time.


Getting Started

Before starting I signed up for AWS Free Tier. It offers 12 months of free usage but you will need to register a debit or credit card for verification purposes. This doesn’t take long (around 5 stages to it) and then your account is active.


Create an IAM User

There are several ways in which an IAM user can be created but for this guide I chose to do it in the console. Navigate to the IAM service and select create a user in AWS. When creating the new user there is an option to “Provide user access to the AWS Management Console - optional”. I didn't select this as there is no requirement for this account to have access to the console. 


At this stage it may redirect you to the IAM Identity Center and and prompt you to enable. I enabled this, closed the window and when I selected create user again in the IAM service it didn't redirect me to the Identify Center.


I specified a username and chose the option to attach the admin policy directly to the account. 

No alt text provided for this image

This creates the user account and shows you everything which is configured for the user. I clicked on the security credentials tab which allows me to configure the security options for this user. As I’m wanting to use this account to connect to the AWS CLI, I clicked on create access key and selected the command line interface. 

No alt text provided for this image

The access key and secret access key have been created and you should see these on the screen giving you the option to copy them. This key will be needed later on in the guide. I downloaded the CSV but remembered to delete it after I'd finished with them.


Using AWS CLI

By this stage AWS CLI is installed on my Windows machine. I opened a command prompt and because I have no access key configured yet I won't be able to connect to AWS. I typed AWS configure where I was able to copy and paste my access key, secret access key and specify a default region.


Once that was complete, I checked connectivity by running aws iam get-user and it will return details of the user account you're using to connect to the CLI.


Creating the Terraform file

I opened a new file in VS Code and named it main.tf and added in the following:


provider "aws" {

  access_key = " "

  secret_key = " "

  region = " " 

}

resource "aws_instance" "ec2example" {

  ami = "ami-0cea4844b980fe49e"

  instance_type = "t2.micro"

}


Where there are quotation marks, a value will need adding in. The access key and secret access key are the same as what you used to connect to the CLI and you can choose a region of your choice where you would like the infrastructure deployed. I chose this particular ami and instance type because it was free under AWS Free Tier.


One thing to point out, I did go into the console first to double check this ami and instance type was available in the region I was deploying to as it will cause issues further down the line.  


It’s also worth noting that the .tf file you've created needs to be saved in the same folder as your terraform download. 


The Terraform Workflow

This is the init stage where the code is initialised and Terraform decided which plugins are required.  


Open a command prompt, cd to the folder where Terraform has been downloaded to which is where your terraform file should also be stored and run terraform init

No alt text provided for this image

The next stage of the workflow is Plan. Typing terraform plan into the command prompt will show you exactly what you’re planning on deploying and whether or not it’s adding, changing or destroying any infrastructure. In this example, we're adding so you'll see this reflected at the bottom of the output.

No alt text provided for this image

Check over the plan to make sure it's as you specified in the Terraform file. If you’re happy with the plan, you can now proceed to the apply stage where the infrastructure is provisioned. 


Side note, I made a mistake when making this guide and chose the wrong ami. I changed the .tf file but running terraform plan again didn't update the plan. I had to delete the .tf file and recreate it for the changes to be pulled through. 


In the command prompt, run terraform apply. You will have successfully used Terraform to deploy an EC2 Instance in AWS. I checked the console and I was able to see it.

No alt text provided for this image
No alt text provided for this image

The final stage of the Terraform workflow is destroy. This can be used to destroy the infrastructure you have just created. Run terraform destroy and typing yes, it will destroy the EC2 instance.

No alt text provided for this image




Stuart Priest

Director of Circus Skills York CIC, Kakapo Energy Ltd & AWS Devops Consultant

1y

That was a good quick introduction to using Terraform to create infrastructure. I remember being told when I first started using Terraform, to make sure you understand what the commands are actually going to do, especially the "destroy" command!

To view or add a comment, sign in

More articles by Andy Marriott

Insights from the community

Others also viewed

Explore topics