Open a Private API Gateway in Local Edge via SSM Port Forwarding (No Admin Rights)

Advertisement
Open a Private API Gateway in Local Edge via SSM Port Forwarding (No Admin Rights) AWS
Advertisement

At work we have an internal web page served through a private API Gateway. Because the API is private, it can’t be reached from outside the VPC. Until now I checked it by RDP-ing into a bastion EC2 instance and using the browser there. It works, but a full RDP session just to look at one page gets old fast.

I wanted to open it in the browser on my own PC. The catch: it’s a company laptop without admin rights, so editing the hosts file is out.

Short answer: combining SSM port forwarding to a remote host with a Microsoft Edge launch option lets you open the page in local Edge at its normal URL, with no admin rights and no certificate errors. I wrapped it in a batch file: double-click it to bring up the page. Closing Edge closes the tunnel automatically.

I already RDP into the bastion over SSM port forwarding (see RDP to an EC2 Windows Server without opening the RDP port). This post builds on that.

How the traffic flows

  1. Local PC → SSM: a launch option makes Edge resolve the API hostname to 127.0.0.1 port 443. The Session Manager plugin listening there forwards the traffic to SSM
  2. SSM → bastion EC2: SSM builds the tunnel through the SSM Agent on the bastion. Only outbound traffic from the bastion to SSM is needed, so no inbound rule on the bastion
  3. Bastion EC2 → VPC endpoint: the bastion connects to the execute-api VPC endpoint on TCP 443. The hostname is resolved on the bastion side, inside the VPC
  4. VPC endpoint → API Gateway: the API’s resource policy only accepts calls from the allowed VPC endpoint

The key point: as far as Edge is concerned, it is talking to the API’s real hostname. Same hostname means the TLS SNI and the certificate match, so there’s no certificate error.

Prerequisites

If you already RDP to the bastion through SSM and can open the page in the bastion’s browser, you have almost everything.

Already in place from SSM-based RDP

  • AWS CLI and the Session Manager plugin on the local PC
  • AmazonSSMManagedInstanceCore on the bastion’s IAM role. A minimal Session Manager policy also works
  • A network path from the bastion to SSM

Already in place if the page opens on the bastion

  • An execute-api VPC endpoint with private DNS enabled
  • The VPC endpoint’s security group allows TCP 443 from the bastion
  • The API’s resource policy allows calls from that VPC endpoint

A resource policy looks something like this (vpce-xxxxxxxx is the VPC endpoint ID). It denies calls from anything but that endpoint.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Deny",
      "Principal": "*",
      "Action": "execute-api:Invoke",
      "Resource": "execute-api:/*",
      "Condition": {
        "StringNotEquals": {
          "aws:SourceVpce": "vpce-xxxxxxxx"
        }
      }
    },
    {
      "Effect": "Allow",
      "Principal": "*",
      "Action": "execute-api:Invoke",
      "Resource": "execute-api:/*"
    }
  ]
}

Things to check

  • SSM Agent version: AWS-StartPortForwardingSessionToRemoteHost, used here, needs SSM Agent 3.1.1374.0 or newer on the bastion. For an older agent, run the Run Command document AWS-UpdateSSMAgent
  • Your IAM policy: if you restrict which documents ssm:StartSession can use, allow AWS-StartPortForwardingSessionToRemoteHost (see below)

Step 1: start port forwarding

First the manual version (the batch file comes later). Run this in PowerShell, replacing abc123 with your API ID and i-xxxxxxxx with the bastion’s instance ID.

$h = "abc123.execute-api.ap-northeast-1.amazonaws.com"
aws ssm start-session --target i-xxxxxxxx --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters "host=$h,portNumber=443,localPortNumber=443" --profile aws-dev --region ap-northeast-1
  • Use the AWS-StartPortForwardingSessionToRemoteHost document. AWS-StartPortForwardingSession, used for RDP, forwards to a port on the bastion itself; this one forwards to a host beyond the bastion
  • You can put the API hostname directly in host. It’s resolved on the bastion, where private DNS points it to the VPC endpoint’s IP addresses
  • --parameters uses key=value instead of JSON. Passing JSON from PowerShell means fighting quote escaping
  • The local port is 443 so the URL stays the usual one, without a port number. Listening on 443 on Windows doesn’t need admin rights; it worked on my non-admin company laptop
  • --profile aws-dev and --region are from my environment; use your own

When you see Waiting for connections..., it’s ready. Closing this PowerShell window closes the tunnel, so leave it open while you use the page.

Step 2: create a dedicated Edge shortcut

Right-click the desktop, choose New → Shortcut, and paste this line as the location. Give it a clear name such as “Edge for API Gateway”.

