DEV Community

Jeshlin P V
Jeshlin P V

Posted on • Updated on

Blog: Creating, Modifying, and Destroying an EC2 Instance in AWS with Terraform

Introduction

Hello Friends! I’ve been exploring the capabilities of Terraform and AWS. Recently, I worked on creating, modifying, and destroying an EC2 instance with Terraform. This blog will detail the steps I followed.

Understanding the tools

Terraform

Terraform is an open-source tool that allows you to define and provision infrastructure as code. Today, I learned some of the basics of Terraform and used it to create an EC2 instance.

Amazon EC2 (Elastic Compute Cloud)

Amazon EC2 provides scalable virtual servers in the cloud, allowing you to run applications and services with flexible computing power.

Here’s a brief overview of my Terraform journey:

● Installation: I started by installing Terraform on my local machine. This was straightforward, requiring just a few commands to download and install the binary.

● Writing Configuration Files: Terraform uses configuration files written in HashiCorp Configuration Language (HCL). I created a simple configuration file to define an EC2 instance.

● Applying the Configuration: Using the “terraform apply” command, I provisioned the resources defined in my configuration file. Terraform handled the creation of the EC2 instance, showing me the power of infrastructure as code.

Here's how I did it:

1. Install Terraform

Ensure Terraform is installed on your local machine. If not, download and install it from the Terraform website. I used Chocolatey software to install Terraform.

2. Set Up Your Working Directory

To configure AWS, open your terminal and run the command "aws configure".
Create a new directory for your Terraform configuration files.
Navigate to this directory in your terminal.

Image description

3. Create a Terraform Configuration File

In your working directory, create a new file named 'main.tf'.

Image description
Define the AWS provider and the region where the EC2 instance will be created:

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

Enter fullscreen mode Exit fullscreen mode

Define the EC2 instance resource with the desired AMI ID and instance type:

resource "aws_instance" "ec2-instance" {
  ami           = "ami-0c55b159cbfafe1f0"  # Replace with your desired AMI ID
  instance_type = "t2.micro"
}

Enter fullscreen mode Exit fullscreen mode

4. Initialize Terraform

In the terminal, run the following command to initialize Terraform and download the necessary plugins for the AWS provider:

terraform init

Enter fullscreen mode Exit fullscreen mode

5. Apply the Configuration to Create the EC2 Instance

Apply the configuration to create the EC2 instance by running:

terraform apply

Enter fullscreen mode Exit fullscreen mode

Terraform will display a plan of what will be created. Type yes to confirm and create the instance.

6. Modify the EC2 Instance

To modify the EC2 instance, update the instance_type in the main.tf file:

resource "aws_instance" "ec2-instance" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.medium"  # Updated instance type
}

Enter fullscreen mode Exit fullscreen mode

Apply the changes by running:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Terraform will detect the change and prompt you to confirm the update. Type yes to apply the modification.

7. Destroy the EC2 Instance

When you no longer need the EC2 instance, destroy it by running:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Terraform will ask for confirmation. Type yes to confirm and destroy the instance.

Conclusion

Working with Terraform to create, modify, and destroy an EC2 instance, was an enlightening experience. This hands-on approach demonstrated the power of IaC in managing cloud resources efficiently and consistently. I'm excited to continue exploring Terraform and AWS and learning more about automating and managing infrastructure.

Top comments (0)