Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCode Remote to EC2 instance (via SSH over SSM) #941

Open
jovanshernandez opened this issue Feb 22, 2020 · 79 comments
Open

VSCode Remote to EC2 instance (via SSH over SSM) #941

jovanshernandez opened this issue Feb 22, 2020 · 79 comments
Labels
auth-credentials authentication, authorization, credentials, AWS Builder ID, sso feature-request New feature or enhancement. May require GitHub community feedback. guidance General information and guidance, answers to FAQs, or recommended best practices/resources. remote-execute SSM, remote-ssh, CodeCatalyst dev env, remote connect service:ec2

Comments

@jovanshernandez
Copy link

jovanshernandez commented Feb 22, 2020

Desktop (please complete the following information):

  • OS: Ubuntu18 and/or Amazon Linux2
  • VS Code version: 1.41.0
  • AWS Toolkit extension version: 1.70

Anyway to connect to EC2 through VSCode without SSH Key?

Trying to connect to EC2 through VSCode without SSH Keys. I'm able to connect to EC2s using AWS Credentials, AWS Profiles, and AWS SSM, but is there a way to pass that connection through VSCode/?

@jovanshernandez jovanshernandez added the guidance General information and guidance, answers to FAQs, or recommended best practices/resources. label Feb 22, 2020
@awschristou
Copy link
Contributor

Hi @jovanshernandez , when you mention "Connect to EC2", are you trying to use VS Code's Remote Development feature, that allows users to open a remote folder in the VS Code file explorer? Or did you have something else in mind?

@jovanshernandez
Copy link
Author

@awschristou Correct, I am trying to open say the /home/ubuntu/ folder on an EC2 in my VSCode desktop file explorer.

@justinmk3

This comment has been minimized.

@justinmk3 justinmk3 added the duplicate This issue or pull request already exists. Closing issue to avoid duplicate efforts. label Jul 30, 2020
@dmattia
Copy link

dmattia commented Sep 15, 2020

I do not believe this to be a duplicate issue.

This issue is talking about a connection like aws ssm start-session --target "i-0012341ef010ffc4f", which uses the AWS CLI to open an SSH-like connection to a remote EC2. This is an AWS specific form of authentication, and is felt to be more secure by many because:

  1. You can use SSM to access instances in private subnets, whereas SSHing requires some instance in a public subnet
  2. SSM uses AWS creds, which has strong MFA support, whereas SSH is just SSH
  3. SSM access control is controlled by AWS IAM Policies, whereas SSH requires maintaining public keys
  4. All SSM access has built in monitoring and auditing by AWS Cloudtrail and the SSM service itself

So this issue is really just asking for almost a direct integration, or otherwise mirroring of functionality, of Microsoft's Remote Dev tooling but for SSM instead of SSH.

This is not related to ECS or EC2 metadata endpoints that issue #918 talks about

@justinmk3 justinmk3 reopened this Sep 15, 2020
@justinmk3 justinmk3 added auth-credentials authentication, authorization, credentials, AWS Builder ID, sso and removed duplicate This issue or pull request already exists. Closing issue to avoid duplicate efforts. labels Sep 15, 2020
@ragebiswas
Copy link

+1 for this, I see this could be useful for many.

@asherawelan
Copy link

This would be the last piece in the puzzle for us...

@justinmk3 justinmk3 changed the title Anyway to connect to EC2 through VSCode without SSH Key? Connect to EC2 through VSCode without SSH Key? Apr 14, 2021
@twitu
Copy link

twitu commented May 10, 2021

This is a powerful feature and an important component in making AWS development experience secure but also seamless. + 💯 from my side.

@justinmk3 justinmk3 changed the title Connect to EC2 through VSCode without SSH Key? VSCode Remote to EC2 instance (via SSM, without SSH) May 10, 2021
@thomas-anderson-bsl
Copy link

As a workaround, you could update the ssh configuration file to run a proxy command, which routes ssh traffic via SSM. See https://pub.towardsai.net/how-to-do-remote-development-with-vs-code-using-aws-ssm-415881d249f3

@justinmk3
Copy link
Contributor

justinmk3 commented Jul 2, 2021

Related:

@szukalski
Copy link

This will work but you have to manually specify the AWS profile and region.

Outside of VS Code, your ssh config for SSM integration would look something like this:

host i-* mi-*
  User ec2-user
  ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

Then you would ssh to your instance directly. Ie. ssh i-xzyyxzzxyyxzzxy. Things work because you have an AWS profile and region loaded.

To get things working with VS Code, your ssh config would look like this:

host i-*.*.*
  User ec2-user
  ProxyCommand bash -c "aws ssm start-session --target $(echo %h|cut -d'.' -f1) --profile $(echo %h|/usr/bin/cut -d'.' -f2) --region $(echo %h|/usr/bin/cut -d'.' -f3) --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

