EC2 (Elastic Compute Cloud) rents you servers. One server is an instance. You choose two things when you start one:
| Choice | Means | Example |
|---|---|---|
| AMI (Amazon Machine Image) | the disk it starts from: the operating system | ami-0c7217cdde317cfec, Amazon Linux 2023 |
| instance type | its size: CPUs and memory | t3.micro, small and cheap |
You pay for every second it runs. Two ways to switch it off:
| The server | Its disk | You pay for | |
|---|---|---|---|
| stop | kept, you can start it again | kept | the disk only |
| terminate | gone for good | deleted | nothing |
New command
aws ec2 run-instances
Starts a new instance. The tag gives it a name that lists show.
Usageaws ec2 run-instances --image-id AMI --instance-type type --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=name}]'
Example
aws ec2 run-instances --image-id ami-0c7217cdde317cfec --instance-type t3.micro --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=test-box}]'
It answers with a long JSON. The instance starts as pending, and is running a few seconds later. To see it, use the list from Chapter 1, with one more detail at the end, the public IP address:
sh
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[0].Value,InstanceId,State.Name,PublicIpAddress]' --output table