Creating , Modifying , and Destroying an EC2 Instance in AWS with Terraform
Published: 2024-06-27 17:12:35

Hi Friends! As part of my internship,We will learn how we can Creating, Modifying, and Destroying an EC2 Instance with Terraform

### Introduction

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It allows you to define, create, and manage infrastructure resources in a declarative way, using a high-level configuration language. With Terraform, you can easily provision, update, and delete infrastructure resources, such as virtual machines, databases, load balancers, and more, across multiple cloud providers or on-premises data centers.

## Prerequisites

Before we begin, make sure you have the following:
1. An AWS account.
2. AWS CLI configured with your AWS credentials.
3. Terraform installed on your machine.

## Step 1: Setting Up Your Terraform Configuration

First, create a new directory for your Terraform project and navigate into it.Next, create a new file named `main.tf` and open it in your favorite text editor. This file will contain the configuration for 
your EC2 instance.

## Step 2: Configuring AWS Provider

In `main.tf`, start by defining the AWS provider. This tells Terraform to use AWS as the provider for your infrastructure.

## Step 3: Creating an EC2 Instance

To create an EC2 instance on AWS in the simplest way using Terraform,create a file called main.tf and define a `provider` block for AWS in your `.tf` file. Add a `resource` block for the `aws_instance` 
specifying the AMI and instance type. Run `terraform init`, `terraform plan`, and `terraform apply` to provision the instance.

## Step 4: Initializing Terraform

To initialize Terraform, create a directory for your configuration files. Inside it, create a main configuration file (e.g., `main.tf`). Run `terraform init` in your terminal within this directory. This command downloads necessary plugins and prepares your working directory for other Terraform commands.

## Step 5: Applying the Configuration

To apply the Terraform configuration, navigate to your project directory in the terminal. Run `terraform init` to initialize the project, then use `terraform plan` to review the changes.Review the plan and type ‘yes’ to apply the changes. Finally, execute `terraform apply` to apply the
configuration and provision the resources. Confirm whenprompted.Terraform will then create the EC2 instance.

## Step 6: Modifying the EC2 Instance

If you want to modify the instance (e.g., change the instance type), simply update the `main.tf` file. For example, to change the instance type to `t2.small`, update the `instance_type` . Save the file and 
run the following command ‘terraform apply’to apply the changes . Terraform will update the instance to the new configuration.

### Step 7: Destroying the EC2 Instance

When you no longer need the EC2 instance, you can destroy it using Terraform. Run the following command ‘terraform destroy’. Terraform will show you a plan of the resources it will destroy. Review the plan and type `yes` to proceed. Terraform will then terminate the EC2 instance.

## Conclusion

In this blog post, we covered the basics of creating, modifying, and destroying an EC2 instance using Terraform. By using Terraform, you can manage your cloud infrastructure in a consistent and repeatable way, making it easier to scale and maintain your environment. Happy Terraforming