AWS EC2: Launch a New Instance
Launch an EC2 instance with aws ec2 run-instances. Configure AMI, instance type, key pair, security groups, subnet, and tags.
EC2 Operations
Detailed Explanation
Launching an EC2 Instance
The aws ec2 run-instances command creates and starts one or more EC2 instances. It requires at minimum an AMI ID and instance type.
Minimal Launch Command
aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--instance-type t3.micro \
--count 1
Production-Ready Launch
aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--instance-type t3.medium \
--count 1 \
--key-name my-ssh-key \
--security-group-ids sg-0123456789abcdef0 \
--subnet-id subnet-0abc123def456789 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=WebServer},{Key=Environment,Value=production}]' \
--region us-east-1
Key Parameters
| Flag | Purpose |
|---|---|
--image-id |
AMI ID — the operating system image |
--instance-type |
CPU and memory configuration (t3.micro, m5.large, etc.) |
--count |
Number of instances to launch |
--key-name |
SSH key pair for remote access |
--security-group-ids |
Firewall rules (can specify multiple) |
--subnet-id |
VPC subnet to place the instance in |
--tag-specifications |
Tags for naming and organizing |
Finding the Right AMI
aws ec2 describe-images \
--owners amazon \
--filters "Name=name,Values=amzn2-ami-hvm-*-x86_64-gp2" \
--query "sort_by(Images, &CreationDate)[-1].ImageId" \
--output text
With User Data (startup script)
aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--instance-type t3.micro \
--user-data file://startup-script.sh \
--count 1
The user data script runs as root when the instance first boots.
Use Case
Provisioning development or staging servers, launching batch processing workers, creating instances for performance testing, or scripting infrastructure deployments outside of CloudFormation/Terraform.