Creating an EC2 Instance on AWS Using Terraform: A Step-by-Step Guide

Dharshana Priya
2 min readJun 26, 2024

--

In this guide, I'll walk you through creating a server on AWS using Terraform, configuring the instance via VS Code, and performing various Terraform commands. Finally, we'll modify the instance configuration and destroy the infrastructure.

Prerequisites

- An AWS account
- AWS CLI configured with your credentials
- Terraform installed on your machine
- VS Code installed on your machine

Step 1: Setting Up Your Terraform Project

1. Create a Project Directory

First, I’ll create a new directory for my Terraform project and navigate into it.

```sh
mkdir my-terraform-project
cd my-terraform-project
```

2. Create a Main Terraform Configuration File

Next, I'll create a file named `main.tf` inside my project directory. This file will contain the Terraform configuration for my AWS resources.

```sh
touch main.tf
```

3. Open VS Code

Then, I'll open the project directory in VS Code.

```sh
code .
```

Step 2: Writing the Terraform Configuration

I'll open `main.tf` in VS Code and add the following configuration:

```hcl
provider "aws" {
region = "us-west-2"
}

resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" Update with your desired AMI ID
instance_type = "t2.micro"

tags = {
Name = "ExampleInstance"
}
}
```

Step 3: Initializing Terraform

1. Initialize the Terraform Project

Now, I'll initialize the Terraform project to download the necessary provider plugins.

```sh
terraform init
```

Step 4: Validating and Formatting the Terraform Configuration

1. Validate the Terraform Configuration

I'll validate the configuration to ensure it is syntactically correct.

```sh
terraform validate
```

2. Format the Terraform Configuration

Next, I'll format the configuration files to follow Terraform's standard style.

```sh
terraform fmt
```

Step 5: Applying the Terraform Configuration

1. Apply the Terraform Configuration

Now, I'll apply the configuration to create the specified AWS resources.

```sh
terraform apply
```

Terraform will display an execution plan and ask for confirmation before creating the resources. I'll type `yes` to proceed.

Step 6: Modifying the Instance Configuration

1. Update the AMI ID

I'll open `main.tf` and change the `ami` value to a new AMI ID.

```hcl
resource "aws_instance" "example" {
ami = "ami-0c123456789abcdef" Update with the new AMI ID
instance_type = "t2.micro"

tags = {
Name = "ExampleInstance"
}
}
```

2. Apply the Updated Configuration

I'll apply the updated configuration to modify the existing AWS resources.

```sh
terraform apply
```

Again, Terraform will display an execution plan and ask for confirmation. I'll type `yes` to proceed.

Step 7: Destroying the Infrastructure

1. Destroy the Terraform-managed Infrastructure

Finally, I'll use the `terraform destroy` command to remove all resources created by Terraform.

```sh
terraform destroy
```

Terraform will show a plan of the resources to be destroyed and ask for confirmation. I'll type `yes` to proceed.

Conclusion

I have successfully created, modified, and destroyed an EC2 instance on AWS using Terraform. This guide has covered the essential Terraform commands (`init`, `validate`, `fmt`, `apply`, and `destroy`) and the process of updating configurations. With these skills, I can manage my AWS infrastructure more effectively using Terraform.

--

--