🪟

[メモ] VSCode Remote-SSH 拡張で EC2 インスタンスに接続する Windows編

2025/01/23に公開
1

はじめに

GUI がない踏み台サーバで作業する際に、VSCode の Remote - SSH 拡張を使って VSCode でファイル編集を行えるようにしたメモです。
for Windows です。

前提

クライアント環境

  • OS:Windows 11 Pro 22H2

ソフトウェア

事前確認

AWS CLI での接続確認

対象 AWS アカウントで接続確認します。

> aws ssm start-session --target <instance-id> --profile <profile>

Starting session with SessionId: <your_user>-xxxxxxxxxxxxxxxxxxxxxxxxx
sh-5.2$ whoami
ssm-user
sh-5.2$ exit
exit


Exiting session with sessionId: <your_user>-xxxxxxxxxxxxxxxxxxxxxxxxx

SSM での接続確認

%USERPROFILE%/.ssh/config に次を追加します:

Host dom
    HostName <instance-id>
    Port 22
    User ec2-user
    ProxyCommand C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "aws.exe ssm start-session --target %h --document-name AWS-StartSSHSession --parameters portNumber=%p --profile <profile>"
    IdentityFile <path_to_your_ssh_key>

ssh で接続できれば勝ち確、準備完了です。

> ssh dom

   ,     #_
   ~\_  ####_        Amazon Linux 2023
  ~~  \_#####\
  ~~     \###|
  ~~       \#/ ___   https://aws.amazon.com/linux/amazon-linux-2023
   ~~       V~' '->
    ~~~         /
      ~~._.   _/
         _/ _/
       _/m/'
Last login: Mon Jan 13 01:28:54 2025 from 127.0.0.1
[ec2-user@ip-10-100-XXX-XXX ~]$

VSCode で接続してみよう

「Remote - SSH」をインストールしていると、左下に「Open a Remote Window」アイコンが表示されます。またアクティビティバーに「Remote Explorer」が追加されます。今回は「Remote Explorer」で設定していきます。

Remote Explorer を開くと、「SSH」の下に先ほど設定したホスト(dom)が登場しているはず。

ホストの OS を選択します。今回の対象ホストは Amazon Linux 2023 だったので Linux を選択。

ちょっと待つと、接続できました。
ホストにあるファイルを参照・編集できます。ばんざーい。

Tips

SessionManagerPlugin が見つからないとき

なお、VSCode から Remote-SSH で接続した際に SessionManagerPlugin is not found. とエラーが出ることがあります。システム環境変数には session-manager-plugin.exe へのパスが入っているんだけどな...?

エラーログ

