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
Web Hosting: Run web applications, blogs, or static websites
Application Hosting: Deploy scalable backend applications or APIs.
Big Data Processing: Analyze large datasets using tools like Apache Hadoop or Spark.
Machine Learning: Train and deploy ML models with powerful GPU-enabled instances.
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:
- Reusability: Easily replicate infrastructure with minimal changes.
- Version Control: Track changes in infrastructure configurations.
- Scalability: Handle large and complex infrastructures efficiently.
Step-by-Step Guide: Creating an AWS EC2 Instance Using Terraform
Prerequisites
- AWS Account: Ensure you have an AWS account and access keys.
- IAM User: Set up an IAM user with sufficient EC2 permissions.
- 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"
}
}
- 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"
}
- 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"
}
}
- Run the command below
terraform init
- If successfully initialized, run:
terraform validate
-Then, run :
terraform plan
terraform apply -auto-approve
The next step is to go and confirm on the AWS console if the instance has been created.
- To destroy the resource created , run this command
terraform destroy -auto-approve
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)