You then connect to your instance with i-xzyyxzzxyyxzzxy.profile_name.region.

@chm123
Copy link

chm123 commented Oct 20, 2021

I can confirm solution from @szukalski works, although I need to do some modifications due to our special AWS config. Our SSH port for EC2 instance is not open, but I can ssh through aws ssm. VS Code remote ssh works perfectly this way, and I can also forward ports in ssh config file.

@jtele2
Copy link

jtele2 commented Nov 15, 2021

@chm123 Can you elaborate? I cannot get this to work like so.

@chm123
Copy link

chm123 commented Nov 16, 2021

@chm123 Can you elaborate? I cannot get this to work like so.

@jtele2 Basically you can follow
https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-getting-started-enable-ssh-connections.html

And try SSH from terminal (not from VS Code). If this works for you, you should be able to connect via VS Code.

@jtele2
Copy link

jtele2 commented Nov 17, 2021

@chm123 Thank you for your response. I can get in via Windows Subsystem for Linux (WSL) but not from PowerShell with the proxy command. I have to use WSL because I cannot install SessionManagerPlugin directly to PowerShell (non-Admin rights), but can install to WSL.

Is there a way to make ProxyCommand use WSL.exe to setup the Remote-SSH session?

@chm123
Copy link

chm123 commented Nov 17, 2021

@jtele2

I'm using MacBook, so I'm not familiar with WSL. You can try this
https://stackoverflow.com/questions/60150466/can-i-ssh-from-wsl-in-visual-studio-code

@jtele2
Copy link

jtele2 commented Nov 19, 2021

@szukalski Any ideas on how to get this to work with Windows (AWS CLI on PowerShell)?

@PeterBaker0
Copy link

+1 for this feature - would be very useful - going to try and setup workaround as above for now.

@mrgum
Copy link

mrgum commented Mar 30, 2022

one thing to note, vscode needs your credentials, so if you use temporary credentials, say via SSO to an IdP you need to close vscode and open it again from a terminal that has your session token, a PITA

if anyone knows a work-around, that doesn't involve session up a credentials file or iam user, that would be great

@serverhorror
Copy link

After this issue is addressed, these won't be needed:

  • aws cli version 2

AWS Toolkit will auto-configure SSH for you. The reason for requiring SSH is because that is what vscode-remote requires.

Please keep in mind that some of us have a mind boggling amount of profiles in ~/.aws/config. If we don't need aws cli it would be great to still use the AWS eco system (profiles, SSO, ...).

@justinmk3
Copy link
Contributor

If we don't need aws cli it would be great to still use the AWS eco system (profiles, SSO, ...).

Absolutely. If you get a chance to try out "Dev Environments" with https://codecatalyst.aws/ , please let us know what you think. We have put a lot of work into how AWS Toolkit presents credentials, with more improvements planned.

@sholtomaud
Copy link

sholtomaud commented Mar 14, 2023 via email

@borontion
Copy link

borontion commented Mar 20, 2023

I implemented a simple prototype EC2 explorer, which can 1) start / stop instances and 2) open via SSH: https://marketplace.visualstudio.com/items?itemName=PengzhanZhao.ec2-farm. However, It does require to provision EC2 instances with a uploaded key pair.

@sholtomaud
Copy link

@borontion nice. However we don't want to use key pairs or ssh.

@serverhorror
Copy link

For anyone interested: the issue in microsoft/vscode-remote-release#8186 is now eligible for upvotes. If we reach 10 or more it will be considered for the next step. Let the upvotes ensue...

@serverhorror
Copy link

VS Code has accepted the refrenced issue in their backlog.

We might want to bring our input so it does get enough priority and people can create useful extensions with the solution they come up with.

@iancullinane
Copy link

@Roseidon your solution gets me through the mfa portion, but asks for a password afterwards. Is this supposed to happen? I don't have a password for this instance.

@Roseidon
Copy link

@Roseidon your solution gets me through the mfa portion, but asks for a password afterwards. Is this supposed to happen? I don't have a password for this instance.

Unfortunately not, I haven't been prompted for a password afterwards.
Maybe that is an auth setting on the instance itself. So far I only worked with saving my public ssh key on the instance, but not with user/pass.

@trallnag
Copy link

For me it works on Windows 10, VS Code 1.79.2, Remote SSH 0.102.0. Entry in SSH config:

Host server
   User ubuntu
   ProxyCommand C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "aws ssm start-session --target i-0824c21f7abd6347c --document-name AWS-StartSSHSession --parameters portNumber=%p"

@justinmk3
Copy link
Contributor

justinmk3 commented Jun 27, 2023

