To authenticate with aws using the correct profile, I use aws-vault. Feel free to remove that and use whatever aws authentication mechanism is relevant for you.
- ssh should be running on the target ec2 instance
- your ssh key should be in
~/.ssh/authorized_keyson the instance - ssm should be enabled on that instance
AWS_VAULT_PROFILE=my-profile
EC2_INSTANCE=i-abcabcabc
aws-vault exec $AWS_VAULT_PROFILE -- ssh -fNT -o PreferredAuthentications=publickey \
-o ProxyCommand="aws ssm start-session --target $EC2_INSTANCE --document-name AWS-StartSSHSession --parameters portNumber=22" \
-D 6669 \
ssm-user@$EC2_INSTANCE