Table of Contents
- Prerequisites
- Launching an EC2 Instance
- Connecting to an EC2 Instance
- Configuring Security Groups
- Creating and Using Key Pairs
- Elastic IPs
- EC2 Instance Types
- Volume Management with EBS
- Monitoring with CloudWatch
- Automating with EC2 User Data
- Stopping and Terminating EC2 Instances
- 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:
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
- Visit AWS Console and log in.
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
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:
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:
- 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:
- How to launch and connect to an EC2 instance.
- How to manage security groups, key pairs, and volumes.
- Elastic IPs, CloudWatch monitoring, and User Data automation.
- 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
POST Answer of Questions and ASK to Doubt