[11:15:14.783] Log Level: 2
[11:15:14.802] SSH Resolver called for "ssh-remote+dom", attempt 1
[11:15:14.803] "remote.SSH.useLocalServer": false
[11:15:14.803] "remote.SSH.useExecServer": true
[11:15:14.803] "remote.SSH.showLoginTerminal": false
[11:15:14.804] "remote.SSH.remotePlatform": {"dom":"linux"}
[11:15:14.804] "remote.SSH.path": undefined
[11:15:14.804] "remote.SSH.configFile": undefined
[11:15:14.804] "remote.SSH.useFlock": true
[11:15:14.804] "remote.SSH.lockfilesInTmp": false
[11:15:14.805] "remote.SSH.localServerDownload": auto
[11:15:14.805] "remote.SSH.remoteServerListenOnSocket": false
[11:15:14.805] "remote.SSH.showLoginTerminal": false
[11:15:14.805] "remote.SSH.defaultExtensions": []
[11:15:14.805] "remote.SSH.loglevel": 2
[11:15:14.806] "remote.SSH.enableDynamicForwarding": true
[11:15:14.806] "remote.SSH.enableRemoteCommand": false
[11:15:14.806] "remote.SSH.serverPickPortsFromRange": {}
[11:15:14.806] "remote.SSH.serverInstallPath": {}
[11:15:14.806] "remote.SSH.permitPtyAllocation": false
[11:15:14.806] "remote.SSH.preferredLocalPortRange: undefined
[11:15:14.806] "remote.SSH.useCurlAndWgetConfigurationFiles: false
[11:15:14.813] VS Code version: 1.95.3
[11:15:14.813] Remote-SSH version: remote-ssh@0.115.1
[11:15:14.813] win32 x64
[11:15:14.817] SSH Resolver called for host: dom
[11:15:14.817] Setting up SSH remote "dom"
[11:15:14.832] Using commit id "f1a4fb101478ce6ec82fe9627c43efbf9e98c813" and quality "stable" for server
[11:15:14.838] Script variables:
 {
  "InstallExitCode.24": "AlreadyInProgress",
  "InstallExitCode.25": "ServerDownloadFailed",
  "InstallExitCode.26": "NoDownloaderAvailable",
  "InstallExitCode.27": "UnsupportedArch",
  "InstallExitCode.28": "StatusCheckFailed",
  "InstallExitCode.29": "NeedInsidersArch",
  "InstallExitCode.30": "NoDownloaderAvailableForStatusCheck",
  "InstallExitCode.31": "ServerTransferFailed",
  "InstallExitCode.32": "ServerFailedToStart",
  "InstallExitCode.33": "NeedInsidersWindows",
  "InstallExitCode.34": "CreateInstallDirFailed",
  "InstallExitCode.35": "UnsupportedPlatform",
  "InstallExitCode.36": "ServerTerminatedCVE20201416",
  "InstallExitCode.37": "UnpackFailed",
  "InstallExitCode.38": "ChangeDirFailed",
  "InstallExitCode.AlreadyInProgress": "24",
  "InstallExitCode.ServerDownloadFailed": "25",
  "InstallExitCode.NoDownloaderAvailable": "26",
  "InstallExitCode.NoDownloaderAvailableForStatusCheck": "30",
  "InstallExitCode.UnsupportedArch": "27",
  "InstallExitCode.StatusCheckFailed": "28",
  "InstallExitCode.NeedInsidersArch": "29",
  "InstallExitCode.ServerTransferFailed": "31",
  "InstallExitCode.ServerFailedToStart": "32",
  "InstallExitCode.NeedInsidersWindows": "33",
  "InstallExitCode.CreateInstallDirFailed": "34",
  "InstallExitCode.UnsupportedPlatform": "35",
  "InstallExitCode.ServerTerminatedCVE20201416": "36",
  "InstallExitCode.UnpackFailed": "37",
  "InstallExitCode.ChangeDirFailed": "38",
  "InstallUnpackCode.Success": "success",
  "InstallUnpackCode.Error": "error",
  "InstallUnpackCode.MissingFiles": "missingFiles",
  "uuid": "c749169274fc",
  "startMarker": "c749169274fc: running",
  "commitId": "f1a4fb101478ce6ec82fe9627c43efbf9e98c813",
  "quality": "stable",
  "token": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  "vscodeAgentFolder": "$HOME/.vscode-server",
  "allowClientDownload": "1",
  "forceClientDownload": "0",
  "cliNameInArchive": "code",
  "ignoreWgetConfigFlag": " --no-config ",
  "ignoreCurlConfigFlag": " --disable ",
  "wgetTriesSegment": "--tries=1",
  "listenArgs": "--on-host=127.0.0.1 --on-port",
  "getDownloadServerStartTrigger": "c749169274fc:trigger_server_download",
  "getDownloadServerEndTrigger": "c749169274fc:trigger_server_download_end",
  "getProgressDownloading": "c749169274fc%%1%%",
  "getProgressInstalling": "c749169274fc%%2%%"
}
[11:15:14.841] Install and start server if needed
[11:15:14.847] Checking ssh with "C:\Windows\system32\ssh.exe -V"
[11:15:14.849] Got error from ssh: spawn C:\Windows\system32\ssh.exe ENOENT
[11:15:14.850] Checking ssh with "C:\Windows\ssh.exe -V"
[11:15:14.851] Got error from ssh: spawn C:\Windows\ssh.exe ENOENT
[11:15:14.851] Checking ssh with "C:\Windows\System32\Wbem\ssh.exe -V"
[11:15:14.853] Got error from ssh: spawn C:\Windows\System32\Wbem\ssh.exe ENOENT
[11:15:14.853] Checking ssh with "C:\Windows\System32\WindowsPowerShell\v1.0\ssh.exe -V"
[11:15:14.854] Got error from ssh: spawn C:\Windows\System32\WindowsPowerShell\v1.0\ssh.exe ENOENT
[11:15:14.855] Checking ssh with "C:\Windows\System32\OpenSSH\ssh.exe -V"
[11:15:15.092] > OpenSSH_for_Windows_9.5p1, LibreSSL 3.8.2