"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --user-data-dir="%LOCALAPPDATA%\edge-apigw" --no-first-run --no-default-browser-check --host-resolver-rules="MAP abc123.execute-api.ap-northeast-1.amazonaws.com 127.0.0.1"
  • --host-resolver-rules: makes this Edge instance alone resolve the API hostname to 127.0.0.1. It replaces the hosts file without touching OS-wide settings, so no admin rights are needed
  • --user-data-dir: starts Edge with a separate profile. If Edge is already running, a new launch is handed over to the existing Edge and the launch options are ignored. A separate profile makes the options work even with your normal Edge open, and leaves your normal Edge alone
  • --no-first-run and --no-default-browser-check: skip the first-run setup and the “make Edge your default browser” prompt for the new profile

Google Chrome is also Chromium-based, so the same options on chrome.exe should work too (I only tested Edge).

Step 3: open the usual URL in that Edge

Launch Edge from the shortcut and open the usual URL, for example https://abc123.execute-api.ap-northeast-1.amazonaws.com/prod/.

The same page I used to see over RDP opened in my local Edge without a certificate error. When you’re done, close Edge and the PowerShell window from step 1. Note that the dedicated profile starts empty: no favorites, no saved sign-ins.

One-click batch file

Typing the command and opening the shortcut every time is still a chore, so I use a batch file that does it all. Edit the settings block at the top for your environment. Feel free to use it.

@echo off
rem ============================================================
rem  Open a private API Gateway page (SSM port forwarding + Edge)
rem  Double-click to open the tunnel and the page in a dedicated Edge.
rem  Closing Edge closes the tunnel too.
rem ============================================================
setlocal

rem ===== Settings: change these for your environment =====
set "API_HOST=abc123.execute-api.ap-northeast-1.amazonaws.com"
set "URL=https://%API_HOST%/prod/"
set "TARGET=i-xxxxxxxx"
set "REGION=ap-northeast-1"
set "PROFILE=aws-dev"
set "EDGE=C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
set "EDGE_DATA=%LOCALAPPDATA%\edge-apigw"
rem ===================================================

rem --- Tunnel already up? Then only start Edge ---
call :is_listening
if not errorlevel 1 (
    echo Local port 443 is already listening. Opening Edge only.
    call :open_edge
    exit /b 0
)

rem --- Check required tools ---
where aws >nul 2>&1
if errorlevel 1 (
    echo [ERROR] AWS CLI v2 not found. Install it and run this again.
    pause
    exit /b 1
)
where session-manager-plugin >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Session Manager plugin not found. Install it and run this again.
    pause
    exit /b 1
)

rem --- Check AWS credentials; if valid, go straight to connecting ---
echo Checking AWS credentials...
aws sts get-caller-identity --profile %PROFILE% --region %REGION% >nul 2>&1
if not errorlevel 1 goto :auth_ok

echo Sign in to the AWS Management Console in the browser that opens.
aws login --profile %PROFILE% --region %REGION%
aws sts get-caller-identity --profile %PROFILE% --region %REGION% >nul 2>&1
if errorlevel 1 (
    echo [ERROR] AWS sign-in failed.
    pause
    exit /b 1
)
:auth_ok

rem --- Start port forwarding in a minimized window ---
echo Starting SSM port forwarding...
start "SSM-TUNNEL" /min aws ssm start-session --profile %PROFILE% --region %REGION% --target %TARGET% --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters "host=%API_HOST%,portNumber=443,localPortNumber=443"

rem --- Wait up to 30 seconds for the port to start listening ---
for /l %%i in (1,1,30) do (
    call :is_listening && goto :ready
    timeout /t 1 /nobreak >nul
)
echo [ERROR] Port forwarding did not start.
echo Check the messages in the minimized "SSM-TUNNEL" window.
pause
exit /b 1

:ready
echo Connected. Opening Edge. Closing Edge also closes the tunnel.
call :open_edge wait

rem --- After Edge closes, end the process listening on local 443 to close the tunnel ---
echo Closing the tunnel...
powershell -NoProfile -Command "Get-NetTCPConnection -LocalAddress 127.0.0.1 -LocalPort 443 -State Listen -ErrorAction SilentlyContinue | ForEach-Object { Stop-Process -Id $_.OwningProcess -Force }"
exit /b 0

rem ============================================================
:is_listening
powershell -NoProfile -Command "if (Get-NetTCPConnection -LocalAddress 127.0.0.1 -LocalPort 443 -State Listen -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }"
exit /b %errorlevel%

:open_edge
if "%~1"=="wait" (set "WAITOPT=/wait") else (set "WAITOPT=")
start "" %WAITOPT% "%EDGE%" --user-data-dir="%EDGE_DATA%" --no-first-run --no-default-browser-check --host-resolver-rules="MAP %API_HOST% 127.0.0.1" "%URL%"
exit /b 0

When you double-click it:

  1. If the tunnel already exists, it just opens Edge: when something is listening on local 127.0.0.1:443, it opens Edge and exits without a new tunnel
  2. Checks the tools: uses where to confirm AWS CLI and the Session Manager plugin are installed
  3. Checks credentials: if aws sts get-caller-identity fails, it runs aws login; sign in in the browser and it continues
  4. Starts port forwarding: in a minimized window, then waits up to 30 seconds for the port to open
  5. Opens Edge: the dedicated Edge with the launch options, and start /wait waits until Edge is closed
  6. Closes the tunnel: once Edge closes, it ends the process listening on local port 443 (the Session Manager plugin)

You could close the tunnel by killing aws.exe processes by instance ID, but that might also take down an RDP tunnel to the same bastion. So it targets only the process listening on port 443.

Why this approach

I compared several options before settling on this one.

Approach Result Why
RDP into the bastion and use its browser What I did before Works, but RDP every time is tedious
Port forwarding + https://localhost:8443 Rejected The certificate hostname doesn’t match, so you get an error. curl with -k plus an x-apigw-api-id header is fine for a quick check
Port forwarding + curl --resolve Good for testing SNI and certificate match, but it’s not a browser
Port forwarding + editing the hosts file Rejected Needs admin rights. Forget to revert it and the URL won’t open without the tunnel
Port forwarding + Edge launch option Chosen No admin rights, no certificate error, nothing left behind once closed

If you only need to check API responses, curl --resolve is the easy route. For a tunnel listening on local port 8443:

curl --resolve abc123.execute-api.ap-northeast-1.amazonaws.com:8443:127.0.0.1 https://abc123.execute-api.ap-northeast-1.amazonaws.com:8443/prod/hello

IAM policy for the person connecting

If you restrict Session Manager with a custom policy, include the AWS-StartPortForwardingSessionToRemoteHost document in the ssm:StartSession resources. For example (adjust account ID, region, instance ID):

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "StartPortForwardingToRemoteHost",
      "Effect": "Allow",
      "Action": "ssm:StartSession",
      "Resource": [
        "arn:aws:ec2:ap-northeast-1:123456789012:instance/i-xxxxxxxx",
        "arn:aws:ssm:ap-northeast-1::document/AWS-StartPortForwardingSessionToRemoteHost"
      ],
      "Condition": {
        "BoolIfExists": {
          "ssm:SessionDocumentAccessCheck": "true"
        }
      }
    },
    {
      "Sid": "ManageOwnSessions",
      "Effect": "Allow",
      "Action": [
        "ssm:TerminateSession",
        "ssm:ResumeSession",
        "ssmmessages:OpenDataChannel"
      ],
      "Resource": "arn:aws:ssm:*:*:session/${aws:username}-*"
    }
  ]
}

If the same user also RDPs to the bastion, add arn:aws:ssm:ap-northeast-1::document/AWS-StartPortForwardingSession to the first Resource list. Each item is explained in my earlier post.

If you sign in with aws login on a powerful account, the same idea as the “no credentials left on the PC” version in that post applies: credentials are only needed at the moment the session starts, so you can aws logout once the tunnel is up.

Gotchas and troubleshooting

Parts of the page don’t load

The tunnel covers only the host you specified. If the page loads JavaScript, images or data from another host (another API, a private S3 bucket and so on), those parts won’t show. On the bastion’s browser, press F12 and check the Network tab to see which hosts the page talks to. Add a tunnel and a --host-resolver-rules entry per host if needed.

start-session fails

  • AccessDenied: your IAM policy doesn’t allow AWS-StartPortForwardingSessionToRemoteHost
  • An error saying the document can’t be used: the bastion’s SSM Agent is too old
  • Port in use: another program is using local port 443. Change localPortNumber, but then the URL needs the port number

The tunnel is up but the page won’t open

RDP to the bastion and open the same URL there to narrow it down:

  • Name resolution error: private DNS on the VPC endpoint is disabled, or there is no endpoint
  • Timeout: the VPC endpoint’s security group is blocking it
  • 403: the API’s resource policy is denying it

Without private DNS this gets awkward

If private DNS on the VPC endpoint is disabled, you need the endpoint-specific hostname (like vpce-xxxxxxxx-xxxxxxxx.execute-api.ap-northeast-1.vpce.amazonaws.com) in host, plus a Host or x-apigw-api-id header to pick the API. That’s hard to do in a browser, so this method assumes private DNS is enabled.

The launch options have no effect

Check that Edge isn’t starting with your normal profile (did you leave out --user-data-dir?). If it still doesn’t work, company policy may be restricting Edge launch options.

Summary

No more RDP just to look at one page: double-click the batch file and the page opens in my local Edge at its usual URL. No hosts file means no admin rights, and closing Edge closes the tunnel and leaves nothing behind.


References: Start a session - AWS Systems Manager, Private REST APIs in API Gateway - Amazon API Gateway

Comments

Copied title and URL