Sub topic 8
Launching an instance with the AWS CLI
This chapter assumes you have the AWS CLI installed and configured with credentials, a default Region, and an existing VPC with a subnet. It also assumes you have a key pair named my-key. The IDs below are examples. Replace them with your own.
What a launch needs
The run-instances command combines choices from earlier chapters into one request.
| Input | Option | Source |
|---|---|---|
| AMI | `--image-id` | Choosing an AMI |
| Instance type | `--instance-type` | Instance types and families |
| Subnet | `--subnet-id` | VPCs and subnets |
| Security group | `--security-group-ids` | Security groups |
| Key pair | `--key-name` | Key pairs |
The subnet decides the Availability Zone. The AMI, key pair, and subnet must all exist in the Region you are calling.
Step 1: Look up an AMI
AMI IDs differ by Region and change as new versions ship. Amazon publishes public SSM parameters that point to the latest Amazon Linux 2023 image, so you don't have to hard-code an ID.
Step 2: Run the instance
AMI_ID=$(aws ssm get-parameter \
--name /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 \
--query Parameter.Value --output text)
aws ec2 run-instances \
--image-id "$AMI_ID" \
--instance-type t3.micro \
--key-name my-key \
--security-group-ids sg-0123456789abcdef0 \
--subnet-id subnet-0123456789abcdef0 \
--metadata-options HttpTokens=required \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=tutorial-web}]'Three options in this command are worth noting:
--metadata-options HttpTokens=requiredmakes the instance accept only IMDSv2 requests. A later chapter covers this.--tag-specificationssets theNametag at creation, so you can find the instance later without its ID.- The AMI is x8664, so the instance type must also be x8664. A Graviton type such as
t4g.microneeds an arm64 AMI.
Step 3: Wait and check
Use the CLI's built-in waiter to block until the instance is running, then read its details.
aws ec2 wait instance-running \
--filters Name=tag:Name,Values=tutorial-web
aws ec2 describe-instances \
--filters Name=tag:Name,Values=tutorial-web \
--query 'Reservations[].Instances[].[InstanceId,State.Name,PublicIpAddress]' \
--output tablerunning means the instance has started booting. It does not mean the operating system is ready for SSH. The status checks in a later chapter confirm that.
Common errors
| Error | Likely cause |
|---|---|
| `InvalidAMIID.NotFound` | The AMI ID belongs to a different Region. |
| `Unsupported` or `InsufficientInstanceCapacity` | The type is not offered, or has no capacity, in that Availability Zone. Try another subnet or type. |
| `UnauthorizedOperation` | Your IAM identity lacks `ec2:RunInstances` or permission on a referenced resource. |
| `InvalidKeyPair.NotFound` | The key pair is missing in this Region. |
| `InvalidParameterValue` on the security group | The group belongs to a different VPC than the subnet. |
Exercise
You run run-instances with an AMI ID copied from a colleague's account in another Region, and the call fails with InvalidAMIID.NotFound. What is the likely cause, and how can you avoid it?
Show answer
AMI IDs are specific to a Region, so the ID does not exist in your current Region. Look up the AMI in the Region you are launching in, for example with the public SSM parameter, instead of copying an ID.