DEV Community

Ejibode Ibraheem
Ejibode Ibraheem

Posted on

Exploring AWS EC2 Instances: Uses and How to Create One Using Terraform

Table of contents

  • Introduction
  • What is AWS EC2 instance
  • Uses OF AWS EC2 instance
  • Benefits of using Terraform to create EC2 instance
  • Step-by-step guide in creating AWS EC2 instance with Terraform
  • Conclusion

Introduction

AWS offers more than 200 services, and Amazon Elastic Cloud Compute (EC2) is one of its most widely used services. It plays a crucial role in hosting applications, managing data processing tasks, and enabling modern cloud architecture. In this article, we will explore the use of EC2 and the step-by-step guide for creating EC2 instances using Terraform.

What is AWS EC2 Instance

An EC2 instance is a virtual server in AWS, which enables you to run applications in a secure and scalable environment. It provides resizable computing capacity, helping developers and businesses avoid the upfront costs of physical servers.

Uses of AWS EC2 Instance

  1. Web Hosting: Run web applications, blogs, or static websites

  2. Application Hosting: Deploy scalable backend applications or APIs.

  3. Big Data Processing: Analyze large datasets using tools like Apache Hadoop or Spark.

  4. Machine Learning: Train and deploy ML models with powerful GPU-enabled instances.

  5. Disaster Recovery: Maintain backup environments for high availability.

Benefits of using Terraform to create AWS EC2 Instance

Terraform allows you to automate the provisioning and management of EC2 instances, ensuring consistent infrastructure deployments across environments and preventing manual errors. Key advantages include:

  1. Reusability: Easily replicate infrastructure with minimal changes.
  2. Version Control: Track changes in infrastructure configurations.
  3. Scalability: Handle large and complex infrastructures efficiently.

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

Prerequisites

  1. AWS Account: Ensure you have an AWS account and access keys.
  2. IAM User: Set up an IAM user with sufficient EC2 permissions.
  3. Terraform Installed: Download and install Terraform from terraform.io.
  • create a main.tf file and paste the below code in the file
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-0c02fb55956c7d316" # Replace with your preferred AMI ID
  instance_type = "t2.micro"

  tags = {
    Name = "MyEC2Instance"
  }
}


Enter fullscreen mode Exit fullscreen mode
  • create a variable.tf file for reusability and paste the below code in it
variable "instance_type" {
  default = "t2.micro"
}

variable "region" {
  default = "us-east-1"
}

Enter fullscreen mode Exit fullscreen mode
  • Update the main.tf to reference these variables
provider "aws" {
  region = var.region
}

resource "aws_instance" "example" {
  ami           = "ami-0c02fb55956c7d316"
  instance_type = var.instance_type

  tags = {
    Name = "MyEC2Instance"
  }
}

Enter fullscreen mode Exit fullscreen mode
  • Run the command below terraform init

Terraform-init

  • If successfully initialized, run: terraform validate

Terraform-validate

-Then, run :
terraform plan

terraform-plan

  • terraform apply -auto-approve

terraform-apply

The next step is to go and confirm on the AWS console if the instance has been created.

AWS instance

  • To destroy the resource created , run this command terraform destroy -auto-approve

terraform-destroy

Conclusion

AWS EC2 instances are a cornerstone of modern cloud computing, and leveraging Terraform to manage them unlocks the full potential of IaC. With Terraform, you can build scalable, repeatable, and consistent infrastructure in minutes.

Top comments (0)