Sub topic 7
EC2 Instance Connect and temporary keys
This chapter assumes you can already connect to an instance with ssh, that the instance's security group allows port 22 from your address, and that the AWS CLI is configured. It covers EC2 Instance Connect, which replaces a long-lived key pair with a short-lived public key.
What Instance Connect does
With a normal key pair, the public key is installed once at launch and stays on the instance until someone removes it. EC2 Instance Connect changes this. You use IAM permissions to push a one-time public key to the instance, and the instance accepts it for about 60 seconds. After that the key is no longer valid for new logins.
The connection itself is still SSH over port 22. Instance Connect changes how the key gets onto the instance. It does not change the network path.
Requirements
- The instance runs an AMI with the Instance Connect package installed. Amazon Linux 2 and Amazon Linux 2023 include it. Other AMIs may need it installed.
- The security group allows inbound TCP 22 from your address, as in the earlier chapters.
- Your IAM identity is allowed to call
ec2-instance-connect:SendSSHPublicKeyon the instance. - You use the correct login user for the AMI, such as
ec2-useron Amazon Linux.
Connecting from the CLI
Generate a key pair if you do not have one. This key lives on your machine, but the instance only trusts it briefly.
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519Push the public key, then connect right away:
aws ec2-instance-connect send-ssh-public-key \
--instance-id i-0123456789abcdef0 \
--instance-os-user ec2-user \
--ssh-public-key file://$HOME/.ssh/id_ed25519.pub
ssh -i ~/.ssh/id_ed25519 [email protected]If the first command succeeds, it prints "Success": true. The --instance-os-user value must match the user you pass to ssh. If you take more than about 60 seconds, push the key again.
Controlling who can push keys
Access is governed by IAM, so you can limit it to specific instances and login users. A policy can restrict the ec2:osuser condition key to ec2-user. Removing the permission from a user stops them from pushing new keys, and no key cleanup on the instance is needed.
Common errors
| Symptom | Likely cause |
|---|---|
| `AccessDeniedException` from the CLI | The IAM identity lacks `ec2-instance-connect:SendSSHPublicKey` for that instance or OS user |
| `Permission denied (publickey)` | More than 60 seconds passed, or the OS user does not match |
| Connection timeout | Port 22 is blocked by the security group or route, which Instance Connect cannot fix |
| Command not found for `ec2-instance-connect` | The AWS CLI is too old to include the command |
Exercise
You run send-ssh-public-key successfully, wait two minutes, and then run ssh. The login fails with "Permission denied (publickey)". Why, and what do you do?
Show answer
The pushed public key is valid for only about 60 seconds, and it has expired. Run send-ssh-public-key again and connect immediately.
Exercise
Does Instance Connect remove the need to open port 22 in the security group?
Show answer
No. The session is a normal SSH connection, so port 22 must still be reachable from your address. Only the key distribution changes.