AWS Java SDK - 运行 在 EC2 实例上使用 SSM 的命令

AWS Java SDK - Running a command using SSM on EC2 instances

我在网上找不到这方面的任何示例,也找不到解释如何执行此操作的文档。基本上我有一个 Windows EC2 实例的列表,我需要在每个实例中 运行 quser 命令来检查有多少用户已登录。

可以使用 AWS Systems Manager 服务和 运行 使用 AWS-RunPowerShellScript 命令来执行此操作。我只找到了使用 AWS CLI 的例子,像这样:

aws ssm send-command --instance-ids "instance ID" --document-name "AWS-RunPowerShellScript" --comment "Get Users" --parameters commands=quser --output text

但是我如何使用 AWS Java SDK 1.11.x 完成此操作?

@Alexandre Krabbe 你问这个问题已经一年多了。所以不确定答案是否对您有帮助。但我最近也在尝试做同样的事情,这让我想到了这个悬而未决的问题。我最终解决了问题,并认为我的回答可以帮助其他面临同样问题的人。这是相同的代码片段:

public void runCommand() throws InterruptedException {
    //Command to be run
    String ssmCommand = "ls -l";
    Map<String, List<String>> params = new HashMap<String, List<String>>(){{
        put("commands", new ArrayList<String>(){{ add(ssmCommand); }});
    }};
    int timeoutInSecs = 5;
    //You can add multiple command ids separated by commas
    Target target = new Target().withKey("InstanceIds").withValues("instance-id");
    //Create ssm client.
    //The builder could be chosen as per your preferred way of authentication
    //use withRegion for specifying your region
    AWSSimpleSystemsManagement ssm = AWSSimpleSystemsManagementClientBuilder.standard().build();
    //Build a send command request
    SendCommandRequest commandRequest = new SendCommandRequest()
            .withTargets(target)
            .withDocumentName("AWS-RunShellScript")
            .withParameters(params);
    //The result has commandId which is used to track the execution further
    SendCommandResult commandResult = ssm.sendCommand(commandRequest);
    String commandId = commandResult.getCommand().getCommandId();
    //Loop until the invocation ends
    String status;
    do {
        ListCommandInvocationsRequest request = new ListCommandInvocationsRequest()
                .withCommandId(commandId)
                .withDetails(true);
        //You get one invocation per ec2 instance that you added to target
        //For just a single instance use get(0) else loop over the instanced
        CommandInvocation invocation = ssm.listCommandInvocations(request).getCommandInvocations().get(0);
        status = invocation.getStatus();
        if(status.equals("Success")) {
            //command output holds the output of running the command
            //eg. list of directories in case of ls
            String commandOutput = invocation.getCommandPlugins().get(0).getOutput();
            //Process the output
        }
        //Wait for a few seconds before you check the invocation status again
        try {
            TimeUnit.SECONDS.sleep(timeoutInSecs);
        } catch (InterruptedException e) {
            //Handle not being able to sleep
        }
    } while(status.equals("Pending") || status.equals("InProgress"));
    if(!status.equals("Success")) {
        //Command ended up in a failure
    }
}

在 SDK 1.11.x 中,像这样使用:

val waiter = ssmClient.waiters().commandExecuted()

waiter.run(WaiterParameters(GetCommandInvocationRequest()
    .withCommandId(commandId)
    .withInstanceId(instanceId)
))