[11:15:15.171] Running script with connection command: "C:\Windows\System32\OpenSSH\ssh.exe" -T -D 56134 "dom" sh
[11:15:15.174] Generated SSH command: 'type "C:\Users\YOUR~1.NAME\AppData\Local\Temp\vscode-linux-multi-line-command-dom-666340663.sh" | "C:\Windows\System32\OpenSSH\ssh.exe" -T -D 56134 "dom" sh'
[11:15:15.174] Using connect timeout of 17 seconds
[11:15:15.175] Terminal shell path: C:\WINDOWS\System32\cmd.exe
[11:15:17.395] > SessionManagerPlugin is not found. Please refer to SessionManager Documentation here: http://docs.aws.amazon.com/console/systems-manager/session-manager-plugin-not-found
> ]0;C:\WINDOWS\System32\cmd.exe
[11:15:17.396] Got some output, clearing connection timeout
[11:15:17.513] > Connection closed by UNKNOWN port 65535
> プロセスが、存在しないパイプに書き込もうとしました。
[11:15:18.769] "install" terminal command done
[11:15:18.770] Install terminal quit with output: プロセスが、存在しないパイプに書き込もうとしました。
[11:15:18.770] Received install output: プロセスが、存在しないパイプに書き込もうとしました。
[11:15:18.770] Failed to parse remote port from server output
[11:15:18.772] Resolver error: Error: 
	at v.Create (c:\Users\your.name\.vscode\extensions\ms-vscode-remote.remote-ssh-0.115.1\out\extension.js:2:493431)
	at t.handleInstallOutput (c:\Users\your.name\.vscode\extensions\ms-vscode-remote.remote-ssh-0.115.1\out\extension.js:2:490753)
	at t.tryInstall (c:\Users\your.name\.vscode\extensions\ms-vscode-remote.remote-ssh-0.115.1\out\extension.js:2:608797)
	at async c:\Users\your.name\.vscode\extensions\ms-vscode-remote.remote-ssh-0.115.1\out\extension.js:2:568008
	at async t.withShowDetailsEvent (c:\Users\your.name\.vscode\extensions\ms-vscode-remote.remote-ssh-0.115.1\out\extension.js:2:571256)
	at async P (c:\Users\your.name\.vscode\extensions\ms-vscode-remote.remote-ssh-0.115.1\out\extension.js:2:564794)
	at async t.resolve (c:\Users\your.name\.vscode\extensions\ms-vscode-remote.remote-ssh-0.115.1\out\extension.js:2:568667)
	at async c:\Users\your.name\.vscode\extensions\ms-vscode-remote.remote-ssh-0.115.1\out\extension.js:2:839059
[11:15:18.779] ------

こちらの記事を参考に、 C:\Windows\System32\ 配下に session-manager-plugin.exe をコピーして解消。

https://qiita.com/dairappa/items/8347fe7faf89b6ba364e

copy "c:\Program Files\Amazon\SessionManagerPlugin\bin\session-manager-plugin.exe" c:\Windows\System32\

参考

1

Discussion