Skip to main content

EC2 Complete Tutorials

 Table of Contents

  1. Prerequisites
  2. Launching an EC2 Instance
  3. Connecting to an EC2 Instance
  4. Configuring Security Groups
  5. Creating and Using Key Pairs
  6. Elastic IPs
  7. EC2 Instance Types
  8. Volume Management with EBS
  9. Monitoring with CloudWatch
  10. Automating with EC2 User Data
  11. Stopping and Terminating EC2 Instances
  12. Best Practices for EC2 Usage

1. Prerequisites

  • AWS Account: Create an AWS account at AWS Sign-Up.
  • AWS CLI (optional): Download and configure the AWS CLI for command-line management of EC2.
    • Install: AWS CLI
    • Configure:


      aws configure
      Enter AWS Access Key ID, Secret Access Key, region, and output format.

2. Launching an EC2 Instance

Follow these steps to launch an instance from the AWS Console:

Step 1: Login to AWS Console

Step 2: Go to the EC2 Dashboard

  • Navigate to EC2 under the "Services" section.

Step 3: Launch Instance

  • Click on Launch Instance.

Step 4: Choose AMI (Amazon Machine Image)

  • Amazon Linux 2 or Ubuntu are popular choices.
  • AMIs come pre-configured with an OS and software.

Step 5: Select Instance Type

  • Choose an instance type (e.g., t2.micro) which is free-tier eligible.
  • Larger instances offer better CPU, RAM, and network speeds.

Step 6: Configure Instance Details

  • Specify the number of instances and VPC (Virtual Private Cloud) or subnet.
  • Choose Auto-assign Public IP to enable SSH access.
  • Add any IAM role if needed (for access to S3, CloudWatch, etc.).

Step 7: Add Storage

  • Use EBS volumes (default is 8 GB).
  • You can add additional volumes if required.

Step 8: Configure Security Group

  • Security groups act as firewalls.
  • Open SSH (Port 22) for your IP address to allow remote access.

Step 9: Review and Launch

  • Review the configurations and click Launch.
  • AWS will ask for a key pair—choose or create a new one.

3. Connecting to an EC2 Instance

Using SSH from Linux/Mac

bash

chmod 400 your-key.pem ssh -i your-key.pem ec2-user@your-ec2-public-ip

Using SSH from Windows (via PuTTY)

  • Convert .pem to .ppk using PuTTYgen.
  • Use the .ppk file to connect via PuTTY.

4. Configuring Security Groups

  • Go to the EC2 Dashboard > Security Groups.
  • Add Inbound Rules for SSH (Port 22), HTTP (Port 80), or HTTPS (Port 443) as needed.
  • Ensure that only trusted IP addresses have access to critical ports.

5. Creating and Using Key Pairs

  • Go to EC2 Dashboard > Key Pairs.
  • Create a new key pair and download the .pem file.
  • Store it securely—it’s required to connect to the instance.

6. Elastic IPs

  • Elastic IPs are static public IPs assigned to instances.
  • Why use Elastic IP?: If an instance restarts, its public IP changes. Elastic IP ensures it remains the same.
  • Go to EC2 Dashboard > Elastic IPs > Allocate new IP.
  • Associate it with your instance.

7. EC2 Instance Types

  • EC2 instances come in different types:
    • t2.micro: General purpose, free tier.
    • m5.large: Balanced CPU and memory.
    • r6g.8xlarge: Memory-optimized for databases.
    • p4d: GPU instances for ML workloads.

8. Volume Management with EBS

  • EBS (Elastic Block Store) provides persistent storage.
  • Attach additional volumes from the Volumes section.
  • Use the following commands to mount the EBS volume:
    bash

    sudo mkdir /mnt/data sudo mount /dev/xvdf /mnt/data

9. Monitoring with CloudWatch

  • CloudWatch helps monitor CPU, network, and disk performance.
  • Go to CloudWatch Dashboard and create custom alarms for usage metrics (e.g., high CPU utilization).

10. Automating with EC2 User Data

  • You can run shell scripts at the time of instance launch using User Data.
  • Example:
    bash

    #!/bin/bash yum update -y yum install httpd -y systemctl start httpd
  • This script installs and starts the Apache web server during the instance launch.

11. Stopping and Terminating EC2 Instances

  • Stop: The instance shuts down, but you can restart it later.
  • Terminate: Deletes the instance and its attached storage (unless marked to persist).

How to Stop/Terminate:

  • Go to EC2 Dashboard > Select instance > Actions > Stop/Terminate.

12. Best Practices for EC2 Usage

  • Security:

    • Keep your key pair secure.
    • Regularly update the OS for security patches.
  • Scaling:

    • Use Auto Scaling Groups to dynamically increase or decrease instances based on traffic.
  • Cost Optimization:

    • Use Spot Instances for non-critical workloads (up to 90% savings).
    • Use Reserved Instances for predictable workloads.
  • Backup:

    • Take EBS Snapshots for backup.
    • Use AMI to create custom machine images for re-deployment.

Summary

In this guide, you learned:

  1. How to launch and connect to an EC2 instance.
  2. How to manage security groups, key pairs, and volumes.
  3. Elastic IPs, CloudWatch monitoring, and User Data automation.
  4. Best practices for cost, security, and scalability.

This tutorial offers a deep dive into every aspect of EC2. Let me know if you need further guidance or any specific help related to your instance setup!

Click to Learn AWS visit Shiva Concept Solution



Comments

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

Conditional Statement in Python

It is used to solve condition-based problems using if and else block-level statement. it provides a separate block for  if statement, else statement, and elif statement . elif statement is similar to elseif statement of C, C++ and Java languages. Type of Conditional Statement:- 1) Simple if:- We can write a single if statement also in python, it will execute when the condition is true. for example, One real-world problem is here?? we want to display the salary of employees when the salary will be above 10000 otherwise not displayed. Syntax:- if(condition):    statements The solution to the above problem sal = int(input("Enter salary")) if sal>10000:     print("Salary is "+str(sal)) Q)  WAP to increase the salary of employees from 500 if entered salary will be less than 10000 otherwise the same salaries will be displayed. Solution:- x = int(input("enter salary")) if x<10000:     x=x+500 print(x)   Q) WAP to display th...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...