How to create an EC2 instance on AWS using Terraform ?
Tags: ec2, terraform, aws, iac
Published: 2022-05-15 17:28:25

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u9k28rk3431r5vuzni1v.jpg)


## Pre-requisites

1. Basic understanding of Terraform.
2. Terraform installed on our system.
3. AWS Account ([Create](https://aws.amazon.com/free/) 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/
```

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
```

```
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}"
} 
```

## variables.tf

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


```
code variables.tf
```

```
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"
}
```

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
```

```
access_key = "Access Key of IAM User"
secret_key = "Secret Key of IAM User"
```


## 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
```


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

```
 terraform plan
```

`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
```

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
```

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.