DEV Community

Harshana vivekanandhan
Harshana vivekanandhan

Posted on

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

Introduction

Hi there! I've been investigating Terraforms and AWS's capabilities as part of my internship. I recently worked on using Terraform to create, modify, and terminate an EC2 instance. This blog will go over the procedures I took, the difficulties I ran across, and the fixes I discovered.

Prerequisites

Before we dive in, ensure you have the following:

✔️AWS Account: If you don't have one, you can create it here.
✔️Terraform Installed: Download and install Terraform from the official site.
✔️AWS CLI Configured: Install and configure the AWS CLI. Follow the instructions here.

Setting Up Your Terraform Project

✨Create a Directory: Start by creating a directory for your Terraform project.
✨Initialize Terraform: Create a new file named main.tf where we'll define our Terraform configuration.
✨Initialize Terraform: Initialize the Terraform working directory.

Creating an EC2 Instance

1.Plan the Execution: Run terraform plan to see what Terraform will do.
2.Apply the Configuration: Apply the configuration to create the EC2 instance.
3.Terraform will prompt for confirmation. Type yes to proceed. After a few moments, your EC2 instance will be created.

Modifying the EC2 Instance

Suppose you want to change the instance type. Edit the main.tf file.
1.Plan the Changes: Run terraform plan to see the changes.
2.Apply the Changes: Apply the configuration to update the EC2 instance.

Destroying the EC2 Instance

When you no longer need the EC2 instance, you can destroy it using Terraform.
1.Destroy the Resource: Run the terraform destroy command. Terraform will prompt for confirmation. Type yes to proceed. Terraform will then destroy the EC2 instance and clean up the resources.

Conclusion

Using Terraform to manage AWS resources simplifies infrastructure management by allowing you to define your infrastructure as code. In this blog post, we covered the basics of creating, modifying, and destroying an EC2 instance with Terraform. This process can be extended to manage more complex infrastructures by leveraging Terraforms extensive provider and resource support. By adopting Terraform, you can ensure your infrastructure is versioned, reproducible, and maintainable, paving the way for more efficient and reliable cloud resource management.

Top comments (0)