private/start/Start-PortForwardingSession.ps1

function Start-PortForwardingSession
{
    [CmdletBinding(PositionalBinding = $true)]
    [OutputType([System.Collections.Hashtable])]
    param(
        [Parameter(Mandatory = $true)]
        [ValidatePattern('^i-[a-f0-9]{8}(?:[a-f0-9]{9})?$')]
        [string] $InstanceId,

        [Parameter(Mandatory = $true)]
        [ValidateRange(5000, 65535)]
        [int] $LocalPort,

        [Parameter(Mandatory = $false)]
        [ValidateRange(1, 65535)]
        [int] $RemotePort = 3389,

        [Parameter(Mandatory = $false)]
        [ValidatePattern('^.{1,256}$')]
        [string] $Reason = 'Session created with EC2Remote PowerShell module.'
    )

    Write-Host "Starting port-forwarding session on local port $localPort with $instanceId on port $RemotePort.." -ForegroundColor DarkGray

    try
    {
        $params = @{
            Target       = $InstanceId
            DocumentName = 'AWS-StartPortForwardingSession'
            Reason       = $Reason
            ErrorAction  = 'Stop'
            Parameters   = @{
                localPortNumber = $LocalPort.ToString()
                portNumber      = $RemotePort.ToString()
            }
        }

        $startSessionResponse = Start-SSMSession @params

        return @{ Parameters = $params; Response = $startSessionResponse }
    }
    catch
    {
        Write-Error "An error occurred starting port-forwarding session. $_"
        throw
    }
}