d
WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

n

StatusNeo

Spinning up an AWS EC2 instance using Terraform

In this tutorial, let us talk about launching an EC2 instance using Terraform. If you are completely new to Terraform, we would recommend you to check out our previous tutorial on have you ever set up a terraform?

1. Open Amazon Management Console and type IAM in Find Services search box. Click on Search.

2. This opens up Identity and Access Management (IAM) page.

3. Using the sidebar navigation, click on Access Management -> Users

4. Once the user management page opens, click on Add user.

Adding a User

1. User Details:
Give a username for the user and check the programmatic access option to generate Access Key and Secret Access Key.

2. Permissions
Add user to a group having administrator rights.

3. Tags
(skip this step for now)

4. Review

Verify all the details on the Review screen and click on Create User. If everything goes right, a success message shows on the screen with the generated access and secret access keys.

Important Note: Make sure to either maintain a copy of the keys or download them as .csv file because they are not accessible again.

Authenticating Amazon User in the operating system via command line

On the command line, pass the Access Keys and Secret Access keys to the operating system:

$ export AWS_ACCESS_KEY_ID=[Access Key]
$ export AWS_SECRET_ACCESS_KEY=[Secret Access Key]

Creating the Configuration file for Terraform

Infrastructure as Code is implemented in HCL (HashiCorp Configuration Language) with .tf (Terraform) extension.

$ gedit main.tf

Here is an example of what a main.tf file should look like.

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

resource "aws_vpc" "main" {
   cidr_block = "10.0.0.0/16"
}

resource "aws_instance" "example" {
    ami = "ami-0c55b159cbfafe1f0"
    instance_type = "t2.micro"  tags = {
     Name = "terraform-example"
    }
}

Executing the Configuration file via Terraform

1. Initialize current directory with terraform configurations

$ terraform init

2. Create Execution plan and dependency graph via Terraform

$ terraform plan

If we run Terraform again after changing the .tf file, Terraform checks the previous version of the file and builds only the changed part.

3. Apply the Terraform actions and provision Infrastructure on Amazon Web Services


The EC2 Instance is launched successfully and can be checked on the AWS Console.

We have successfully launched an EC2 instance on Amazon Web Services using Terraform.

Principal DevOps Evangelist and Consultant at StatusNeo

Add Comment