0% found this document useful (0 votes)
11 views10 pages

Launch and Manage EC2 Instances Guide

Uploaded by

Souvik Acharyya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Launch and Manage EC2 Instances Guide

Uploaded by

Souvik Acharyya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Launch a Basic EC2 Instance (Amazon Linux 2023)

Steps:

1. Go to EC2 Dashboard → Instances → Launch instance.

2. Enter name: BasicLinuxInstance.

3. Choose AMI: Amazon Linux 2023.

4. Instance type: [Link] (Free tier).

5. Create new key pair → Download and save.

6. Configure network settings: Allow SSH (port 22).

7. Launch instance.

2. Install putty and connect Amazon Linux 2023 instance

✅ PART 1: Install PuTTY on Windows

Step 1: Download PuTTY and PuTTYgen

1. Visit the official website: [Link]

2. Download:

o PuTTY (SSH client)

o PuTTYgen (key converter)

o Choose Windows Installer (64-bit) for most systems.

Step 2: Install PuTTY

1. Run the installer.

2. Click Next and accept defaults.

3. Click Install, then Finish.

✅ PART 2: Convert PEM to PPK (for PuTTY use)

Amazon provides the key in .pem format. PuTTY requires .ppk.

Step 3: Open PuTTYgen

1. Launch PuTTYgen from Start Menu.

Step 4: Load the PEM file

1. Click Load.

2. At the bottom, change file type to All Files (*.*).

3. Select your downloaded .pem key file (e.g., [Link]).

4. Click Open → You’ll see “Successfully imported foreign key”.

Step 5: Save the PPK file

1. Click Save private key.

2. Choose a name like [Link].

3. (Click "Yes" when prompted to save without a passphrase.)

4. Save it to a safe location.


✅ PART 3: Connect to Amazon Linux EC2 Using PuTTY

Step 6: Get EC2 Instance Public IP

1. Go to AWS Console → EC2 Dashboard.

2. Select your running EC2 instance.

3. Copy Public IPv4 address (e.g., [Link]).

Step 7: Launch PuTTY

1. Open PuTTY from Start Menu.

Step 8: Configure PuTTY Session

1. In Host Name, type:

ec2-user@<[Link]> (Your EC2 public IP)

2. Under Connection → SSH → Auth:

o Click Browse next to "Private key file for authentication".

o Select the .ppk file you saved earlier (e.g., [Link]).

3. (Optional) Under Session, you can:

o Enter a name in Saved Sessions (e.g., MyEC2Instance)

o Click Save for future use.

Step 9: Connect

1. Click Open.

2. On first connection, PuTTY will ask to trust the host — click Yes.

3. You'll be connected to your EC2 instance with the default Amazon Linux user: ec2-user.

🔒 Important Notes

 Usernames:

o Amazon Linux → ec2-user

o Ubuntu → or ubuntu

o Red Hat → ec2-user

o Debian → admin or ec2-user

 Ensure port 22 (SSH) is open in EC2 Security Group.

3. Install Apache Web Server

Steps:

1. SSH into EC2.

2. Run:

sudo yum update -y

sudo yum install httpd -y


sudo systemctl start httpd

sudo systemctl enable httpd

3. Open port 80 in EC2 Security Group.

4. Access via browser: [Link] of EC2 instance>

4. Host a Simple HTML Website

Steps:

1. SSH into EC2.

2. Create HTML file:

echo "<h1>Hello from EC2</h1>" | sudo tee /var/www/html/[Link]

3. Visit [Link]

5. Create a New EC2 Security Group

Steps:

1. Go to EC2 Dashboard → Security Groups → Create.

2. Allow:

o SSH (22)

o HTTP (80)

o Custom TCP (any additional if needed)

3. Attach to EC2 instance.

6. Create and Attach IAM Role to EC2

Steps:

1. Go to IAM → Roles → Create Role.

2. Select EC2 → Next.

3. Attach AmazonS3ReadOnlyAccess.

4. Name: EC2S3ReadRole.

5. Attach to running EC2: Actions → Security → Modify IAM role.

7. Access S3 from EC2 Instance

Steps:

1. SSH into EC2.

2. Install AWS CLI:

