How to Deploy an AWS EC2 Instance Using Terraform



Deploying AWS EC2 instances with Terraform is an excellent way to build infrastructure as code, and automate the provisioning, deployment and maintenance of resources to EC2 as well as custom solutions. This guide will walk you through the basics of configuring a single instance using a simple configuration file and the Terraform provider.
Before we begin, there are a couple of assumptions:

  1. You have access to AWS and can create EC2 instances.
  2. You are comfortable with the command line (PowerShell).
  3. You have Microsoft Visual Studio Code (VSCode) installed (or the editor of your choice).

There are three actions covered here:
1. Configure
2. Initialize
3. Apply

Let’s get started!


Head over to the Terraform download page and get the latest version for your operating system. For the purposes of this example, we are using a Windows 10 host, and PowerShell 6.1.1.
The downloaded file will contain a single executable named (you guessed it) terraform.exe. This executable will need to be located in the same directory as the Terraform configuration file you will create shortly. In the example below, the


file has already been extracted to the directory used for this guide:


For the purposes of this demonstration, create a new file called using the following command in your PowerShell terminal:

code .\

This assumes you have installed VSCode installed, and if so, creates a blank file called

This will be our Terraform configuration file.

Below is a sample of what can be contained within the

configuration file:
provider "aws" {
  region 	= "eu-west-2"
  shared_credentials_file = "c:/Users/admin/.aws/credentials"
  profile = "TechSNIPS"
resource "aws_instance" "example" {
  ami       	= "ami-0c09927662c939f41"
  instance_type = "t2.micro"
  tags {
	name = "TESTVM"

As you can see, we have some parameters defined. The first is the Provider block that we have set to “aws”. Within this code block, we need to define the configuration of the resource we are deploying. So, in this block, you are assigning a region, and the path to some saved API credentials or IAM instance profile credentials. Alternatively, you can save your AWS access and secret keys here by using the ACCESS_KEY_HERE and SECRET_KEY_HERE configurations. For more information on the different providers you can use with Terraform, see the documentation here.

The Resource block contains the name of the resource type you are working with and the associated configuration for the resource. In the above example, we are deploying a microserver instance in the free tier. For good measure, we include a tag to make it easy to filter for this instance in the AWS EC2 dashboard.

Once you are satisfied with your configuration, save the file. Your directory should now contain the Terraform executable and your new Terraform configuration file:


Let’s take a quick look at the available commands for a moment:

The commands we are going to focus on are ‘init’, ‘apply’, and ‘show’. Init, which will initialize the configuration, will make the configuration ready for application to our EC2 instance. Type the following command to begin:

.\terraform.exe init

You should see the output below:

Terraform is ready to begin applying the configuration as you have outlined.


Now it’s time to begin deploying the configuration to your infrastructure. Enter the following command and press Enter:

.\terraform.exe apply

You should then see the configuration actions that are about to be deployed. Note that these fields are what we referenced earlier while building the configuration file:

After the configuration is loaded, you will be asked to confirm your planned actions. Type Yes and press Enter. After a few moments, you will see a message stating that the application is complete. To confirm the EC2 instance you have deployed, you can run the following command to obtain information about the public/private IP address, ID, instant state and availability zone, to name a few:

.\terraform.exe show

You should then see output similar to the example below:

Congratulations, you have just deployed infrastructure from code!

You can learn more about Terraform by visiting the HashiCorp documentation pages. To see a demo that includes additional management task demonstrations, be sure to check out .


Leave a Comment

Your email address will not be published. Required fields are marked *

Skip to toolbar