Link Search Menu Expand Document

Launch a basic EC2 with terraform

Lesson 1

Now it’s time to launch your first EC2 instance! Remember, an EC2 instance is just a computer in the cloud. AWS is providing you with EC2 servers you can use to host your applications, servers, websites and more!

While you could launch an instance manually in the AWS EC2 console (and feel free to go do so!), the scope of Learn the Cloud is to teach you with hands-on practice in terraform specifically. Terraform takes the manual work out of it for you – after you build the template the first time, every time you deploy an EC2 instance after that, it will be as simple as running a quick command in the terminal – much easier and less time-intensive than going through the steps in the console every time!

Note: We strongly encourage you work in us-east-1, as many values will change if you do not.

Table of contents

  1. Launch a basic EC2 with terraform
  2. Key Pairs
    1. Create your first key pair
  3. Terraform - EC2
    1. Create the project structure
    2. Write some terraform code!
    3. Run terraform init
    4. Run terraform apply
    5. Run terraform destroy
  4. Next Steps
    1. Redo the lesson (optional)
    2. Move On

Key Pairs

If an EC2 instance is like your laptop, then you can think of a key pair like a password you use to unlock your laptop.

A key pair is what AWS uses to make sure you are allowed to connect to an EC2 instance. Since key pairs are like passwords for you EC2 instance, every time you launch an EC2 instance, you’ll need to designate a key pair to use. Key pairs are created in specific regions. While there are no hard and fast rules for how many key pairs you should have, a good rule of thumb is 1 key pair per environment (ie development, staging, production), per region (ie us-east-1, etc). We’ll talk more about that later though!

Create your first key pair

Before you can launch an EC2 instance, you’ll need to create a key pair:

  1. Login to the AWS console using the sign-in link you bookmarked in the Getting Started: AWS Account.
  2. In the upper left-hand corner of the home page, click Services and type key pairs into the search box. Select EC2 from the dropdown.
  3. On the left-hand side navigation pane, look for Network & Security. Click the Key Pairs section beneath it.
  4. Click Create key pair in the upper right.
  5. Enter a yourname-us-east-1 for the name of your key pair, choose pem format and click Create key pair again.

Your key pair will download to your computer. Save it somewhere you’ll remember, although you won’t need it for now.

Terraform - EC2

  • You will need to open up the code editor you downloaded and set up in the Getting Started: Code Editor section.
  • You will also need to open up your terminal.

Create the project structure

Before we build out our terraform code, in your terminal you will first need to create the directory and files for your ec2-terraform project. You will need to create a directory called ec2-terraform, and the following files: aws.tf and ec2.tf.

We suggest you test out your terminal command skills and try it without the Instructions below, but if you get stuck or just want direction expand the section below.

Instructions
  1. Create a directory called ec2-terraform:
    mkdir ec2-terraform
    
  2. Change into your new directory
    cd ec2-terraform
    
  3. Create the necessary terraform files:
    • A file for your aws information:
      touch aws.tf
      
    • A file for your ec2 resource:
      touch ec2.tf
      

      ec2-terraform-proj1

Write some terraform code!

Now that you have your project structure built, you can start writing some terraform (don’t worry - we’ll guide you along).

We have written instructions for you to follow, and, at the end, we have included a couple short videos, or gifs, for you to watch. Just click the See walk-through. arrow below.

NOTE: If you get an error in WebStorm that says it doesn’t recognize the file type (*.tf), then navigate to this link, click the blue Install to IDE button and select WebStorm from the list. This will install the terraform language recognition you need.

NOTE: If you are using Visual Studio Code and receive a similar error, navigate to this link and NEED TO FILL IN HERE>

  • In your code editor, open up your ec2-terraform project.
  • Open the aws.tf file. Copy the below code, paste it into your file, and Save (ctrl + S (Linux/Windows) or cmd + S (Mac)).
    provider "aws" {
     region  = "us-east-1"
    }
    

    This is the provider { } block we mentioned in the Getting Started: Terraform section. This code block tells terraform to build resources with aws (and not google cloud, azure, or any other cloud provider). It also tells terraform the region you want your AWS resources deployed in.

  • Open the ec2.tf file. Copy the below code, paste it into your file, and Save.
    resource "aws_instance" "learn-the-cloud-test1" {
     ami = "ami-0947d2ba12ee1ff75"
     instance_type = "t2.micro"
     key_name = "yourname-us-east-1"
    }
    

    This is a resource { } block (also mentioned in the Getting Started: Terraform section). This resource is an aws_instance named learn-the-cloud-test1. This code block tells terraform what resource you want to build in AWS. In this case, you want to build an EC2 instance (or aws_instance as terraform calls it). To build an EC2 instance, you need to give terraform the following information:

    • The ami / image_id
    • The instance_type
    • The key pair / key_name