sudo yum install awscli -y

3. Run:

aws s3 ls
8. Create a Custom AMI from EC2

Steps:

1. Go to EC2 Dashboard → Instances.

2. Select your instance → Actions → Image → Create Image.

3. Name it and create.

4. Use the AMI to launch new EC2 instances.

9. Use EC2 Instance Connect (no SSH key required)

Steps:

1. Launch Amazon Linux instance with default settings.

2. Go to Instances → Connect → EC2 Instance Connect.

3. Click "Connect" and access the shell via browser.

10. Run a Python Script on EC2

Steps:

1. SSH into EC2.

2. Install Python:

sudo yum install python3 -y

3. Create script:

echo "print('Hello EC2 from Python')" > [Link]

python3 [Link]

11. Create EC2 Instance with User Data Script

Steps:

1. While launching EC2, expand "Advanced Details".

2. Paste user data script:

#!/bin/bash

yum update -y

yum install httpd -y

echo "<h1>Launched using User Data</h1>" > /var/www/html/[Link]

systemctl start httpd

systemctl enable httpd

3. Security group : allow http service and source [Link]/0


4. Visit [Link] ip of ec2 instance
12. Restrict SSH Access by IP

Steps:

1. Edit Security Group attached to EC2.

2. Modify SSH rule:

o Source: My IP (only allow your IP).

3. Save rules.

13. Create EC2 Tags and Use for Organization

Steps:

1. Launch instance or go to existing one.

2. Tags tab → Manage Tags → Add tags:

o Key: Environment, Value: Test

o Key: Owner, Value: Student01

3. Use tags to filter or group resources in EC2 dashboard.

14. Enable EC2 Termination Protection

Steps:

1. Go to EC2 → Select instance.

2. Actions → Instance Settings → Change termination protection.

3. Enable → Save.

4. Try terminating — AWS will block the termination.

15. Change EC2 Instance Type

Steps:

1. Stop the instance.

2. Actions → Instance Settings → Change instance type.

3. Choose new type (e.g., [Link]).

4. Start the instance again.

16. Host a PHP Website on EC2

Steps:

1. SSH into EC2.

2. Run:

sudo yum install httpd php -y


sudo systemctl start httpd

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/[Link]

3. Open browser → [Link]

17. Assign and Use Elastic IP

Steps:

1. Go to EC2 Dashboard → Elastic IPs → Allocate Elastic IP.

2. Associate with instance.

18. Copy Files from Local Machine to EC2

Steps (Linux/macOS):

1. From terminal:

C:\Users\abcd\Downloads>scp -i [Link] [Link] ec2-user@[Link]:/home/ec2-user/

 Connect EC2 instance and type command> ls (See the [Link] file)

19. Accessing and Using EC2 Instance Metadata from Amazon Linux EC2

🎯 Objective:

✅ Prerequisites:

1. AWS Account
2. Amazon Linux EC2 instance created
3. PuTTY connected to EC2 instance via .ppk or .pem
4. Instance has internet access (public IP or via NAT)

🛠️Step-by-Step Configuration:

🔹 Step 1: Launch EC2 Instance

1. Go to AWS Console → EC2 → Launch Instance.


2. Choose:
o Amazon Linux 2023 AMI
o Instance type: [Link] (Free Tier)
o Key Pair: Use existing or create new
o Network: Default VPC
o Auto-assign Public IP: Enabled
3. Click "Launch Instance".

🔹 Step 2: Connect EC2 Using PuTTY

If you have .pem file, convert it to .ppk using PuTTYgen.

1. Open PuTTY
2. Hostname: ec2-user@<your-public-ip>
3. In "Auth" section, load your .ppk file
4. Click "Open" → Login as: ec2-user
🔹 Step 3: Understand Instance Metadata

Metadata provides information like:

 Instance ID
 AMI ID
 Security groups
 IAM role
 Region
 Hostname
 Tags (if allowed)
 User data

🔹 Step 4: Use Metadata (IMDSv2 Method Recommended)


🧩 Step 4.1: Get Metadata Token (IMDSv2)

