An easier way to do EC2 instance connect

Will Thames
Jul 30 · 1 min read

AWS recently announced EC2 Instance Connect, a way to grant yourself temporary ssh access to a host without the need for more permanent ssh credentials to be stored on the host.

While that’s very cool, the instructions to use it make it nowhere as nice as using standard ssh. You need to:

  • Find out the instance ID
  • Run a lengthy command to push your ssh key onto the instance
  • Then ssh to the instance

Thankfully, with the joys of ssh’s ProxyCommand, we can make this a lot nicer.

A helper script can do all the grunt work above

Then all we need to do is be able to able to set an appropriate ProxyCommand for the instances

All of our hosts are on private networks with separate CIDR ranges, so that I can map the hostname/IP address to a region, which means that ~/.ssh/config can be set up as follows:

Host ip-10-1-*
User ec2-user
ProxyCommand ~/.ssh/aws-proxy.sh --profile my_aws_profile --region ap-southeast-2 --filter private-dns-name --key ~/.ssh/my_private_key %r %h %p
Host 10.1.*
User ec2-user
ProxyCommand ~/.ssh/aws-proxy.sh --profile my_aws_profile --region ap-southeast-2 --filter private-ip-address --key ~/.ssh/my_private_key %r %h %p

At that point, runningssh 10.1.2.3 logs me straight in. This completely mitigates the need for SSH key rotation (for example as part of an offboarding process or a regular 90-day access key rotation).

As well as being in a gist, the aws-proxy.sh is also in a github repo for further improvement

Skedulo Product Development

Creative Engineers, Designers and Product people solving mobile workforce management problems for our customers. https://skedulo.com

Will Thames

Written by

Skedulo Product Development

Creative Engineers, Designers and Product people solving mobile workforce management problems for our customers. https://skedulo.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade