How to deploy and aws ec2 instance using terraform

How to deploy an AWS Linux EC2 Instance Using Terraform

As the companies start to move to the cloud, you may find yourself needing to deploy an aws linux ec2 instance using terraform. There are 2 ways you can do this:

  • Deploy an instance using the aws gui
  • Use automation & Infrastructure as code

The latter is becoming more popular to be able to create a more consistent environment. The tool we will be talking about is Terraform. Before we get into how to deploy and aws linux ec2 instance using terraform, we must first setup terraform on our machine.

Installing Terraform on Windows

The first step is to install terraform. I detailed out step by step directions here on how to fully set terraform up on your computer. Here is a quick run down of how that works:

  • First visit terraform.io and download the zip file
  • then unzip the file
  • create a folder on your c: drive
  • place the exe in that folder
  • finally put the path in your environment variables

How to Configure Terraform for AWS

After you have terraform installed, you will need to create an IAM user that can gain access to your aws account to create AWS EC2 instances using terraform. AWS best practice recommends that you not use the principal of least privilege.

Principal of least privilege means you are only given the permissions needed to do your job and nothing more. If you don’t already have IAM access keys follow these steps:

  1. Sign into the Amazon AWS IAM Console
  2. Click on users
  3. Select the user you signed in with
  4. Then select security credentials
  5. Finally Create access key and download the csv

Now that you have the access key created lets setup Terraform cloud. Terraform cloud allows you to store your terraform state file and access key securely.

Setting up Terraform Cloud Account for AWS

Head over to app.terraform.io and create a free account. Then go to settings and create a variable set. Take your downloaded iam access keys and create a sensitive environment variable.

Terraform Cloud Variable Set
Terraform Cloud Variable Set

When saved they should look like the above. Once you have terraform installed, The path placed in your environment variables on your computer and terraform cloud setup. Go to the command prompt and enter “terraform login” to connect your online terraform account to your host.

Create a Terraform File to Deploy an AWS Linux EC2 Instance

To utilize terraform you must create a .tf file inside of a created folder on your computer. Create a main.tf, variable.tf and outputs.tf file. They can be empty but this is the recommended minimum file structure to get started with terraform.

In the main.tf file we will setup all the needed information to deploy an instance to the default aws vpc.

This code here indicates how you will be connecting to the terraform cloud. You need to fill in your terraform cloud organization name.

terraform {
  cloud {
    organization = "<organization name here>"
    workspaces {
        name = "terraform-ec2-workspace"
    }
  }
  required_providers {
    aws = {
      source  = "hashicorp/aws"
       version = "= 3.74.2"
    }
  }
}


provider "aws" {
  region = "us-east-1"
}

Next paste the following code below the provider.


#EC2 instance details

resource "aws_instance" "linuxinstance" {

ami = "ami-090fa75af13c156b4"

instance_type = "t2.micro"


tags = {

Name = "linux"

}

}

This here will deploy an AWS Linux EC2 Instance Using Terraform using the aws_instance block. This ami can change depending on the region or type of instance so you may need to check that for your region.

Deploy an AWS Linux EC2 Instance Using Terraform

Now you have everything in place, its time to run our configuration. First run the command “terraform init”. This must be done from the file path of the .tf files.

Terraform init

Then run a “terraform plan” to make sure everything looks fine.

Terraform plan

Finally run a “terraform apply” and enter yes to approve the configuration.

Terraform Apply

If you go to your AWS EC2 console you will now see your fully deployed Linux instance. More terraform resources can be applied to this code for aws. Check out their registry here.

Conclusion

There you have it, This post helped you deploy an AWS Linux EC2 Instance Using Terraform. It’s a quick and effective technique to deploy resources in the cloud, as you may have noticed. Now this code is meant to be a good starter. Play around and add some additional resources like security groups, key pairs, etc..

Comments are closed.