Skip to content

Instantly share code, notes, and snippets.

@sss-ng
Created November 20, 2024 19:28
Show Gist options
  • Select an option

  • Save sss-ng/e1d2c19b7f169ae990d966e805603e8d to your computer and use it in GitHub Desktop.

Select an option

Save sss-ng/e1d2c19b7f169ae990d966e805603e8d to your computer and use it in GitHub Desktop.
Access private AWS resources from outside VPC

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_keys on 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment