2

I'm starting a session using AWSSimpleSystemsManagementAsync as follows:

Map<String, List<String>> parameters = new HashMap<>();
parameters.put("portNumber", Arrays.asList("80"));
parameters.put("localPortNumber", Arrays.asList("8080"));

StartSessionResult result =
    getSsmClient()
        .startSession(
            new StartSessionRequest()
                .withTarget(sb.toString())
                .withDocumentName("AWS-StartPortForwardingSession")
                .withParameters(parameters));

sessionId = result.getSessionId();

This seems to work and mimics what I can do manually:

aws ssm start-session --target "Your Instance ID" --document-name AWS-StartPortForwardingSession --parameters "portNumber"=["80"],"localPortNumber"=["8080"]

When I run manually I do open my browser at localhost:8080 to interact with my application as I need to but I'm struggling to do this type of thing via the SDK as opening the browser after starting the session doesn't seem to work as it does manually.

Hopefully, I'm just missing something.

javydreamercsw
  • 5,363
  • 13
  • 61
  • 106

1 Answers1

1

Assuming you had already installed the Session Manager Plugin as per https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html

The next step would be attaching it to your newly created session by spawning a new session-manager-plugin process like this.

...
StartSessionResult result = ssmClient.startSession(ssRequest);

StringBuilder responseJson = new StringBuilder("{");
responseJson.append("\"SessionId\":");
responseJson.append("\"");
responseJson.append(result.getSessionId());
responseJson.append("\"");
responseJson.append(",\"TokenValue\":");
responseJson.append("\"");
responseJson.append(result.getTokenValue());
responseJson.append("\"");
responseJson.append(",\"StreamUrl\":");
responseJson.append("\"");
responseJson.append(result.getStreamUrl());
responseJson.append("\"");
responseJson.append("}");

List<String> arguments = new ArrayList<>();
arguments.add("session-manager-plugin");
arguments.add(responseJson.toString());
arguments.add(AWS_REGION); // Whatever region you use
arguments.add("StartSession");
arguments.add(AWS_PROFILE_NAME); // Whatever profile you use
arguments.add("{\"Target\":\"i-xxxx\",\"DocumentName\":\"AWS-StartPortForwardingSession\",\"Parameters\":{\"portNumber\":[\"xx\"],\"localPortNumber\":[\"yyyy\"]}}");
arguments.add("https://ssm.us-east-1.amazonaws.com/"); // SSM regional endpoint

try {
    ProcessBuilder pb = new ProcessBuilder(arguments.toArray(new String[] {}));
    pb.redirectErrorStream(true);
    Process p = pb.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while (true) {
        line = r.readLine();
        if (line == null) { break; }
        System.out.println(line);
    }
} catch (IOException e) {
    e.printStackTrace();
}

The fragment should yield the following in your console

Starting session with SessionId: aws-sdk-java-zzzz
Port yyyy opened for sessionId aws-sdk-java-zzzz.
Slavek Tecl
  • 244
  • 2
  • 5
  • This works, however the original post had this solution also, and the question was intended for doing the same from java code. – plajko May 18 '23 at 09:22