TOKEN=`curl -X PUT "[Link] \


-H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
Step 4.2: Access Basic Metadata Info
curl -H "X-aws-ec2-metadata-token: $TOKEN" \
[Link]

🔹 Step 5: Get Specific Metadata


🔍 Metadata
🔧 Command
Information

AMI ID curl -H "X-aws-ec2-metadata-token: $TOKEN" [Link]

Instance ID curl -H "X-aws-ec2-metadata-token: $TOKEN" [Link]

Public IPv4 curl -H "X-aws-ec2-metadata-token: $TOKEN" [Link]

curl -H "X-aws-ec2-metadata-token: $TOKEN" [Link]


Security Groups
groups

`curl -H "X-aws-ec2-metadata-token: $TOKEN" [Link]


Region
identity/document

🔹 Step 6: Optional – Add IAM Role and Get Credentials

If your instance has an IAM role:

Step 6.1: Get IAM Role Name


curl -H "X-aws-ec2-metadata-token: $TOKEN" \
[Link]
Step 6.2: Get Temporary Credentials
curl -H "X-aws-ec2-metadata-token: $TOKEN" \
[Link]

20. Configure a Local Web Server with Nginx

Steps:

1. SSH into EC2.

2. Install Nginx:
sudo amazon-linux-extras enable nginx1

sudo yum install nginx -y

sudo systemctl start nginx

sudo systemctl enable nginx

21. Install and Configure Mariadb Server

Steps:

1. SSH into EC2.

2. Run:

bash

CopyEdit

sudo yum install mariadb-server -y

sudo systemctl start mariadb

sudo mysql_secure_installation

22. Enable EC2 System Logs for Boot Diagnostics

Steps:

1. Launch EC2 with "Enable CloudWatch Logs".

2. Go to Actions > Instance Settings > Get System Log.

3. View logs of startup or boot issues.

23. Schedule System Shutdown and Restart

Steps:

1. SSH into EC2.

2. Run:

sudo shutdown -h +10 # Shuts down after 10 minutes

sudo shutdown -r +5 # Reboots after 5 minutes

24. Attach IAM Role to EC2 and Launch EC2 via CLI

Steps:

1. Create IAM Role (e.g., S3AccessRole).

2. Run:

aws ec2 run-instances \

--image-id ami-084a7d336e816906b \

--instance-type [Link] \

--iam-instance-profile Name="S3AccessRole" \
--key-name rndkey \

--security-group-ids sg-000895217061b02c7 \

--subnet-id subnet-0bf4c2ec3994ac5e6

25. Connect to EC2 via SSH using Key Pair

Steps:

1. Open terminal or PuTTY.

2. Run:

C:\Users\abcd\Downloads>ssh -i [Link] ec2-user@ [Link]

26. To close the AWS CloudShell session,

✅ Option 1: Exit the shell

From within the CloudShell terminal:

exit

 This command logs you out of the CloudShell session and terminates the shell.

✅ Option 2: Close the browser tab or CloudShell window

If you’re using the AWS Management Console:

1. Click the X icon at the top right of the CloudShell window.


2. Or, simply close the browser tab where CloudShell is open.

27. Access a Python-based webpage on your EC2 via its public IP in a browser.

Steps to Host Website via EC2 Public IP Using Python (Flask)

1. SSH into your EC2 instance


ssh -i [Link] ec2-user@your-ec2-public-ip

2. Install Python 3 and Flask


sudo yum update -y
sudo yum install python3 -y
pip3 install flask

3. Create a simple Flask web app


nano [Link]

Paste the following code:

from flask import Flask


app = Flask(__name__)

@[Link]('/')
def home():
return "Hello EC2 from Python Web App!"
if __name__ == "__main__":
[Link](host='[Link]', port=5000)

Save and exit (Ctrl + O, Enter, then Ctrl + X).

4. Run the Flask app


python3 [Link]

The app starts on port 5000.

5. Allow incoming traffic on port 5000

 Go to EC2 > Security Groups in AWS Console.


 Find the security group used by your instance.
 Edit Inbound Rules → Add rule:
o Type: Custom TCP
o Port range: 5000
o Source: [Link]/0 (or restrict to your IP)

6. Access the site from your browser


[Link]

Example:

[Link]

You should see: Hello EC2 from Python Web App!

You might also like