These are called parameters. They customize the EC2 instance and provide terraform the necessary information it needs to launch an instance for you. You will learn more about all of the above. For now, we have provided you with what you need to put as long as you have been working in the us-east-1 region.

You might notice that this code feels familiar. In the Getting Started: Terraform section, you created a very similar first project. A couple differences to note:

  1. This time, we have created two files instead of one: aws.tf and ec2.tf. We’ve done this strictly for ease of organization. Terraform doesn’t care if you separate your resources into multiple files or not - as long as it is a terraform directory, terraform will handle finding the files and code for you.
  2. We’ve added one more field to our EC2 instance - key pair. Last time, we only used the required fields (instance_type and ami). You will learn more about all of these fields later, so for now just follow along and get a feel for the process.
See walk-through
  1. Finding your project’s path and opening it in WebStorm. ec2-open-proj1
  2. Pasting in the provided code into aws.tf and ec2.tf. paste-ec2-code

Run terraform init

Now that you have your first terraform code written, you get to initialize the directory.

  • In your terminal, make sure you are still in your ec2-terraform directory by typing pwd. It should print something similar to home/yourname/ec2-terraform. If you aren’t, cd into it.
  • Run the command:
    terraform init
    

    This command will initialize a terraform directory. You only need to run this command once every time you start a new project. This command tells terraform (which you downloaded on your computer in Getting Started) that this directory is a terraform directory and that the files inside it will be used to launch resources with terraform. terraform init

Run terraform apply

Now you get to deploy your EC2 instance into AWS!

  • In your terminal, make sure you are still in your ec2-terraform directory by typing pwd. It should print something similar to home/yourname/ec2-terraform. If you aren’t, cd into it.
  • Run the command:
    terraform apply
    
  • Terraform will list out the resources/changes it wants to make to your AWS account for you, then ask you for input. Type yes to allow terraform to build your EC2.

This command will apply the terraform code in you aws.tf and ec2.tf files. Everything happens behind the scenes–terraform finds the right files it needs and knows to use the default AWS profile you have stored (remember in Getting Started when we created our profile and saved our AWS Access and Secret keys? That’s where this comes in!). terraform init

  • Once terraform apply is complete (you’ll get a message that says, Apply complete!), log in to your AWS account, go to the EC2 UI (search for EC2 in the Services search box) and find your EC2 instance! Look for the following:
    • Find the ami and make sure it matches what you put in terraform.
    • Find the instance type and make sure the instance type matches what you put in terraform.
    • Make sure the instance is in the region you told terraform to use.

find-ec2-aws

Run terraform destroy

Now that you’ve seen your EC2 in AWS, you’ll run the last command you need for now, terraform destroy.

  • In your terminal, make sure you are still in your ec2-terraform directory by typing pwd. It should print something similar to home/yourname/ec2-terraform. If you aren’t, cd into it.
  • Run the command:
    terraform destroy
    

    This command will destroy all the resources terraform just built for you (right now it’s only a single EC2 instance). You’ll need to respond with yes just like you did when you ran terraform apply. While this isn’t incredibly important right now, later on in this course you will be building resources in AWS that can cost you a good bit of money if you don’t remember to delete them. Because we are building everything with terraform, you can simply run terraform destroy every time you’re done with a lesson and terraform will take care of it for you! terraform-destroy

Next Steps

You’ve now launched your first EC2 instance using terraform! How you feel about the process determines what you should do next.

Redo the lesson (optional)

If you don’t feel as comfortable as you’d like with the above lesson, you can start over from scratch in one of two ways.

  1. You can simply cd into your home directory and create a new project directory called ec2-terraform2 (note: you will need to change all references in the lesson to ec2-terraform2 instead of ec2-terraform), OR
  2. You can delete everything and begin again.
    • To do this, you need to be in the directory your ec2-terraform project is in. If you followed along, this is in your home directory.
    • Next, ls to make sure you see ec2-terraform listed. If you don’t you’ll need to find out where you stored it first. If you do, continue on.
    • Run the command rm -rf ec2-terraform. Note: This will completely remove the entire project. You will start from the very beginning of this lesson and won’t have a project folder until you create it again.
  3. Go back to the beginning of the Lesson!

Move On

Now that you feel comfortable launching EC2 instances via terraform, you can move on to the next lesson, Web Server.