Here’s a quick recipe for launching an EC2 instance using the AWS CLI (v2). You’ll need:
• AWS CLI installed & configured (aws configure)
• An SSH key pair (in EC2 → Key Pairs)
• A security group (in EC2 → Security Groups)
• A subnet (if you’re in a non-default VPC)
1. (Optional) Create a key pair & security group
# 1a. Create a new key pair
aws ec2 create-key-pair \
--key-name MyKeyPair \
--query 'KeyMaterial' --output text > MyKeyPair.pem
chmod 400 MyKeyPair.pem
# 1b. Create a security group
aws ec2 create-security-group \
--group-name MySG \
--description "SSH access"
# Allow SSH (port 22) from anywhere
aws ec2 authorize-security-group-ingress \
--group-name MySG \
--protocol tcp --port 22 --cidr 0.0.0.0/0
2. Launch your EC2 instance
aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \ # e.g. Amazon Linux 2 in us-east-1
--count 1 \ # how many instances
--instance-type t2.micro \ # choose your size
--key-name MyKeyPair \ # SSH key pair you just created
--security-group-ids sg-0123456789abcdef0 \ # your SG ID
--subnet-id subnet-6e7f829e \ # only if in a nondefault VPC
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=MyFirstEC2}]'
— you can also pass user-data, e.g.:
--user-data file://bootstrap.sh
🎯 This will spin up an EC2 instance in the region you’ve set in ~/.aws/config (override with --region eu-west-1, etc.).
3. Verify it’s running
aws ec2 describe-instances \
--filters "Name=tag:Name,Values=MyFirstEC2" \
"Name=instance-state-name,Values=pending,running" \
--query 'Reservations[].Instances[].[InstanceId,PublicIpAddress,State.Name]' \
--output table
That’s it! You should now see your new instance (and its public IP) in the output. 🎉
Need help with a specific AMI, user-data script, or networking setup? Let me know!