DEV Community

MakendranG for Kubernetes Community Days Chennai

Posted on • Edited on

How to create an EC2 instance on AWS using Terraform ?

In this article, we will see how to create an EC2 Instance using Terraform.

Before proceeding, We need to be familiar with the basics of Terraform and AWS EC2 Instance.

EC2

Pre-requisites

  1. Basic understanding of Terraform.
  2. Terraform installed on our system.
  3. AWS Account (Create if you donโ€™t have one).
  4. access_key & secret_key of an AWS IAM User.

Terraform configuration files for creating an AWS EC2 Instance

Create a dedicated directory where we can create terraform configuration files.

Use the following command to create a directory and change our present working directory to it.

mkdir terraform_ec2instance
cd terraform_ec2instance/
Enter fullscreen mode Exit fullscreen mode

I have used Visual Studio Code as an editor to write in files, we can use an editor of our choice and copy paste the following configurations to create variables.tf, terraform.tfvars and main.tf

main.tf

Create main.tf which is responsible to create an EC2 on AWS. This main.tf will read values of variables from variables.tf and terraform.tfvars.

code main.tf
Enter fullscreen mode Exit fullscreen mode
provider "aws" {
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
    region = "us-east-1"
}

resource "aws_instance" "ec2_instance" {
    ami = "${var.ami_id}"
    count = "${var.number_of_instances}"
    subnet_id = "${var.subnet_id}"
    instance_type = "${var.instance_type}"
    key_name = "${var.ami_key_pair_name}"
} 
Enter fullscreen mode Exit fullscreen mode

variables.tf

Create variables.tf which contains the declaration and definition of the variables.

code variables.tf
Enter fullscreen mode Exit fullscreen mode
variable "access_key" {
        description = "Access key to AWS console"
}
variable "secret_key" {
        description = "Secret key to AWS console"
}


variable "instance_name" {
        description = "Name of the instance to be created"
        default = "kcdchennai-demo"
}

variable "instance_type" {
        default = "t2.micro"
}

variable "subnet_id" {
        description = "The VPC subnet the instance(s) will be created in"
        default = "subnet-07ebbe60"
}

variable "ami_id" {
        description = "The AMI to use"
        default = "ami-09d56f8956ab235b3"
}

variable "number_of_instances" {
        description = "number of instances to be created"
        default = 1
}


variable "ami_key_pair_name" {
        default = "tomcat"
}
Enter fullscreen mode Exit fullscreen mode

Once variables.tf file is created, We need to change values assigned to variable. We must change ami_key_pair_name, ami_id and subnet_id as these are specific to the environment.

terraform.tfvars

Create terraform.tfvars which contains the definition of access_key and secret_key variables defined in the above file.

The following keys need to be changed with the keys of our IAM user.

code terraform.tfvars
Enter fullscreen mode Exit fullscreen mode
access_key = "Access Key of IAM User"
secret_key = "Secret Key of IAM User"
Enter fullscreen mode Exit fullscreen mode

Creating an EC2 using the Terraform configuration files

terraform init command downloads and installs plugins for providers used within the configuration. In our case it is AWS.

terraform init
Enter fullscreen mode Exit fullscreen mode

terraform plan command is used to see the changes that will take place on the infrastructure.

 terraform plan
Enter fullscreen mode Exit fullscreen mode

terraform apply command will create the resources on the AWS mentioned in the main.tf file. It will be prompted to provide our input to create the resources.

terraform apply
Enter fullscreen mode Exit fullscreen mode

When we execute the above command, we can see that 1 new resource has been added and 0 has been destroyed in the output.

We can go to the AWS EC2 console to verify if the EC2 instance is created or not.

Deleting the created EC2 instance using Terraform

If we no longer require resources that we have created using the configuration mentioned in the main.tf file, we can use the terraform destroy command to delete all those resources.

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Thanks for reading my article till end. I hope you learned something special today. If you enjoyed this article then please share to your friends and if you have suggestions or thoughts to share with me then please write in the comment box.

Top comments (0)