aws-toolkit-vscode: EC2 Remote from Windows : ec2_connect.ps1 missing

Problem

When trying the new experimental feature to “connect to EC2 instance in a new Remote window” from Windows, the script ec2_connect.ps1 is missing.

image

Logs :

2024-12-16 13:36:48.386 [error] aws.ec2.openRemoteConnection: Error: Failed to update connect script [ConnectScriptUpdateFailed]
	 -> EntryNotFound (FileSystemError): Error: ENOENT: no such file or directory, open 'c:\Users\[redacted]\.vscode\extensions\amazonwebservices.aws-toolkit-vscode-3.39.0\resources\ec2_connect.ps1' [FileNotFound]

Steps to reproduce the issue

  1. Start VSCode on Windows, with AWS Toolkit 3.39
  2. Enable experimental feature “ec2RemoteConnect”
  3. Browse to an EC2 in AWS Toolkit explorer
  4. Click the button “connect to EC2 instance in a new Remote window”
  5. The error is shown

Expected behavior

I should be able to open a new VSCode window, connected to this remote EC2 through SSM.

System details (run AWS: About and/or Amazon Q: About)

  • OS: Windows 11
  • Visual Studio Code version: 1.96.0
  • AWS Toolkit version: 3.39.0
  • Amazon Q version: NA

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 5
  • Comments: 22 (5 by maintainers)

Most upvoted comments

Connecting from Windows is not yet implemented. We plan to follow up on this.