Status

We are actively working on these features.

  • Implemented:
    • Open Terminal to an EC2 instance (including Windows instances)
    • AWS: Connect to EC2 Instance... command
      • image
    • ✅ Browse EC2 instances in AWS Explorer
    • ✅ Start/Stop/Reboot actions #1559
    • ✅ Connect to EC2 instance via VSCode remote-ssh
  • Not implemented yet:
    • Connect to EC2 instance from Windows
    • Connect to EC2 Windows instance
    • Other actions: list EBS volumes, set shutdown schedule #1559

@mwanser1

This comment was marked as resolved.

@justinmk3
Copy link
Contributor

Is AWS: Connect to EC2 Instance in version 1.91.0?

The features in the above comment are implemented but not released. We're hoping to make it available as an "experimental" option.

Will update this issue when there is any news.

@mwanser1

This comment was marked as duplicate.

@justinmk3
Copy link
Contributor

See #941 (comment)

@jfmcdowell
Copy link

Is there an update on this? I periodically (about every VSCode update) lose the ability to run SSM via the proxy script. Using my terminal is fine but inside of VSCode it seems to lose the awscli path.

See this issue for additional details

@justinmk3
Copy link
Contributor

Current status is #941 (comment)

@la-cruche
Copy link

@justinmk3 those features are still not part of the extension? #941 (comment) any ETA?

@deanhtid
Copy link

deanhtid commented Mar 5, 2024

anything new on this? @justinmk3

@justinmk3
Copy link
Contributor

Current status is #941 (comment)

@ekalosak
Copy link

ekalosak commented May 6, 2024

Would like to see progress on this as well - it was a surprise to not see EC2 in the AWS VScode toolkit. I'm still cycling the public IP for my ec2 instance :(

@SuperP4rks
Copy link

Would it be possible to release this as experimental as mentioned in an above message @justinmk3.

My understand would be that there would be an ability to SSM on Linux/Mac in its current phase.

@sholtomaud
Copy link

sholtomaud commented Jun 9, 2024

This Connect to EC2 instance via VSCode remote-ssh, is that considered zero-trust with MFA auth + Enterprise AzureAD? Can I access the EC2 without being in the private network using the DirectConnection to the AWS data centre from the on-prem network? OR is all traffic over the public internet, and Enterprise AzureAD auth not supported?

@TheCaffeintedSloth
Copy link

TheCaffeintedSloth commented Sep 26, 2024

Would it be possible to release this as experimental as mentioned in an above message @justinmk3.

My understand would be that there would be an ability to SSM on Linux/Mac in its current phase.

Echoing this from June. Any update on when the experimental will be released? Have been hoping for this feature for quite awhile. @justinmk3

@justinmk3
Copy link
Contributor

justinmk3 commented Sep 26, 2024

We've made progress on this and it's likely for Q1. We definitely want to see this too :)

@tehnrd
Copy link

tehnrd commented Sep 28, 2024

I have a single stateless ec2 bastion box, so the instance ID is constantly changing. Below is a simple config file that works for me and maybe others will find helpful. It will dynamically look up the ec2 instance ID based on the name of the ec2 instance.

I did have to set up a Key Pair for this to work.

ssh config file entry:

Host auditr
    HostName ec2-0-00-00-000.us-east-2.compute.amazonaws.com
    User ec2-user
    IdentityFile /Path/To/Pem/file.pem
    ProxyCommand aws ssm start-session --target $(aws ec2 describe-instances --profile profileName --filters 'Name=tag:Name,Values=name-of-ec2-instance' 'Name=instance-state-name,Values=running' --query 'Reservations[].Instances[].InstanceId' --output text) --profile profileName --document-name AWS-StartSSHSession --parameters 'portNumber=%p'

@sholtomaud
Copy link

I have a single stateless ec2 bastion box ...

There's your problem. Just delete the Bastion and this issue is solved.

@tehnrd
Copy link

tehnrd commented Sep 30, 2024

I have a single stateless ec2 bastion box ...

There's your problem. Just delete the Bastion and this issue is solved.

I'm unsure how this helps, as the instance ID would get recreated/change, and you'd have to constantly update your scripts with the new instance ID.

Also, deleting is not a great option when using CDK or other IaC, as you'll get stack drift and all sorts of deployment challenges. I just shut down (not terminate) the instance when not needed, and the script above works great if the instance gets swapped out for any reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth-credentials authentication, authorization, credentials, AWS Builder ID, sso feature-request New feature or enhancement. May require GitHub community feedback. guidance General information and guidance, answers to FAQs, or recommended best practices/resources. remote-execute SSM, remote-ssh, CodeCatalyst dev env, remote connect service:ec2
Projects
None yet
Development

No branches or pull requests