AWS · Compute & Storage

EC2

EC2 (Elastic Compute Cloud) gives you virtual servers. You choose the size and operating system; AWS runs the hardware.

Beginner 5 min Lesson 4 of 30
0/30 in AWS
On this page · 5 min · Beginner

Learning objectives#

  • Launch an EC2 instance
  • Choose an instance type and AMI
  • Bootstrap software with user data
  • Attach a role and secure access

Explanation#

An AMI (Amazon Machine Image) is the OS template. Instance types combine family and size: t3.micro (burstable), m7i.large (general purpose), c7g (compute, Graviton ARM).

Storage is usually an EBS volume — a network disk that persists when the instance stops.

Security groups act as a firewall. User data runs a script on first boot to install software.

Attach an IAM instance profile (role) so code on the instance can call AWS APIs without keys. Prefer Session Manager over opening SSH.

Code example: Launch a web server#

ShellLaunch a web server Illustrative
aws ec2 run-instances \
  --image-id resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 \
  --instance-type t3.micro \
  --security-group-ids sg-0web123 \
  --subnet-id subnet-0pub123 \
  --iam-instance-profile Name=web-ssm-role \
  --user-data file://user-data.sh \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=web-1}]'


# user-data.sh
#!/bin/bash
dnf install -y nginx
systemctl enable --now nginx


aws ssm start-session --target i-0abc123def456     # shell access without SSH
Expected output
{
  "Instances": [{ "InstanceId": "i-0abc123def456", "InstanceType": "t3.micro", "State": { "Name": "pending" } }]
}

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. The AMI ID is resolved from a public SSM parameter so you always get the latest Amazon Linux 2023.
  2. The security group controls inbound ports.
  3. The instance profile grants permissions (here, Session Manager access).
  4. User data installs and starts Nginx on first boot.
  5. Session Manager opens a shell with no port 22 open.

Real-world use case#

Running a self-managed app server, a build agent or software that needs full OS control.

Common mistakes#

SSH open to 0.0.0.0/0

Restrict to your IP, or close port 22 and use Session Manager.

Storing data only on instance store

Instance store disks are wiped when the instance stops. Use EBS or S3 for anything important.

Best practices#

  • Use instance roles, not keys
  • Use Session Manager instead of SSH
  • Stop or schedule dev instances
  • Use Auto Scaling groups for production instead of single instances

Remember this#

AMI = OS template. Instance type = size.

Security group = firewall. User data = first-boot script.

Role on the instance, no keys on disk.

Practice exercise#

Your task: Stop and then start the instance i-0abc123def456.

Show solution
Shell Illustrative
aws ec2 stop-instances --instance-ids i-0abc123def456
aws ec2 start-instances --instance-ids i-0abc123def456

Mini quiz#

EC2 quizMultiple choice Question 1 of 1

What runs a script automatically when an instance first boots?

Score so far: 0/0

Want more? Practise all AWS MCQs · AWS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.