0
0
AwsHow-ToBeginner · 4 min read

How to Launch an EC2 Instance on AWS: Step-by-Step Guide

To launch an EC2 instance, use the aws ec2 run-instances command with parameters like --image-id for the OS, --instance-type for the hardware size, and --key-name for SSH access. This command creates a virtual server in the cloud ready to use.
📐

Syntax

The basic command to launch an EC2 instance is:

  • aws ec2 run-instances: Starts the instance creation process.
  • --image-id: Specifies the Amazon Machine Image (AMI) ID to use as the OS.
  • --instance-type: Defines the hardware size (CPU, memory) like t2.micro.
  • --key-name: The name of your SSH key pair for secure login.
  • --security-group-ids: IDs of firewall rules to allow traffic.
  • --subnet-id: The network subnet where the instance will run.
  • --count: Number of instances to launch.
bash
aws ec2 run-instances --image-id <ami-id> --instance-type <type> --key-name <key-name> --security-group-ids <sg-id> --subnet-id <subnet-id> --count 1
💻

Example

This example launches one t2.micro instance using a common Amazon Linux 2 AMI, with a key pair named MyKeyPair and a security group allowing SSH access.

bash
aws ec2 run-instances \
  --image-id ami-0abcdef1234567890 \
  --instance-type t2.micro \
  --key-name MyKeyPair \
  --security-group-ids sg-0123456789abcdef0 \
  --subnet-id subnet-6e7f829e \
  --count 1
Output
{ "Instances": [ { "InstanceId": "i-1234567890abcdef0", "ImageId": "ami-0abcdef1234567890", "InstanceType": "t2.micro", "State": { "Code": 0, "Name": "pending" }, "KeyName": "MyKeyPair", "SubnetId": "subnet-6e7f829e" } ] }
⚠️

Common Pitfalls

  • Using an incorrect or unavailable --image-id causes failure; always verify the AMI ID for your region.
  • Missing --key-name means you cannot SSH into the instance.
  • Not specifying a --security-group-ids that allows SSH (port 22) blocks remote access.
  • Launching in a subnet without internet access may prevent updates or downloads.
bash
aws ec2 run-instances --image-id ami-0abcdef1234567890 --instance-type t2.micro

# This will fail because key-name and security groups are missing

# Correct way:
aws ec2 run-instances --image-id ami-0abcdef1234567890 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-0123456789abcdef0
📊

Quick Reference

Remember these quick tips when launching EC2 instances:

  • Always choose the right AMI for your region and OS.
  • Use t2.micro for free tier eligible small instances.
  • Set up a key pair before launching to access your instance.
  • Configure security groups to allow necessary traffic like SSH.
  • Check your subnet has internet access if needed.

Key Takeaways

Use the aws ec2 run-instances command with required parameters to launch an EC2 instance.
Always specify a valid AMI ID, instance type, key pair, and security group for access.
Verify your security group allows SSH (port 22) to connect to the instance.
Choose the correct subnet to ensure network connectivity.
Check the command output for the new instance ID to manage it later.