How to Launch AWS EC2 Instance Using Terraform

Terraform is an open source ‘infrastructure as code’ command line tool used to manage infrastructure in the cloud. With terraform you define declarative configuration file called HashiCorp Configuration Language (HCL) and provision your infrastructure. For instance, you need a Virtual machine, you just define resources like memory, storage, computing in the form of code and push in cloud. You will get the virtual machine or virtual instanace.Terraform is supported in all major cloud provider like Amazon cloud, Google cloud, Alibaba cloud and Microsoft Azure cloud.

This article will cover installation Terraform on Ubuntu 20.04 LTS system and launching AWS EC2 instance (Centos 8 stream) using terraform.

Installation of Terraform on Ubuntu 20.04 LTS

Download the latest version of Terraform  from URL https://www.terraform.io/downloads.html . At the time of writing article, the latest version is 0.14.3.

To Download terraform from command, run following wget command

$ wget https://releases.hashicorp.com/terraform/0.14.3/terraform_0.14.3_linux_amd64.zip

Now, unzip the downloaded file.

$ sudo apt install zip -y
$ sudo unzip  terraform_0.14.3_linux_amd64.zip

This will output you a terraform file just move it to /usr/local/bin/ to execute the command.

$ sudo mv terraform /usr/local/bin/

Check the version

$ terraform version

This should provide you output similar to below

ubuntu@linuxtechi:~$ terraform version
Terraform v0.14.3
ubuntu@linuxtechi:~$

Prefect, above output confirm that Terraform has been installed.

Launching AWS EC2 Instance Using Terraform

Let’s make a directory and configure Terraform inside it. Run following commands

$ mkdir terraform
$ cd terraform

Now, make a configuration file. I am giving here the name as config.tf . You can give name as per your choice but remember the extension must be ‘tf’.

$ vi config.tf

Add the following terms provider AWS, your access key, secret key and region where you are going to launch ec2 instance. Here, I am going to use my favorite Singapore region.

On the second block of the code define resource as ‘aws_instance’, ami  (I have picked ami from Centos AMI <https://wiki.centos.org/Cloud/AWS>). Give a instance type and also a tag of your choice.

provider "aws" {
access_key = "YOUR-ACCESS-kEY"
secret_key = "YOUR-SECRET-KEY"
region = "ap-southeast-1"
}

resource "aws_instance" "instance1" {
ami = "ami-05930ce55ebfd2930"
instance_type = "t2.micro"
tags = {
Name = "Centos-8-Stream"
}
}

Save & close the file.

Now, initialize your configuration by executing beneath terraform command

$ terraform init

Once Terraform has initialized, see what is going to happen by executing command,

$ terraform plan

If everything goes fine, then you should see following output.

terraform-plan

Now, execute your terraform code,

$ terraform apply

Type ‘yes’ and press enter for the confirmation.

enter-yes-terraform-apply

On the success of the executing you should be able to see output as below:

success-terrafrom-apply

Log-in to your AWS account and go to ec2 service you should find a ec2 instance with the tag you defined above.

ec2-in-aws-console

It’s simple and easy to provision infrastructure in cloud using the terraform. Hope you like the article. If you found any difficulty, please do comment us.

4 thoughts on “How to Launch AWS EC2 Instance Using Terraform”

  1. i receive an error :
    Error: error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: InvalidClientTokenId: The security token included in the request is invalid.
    status code: 403, request id: 678ef266-6221-4617-8a80-38d67b775f93

    on config.tf line 1, in provider “aws”:
    1: provider “aws” {

    1. Pradeep Kumar

      Hi Mamta,

      It seems like issue is with AWS credentials, please cross check access and secret key.

  2. It is the access key. Also, check that your access key is wrapped in strings.

    Question: How do I save my access and secret code in a secret file so that I’m not hacked?

Leave a Comment

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