Just as a recap for others looking at this to save yourself the two hours it took me:

  • Depending where you are at in the debugging process, make sure to delete your .ssh config or at least the ec2-user-* portion
  • Install powershell 7
  • Open C:\Users\<USERNAME>\.cursor\extensions\amazonwebservices.aws-toolkit-vscode-3.57.0\dist\src\extensionNode.js and find and replace powershell.exe with pwsh (note that file path is for cursor, but just use .vscode for vscode
  • Copy the file from this PR into the amazonwebservices.aws-toolkit-vscode-3.57.0\resources folder as ec2_connect.ps1.

The PR has been merged, so hopefully a release soon will contain the fix but for now this is how you can do.

We’re certainly open to changing https://github.com/aws/aws-toolkit-vscode/blob/1d79df7ea7e9d44ad86e0fdb20d7f1fa4d67eca1/packages/core/src/shared/extensions/ssh.ts#L72 if that’s the code you are referring to. Or could update that function to try both powershell.exe and pwsh, and use the first one that succeeds.

@nathanlisabio Thanks for the info, I managed to track it down and change the file locally and also have it working! I had to edit the contents of .vscode\extensions\amazonwebservices.aws-toolkit-vscode-3.50.0\dist\src\extensionNode.js to replace three occurrences of powershell.exe with pwsh

updates:

  1. got connect via vscode to work (with caveats)

    • only works if target is ubuntu
    • does not seem to work if the OS is Amazon Linux (2025-03-18 01:29:48.370 [error] aws.ec2.openRemoteConnection: Error: Unrecognized OS name Amazon Linux AMI on instance i-0890259255f2712c1 [UnknownEc2OS] )
      • the connect via terminal does not have this issue
    • seems to have another failure connect condition, unsure about this one tho:
      • 2025-03-18 01:42:59.750 [warning] ec2: failed to clean keys: 'Error: Failed in sending command to target i-0e190c5ec1e3c650c\n' + '\t -> Error: {"state":"FAILURE","reason":{"$metadata":{"httpStatusCode":200,"requestId":"20d8db59-60ef-4154-8e28-40e3b8e1bec5","attempts":1,"totalRetryDelay":0},"CloudWatchOutputConfig":{"CloudWatchLogGroupName":"","CloudWatchOutputEnabled":false},"CommandId":"7f86a4b0-b395-40d7-b0d7-340096b4fe65","Comment":"","DocumentName":"AWS-RunShellScript","DocumentVersion":"$DEFAULT","ExecutionElapsedTime":"PT0.004S","ExecutionEndDateTime":"2025-03-18T05:42:54.732Z","ExecutionStartDateTime":"2025-03-18T05:42:54.732Z","InstanceId":"i-0e190c5ec1e3c650c","PluginName":"aws:runShellScript","ResponseCode":1,"StandardErrorContent":"","StandardErrorUrl":"","StandardOutputContent":"","StandardOutputUrl":"","Status":"Failed","StatusDetails":"Failed"},"observedResponses":{"400: UnknownError":1,"200: OK":1}}' 2025-03-18 01:43:05.175 [error] aws.ec2.openRemoteConnection: Error: Failed in sending command to target i-0e190c5ec1e3c650c -> Error: {"state":"FAILURE","reason":{"$metadata":{"httpStatusCode":200,"requestId":"564bfa05-8b95-4785-a444-781de4f48a3f","attempts":1,"totalRetryDelay":0},"CloudWatchOutputConfig":{"CloudWatchLogGroupName":"","CloudWatchOutputEnabled":false},"CommandId":"85f11865-f926-4e34-9d4a-63fcdbeaaab5","Comment":"","DocumentName":"AWS-RunShellScript","DocumentVersion":"$DEFAULT","ExecutionElapsedTime":"PT0S","ExecutionEndDateTime":"2025-03-18T05:43:00.136Z","ExecutionStartDateTime":"2025-03-18T05:43:00.136Z","InstanceId":"i-0e190c5ec1e3c650c","PluginName":"aws:runShellScript","ResponseCode":1,"StandardErrorContent":"","StandardErrorUrl":"","StandardOutputContent":"","StandardOutputUrl":"","Status":"Failed","StatusDetails":"Failed"},"observedResponses":{"400: UnknownError":1,"200: OK":1}} 2025-03-18 01:43:09.860 [info] telemetry: sent batch (size=9)
      • connect via terminal does not encounter this issue
  2. powershell.exe (the default come-with pwsh) does not pass the testSshConnection function, causing the unknown port issue mentioned earlier. By changing it to pwsh (v7.5.0), it just worked… (sshConfig.ts). I’m unsure which direction to go to debug why v5.1 encounters this issue

    • this is an issue bc pwsh7 would require external installation dependencies
  3. during testing, encountered other issues that are not related to this issue

    • when changing aws connections/accounts, the explorer is unable to refresh it’s contents. I needed to close the debugging session and re-open to refresh the contents

https://github.com/aws/aws-toolkit-vscode/issues/6802

Testing SSM connection to instance failed: Connection closed by UNKNOWN port 65535 [EC2SSMTestConnect]

Before connecting, AWS Toolkit tries a test command to ensure that things will actually work: https://github.com/aws/aws-toolkit-vscode/blob/1d79df7ea7e9d44ad86e0fdb20d7f1fa4d67eca1/packages/core/src/shared/extensions/ssh.ts#L150

That is failing from a Connection closed by UNKNOWN port 65535 error, which seems like a network or permissions issue.

some updates:

  1. just by using the above script (small modification to the input args), was able to get the terminal button to connect successfully
  2. however, using the connect using vscode method got me to a brand new error. If anyone has any hints, please drop:

2025-03-06 21:58:23.729 [error] aws.ec2.openRemoteConnection: Error: Unable to connect to target instance {the instance id redacted} on region us-east-1. Testing SSM connection to instance failed: Connection closed by UNKNOWN port 65535 [EC2SSMTestConnect] 2025-03-06 22:00:56.941 [info] telemetry: sent batch (size=19)

here is the ai generated version i am working on testing

#Requires -Version 5.1

# Usage:
#   When connecting to a dev environment
#   $env:AWS_REGION="…"; $env:AWS_SSM_CLI="…"; $env:STREAM_URL="…"; $env:TOKEN="…"; $env:LOG_FILE_LOCATION="…"; $env:DEBUG_LOG="…"; .\ec2_connect.ps1

# Exit on errors
$ErrorActionPreference = "Stop"

# Date command equivalent
function Get-DateString {
    return Get-Date -Format "yyyy/MM/dd HH:mm:ss"
}

function Write-Log {
    param (
        [Parameter(Mandatory=$true)]
        [string]$Message
    )
    "$(Get-DateString) $Message" | Out-File -FilePath $env:LOG_FILE_LOCATION -Append
}

function Test-RequiredNoLog {
    param (
        [string]$Name,
        [string]$Value
    )
    if ([string]::IsNullOrEmpty($Name) -or [string]::IsNullOrEmpty($Value)) {
        Write-Log "error: missing required arg: $Name"
        exit 1
    }
}

function Test-Required {
    param (
        [string]$Name,
        [string]$Value
    )
    Test-RequiredNoLog -Name $Name -Value $Value
    Write-Log "$Name=$Value"
}

function Start-EC2Session {
    param (
        [string]$AWSSSMCLI,
        [string]$AWSRegion,
        [string]$StreamURL,
        [string]$Token,
        [string]$SessionID
    )
    
    $jsonPayload = @{
        streamUrl = $StreamURL
        tokenValue = $Token
        sessionId = $SessionID
    } | ConvertTo-Json -Compress
    
    # Execute the SSM CLI command
    & $AWSSSMCLI $jsonPayload $AWSRegion "StartSession"
}

function Main {
    Write-Log "=============================================================================="
    
    Test-Required -Name "DEBUG_LOG" -Value $env:DEBUG_LOG
    Test-Required -Name "AWS_REGION" -Value $env:AWS_REGION
    
    Test-Required -Name "SESSION_ID" -Value $env:SESSION_ID
    Test-RequiredNoLog -Name "STREAM_URL" -Value $env:STREAM_URL
    Test-RequiredNoLog -Name "TOKEN" -Value $env:TOKEN

    # Only log file paths when debug level is enabled
    if ([int]$env:DEBUG_LOG -eq 1) {
        Test-Required -Name "AWS_SSM_CLI" -Value $env:AWS_SSM_CLI
        Test-Required -Name "LOG_FILE_LOCATION" -Value $env:LOG_FILE_LOCATION
    }
    else {
        Test-RequiredNoLog -Name "AWS_SSM_CLI" -Value $env:AWS_SSM_CLI
        Test-RequiredNoLog -Name "LOG_FILE_LOCATION" -Value $env:LOG_FILE_LOCATION
    }

    Start-EC2Session -AWSSSMCLI $env:AWS_SSM_CLI `
                    -AWSRegion $env:AWS_REGION `
                    -StreamURL $env:STREAM_URL `
                    -Token $env:TOKEN `
                    -SessionID $env:SESSION_ID
}

# Execute main function
Main