You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP SDK在AWS EC2实例执行命令无效果的问题排查

使用AWS PHP SDK在EC2实例执行命令无效果的问题排查

我尝试用AWS PHP SDK在EC2实例上执行任意命令,但命令返回成功后,实例上却没有预期效果。操作流程如下:

  1. 执行命令的PHP代码:
return $this->awsSdk
            ->createSsm()
            ->sendCommand([
                'DocumentName' => 'AWS-RunShellScript',
                'Targets' => [
                    [
                        'Key' => 'InstanceIds',
                        'Values' => [$this->getInstance($instance)] // 示例返回值:i-012345345h30
                    ]
                ],
                'Commands' => $commands, // 测试用命令数组:['touch testing.txt']
                'Output' => 'text'
            ])->toArray();

该命令返回结果:

array:2 [
  "Command" => array:26 [
    "CommandId" => "MY_COMMAND_ID"
    "DocumentName" => "AWS-RunShellScript"
    "DocumentVersion" => "$DEFAULT"
    "Comment" => ""
    "ExpiresAfter" => Aws\Api\DateTimeResult @1704816674^ {#910
      date: 2024-01-09 16:11:14.150 UTC (+00:00)
    }
    "Parameters" => []
    "InstanceIds" => []
    "Targets" => array:1 [
      0 => array:2 [
        "Key" => "InstanceIds"
        "Values" => array:1 [
          0 => "MY_INSTANCE_ID"
        ]
      ]
    ]
    "RequestedDateTime" => Aws\Api\DateTimeResult @1704809474^ {#913
      date: 2024-01-09 14:11:14.150 UTC (+00:00)
    }
    "Status" => "Pending"
    "StatusDetails" => "Pending"
    "OutputS3Region" => "eu-west-1"
    "OutputS3BucketName" => ""
    "OutputS3KeyPrefix" => ""
    "MaxConcurrency" => "50"
    "MaxErrors" => "0"
    "TargetCount" => 0
    "CompletedCount" => 0
    "ErrorCount" => 0
    "DeliveryTimedOutCount" => 0
    "ServiceRole" => ""
    "NotificationConfig" => array:3 [
      "NotificationArn" => ""
      "NotificationEvents" => []
      "NotificationType" => ""
    ]
    "CloudWatchOutputConfig" => array:2 [
      "CloudWatchLogGroupName" => ""
      "CloudWatchOutputEnabled" => false
    ]
    "TimeoutSeconds" => 3600
    "AlarmConfiguration" => array:2 [
      "IgnorePollAlarmFailure" => false
      "Alarms" => []
    ]
    "TriggeredAlarms" => []
  ]
  "@metadata" => array:4 [
    "statusCode" => 200
    "effectiveUri" => "https://ssm.eu-west-1.amazonaws.com"
    "headers" => array:6 [
      "server" => "Server"
      "date" => "Tue, 09 Jan 2024 14:11:14 GMT"
      "content-type" => "application/x-amz-json-1.1"
      "content-length" => "903"
      "connection" => "keep-alive"
      "x-amzn-requestid" => "SOME_ID"
    ]
    "transferStats" => array:1 [
      "http" => array:1 [
        0 => []
      ]
    ]
  ]
] // app/Console/Commands/GenerateSslCertificate.php:45
  1. 调用获取命令结果的PHP代码:
return $this->awsSdk
            ->createSsm()
            ->getCommandInvocation([
                'CommandId' => $id,
                'InstanceId' => $this->getInstance($instance),
            ]);

返回结果:

Aws\Result^ {#892
  -data: array:18 [
    "CommandId" => "MY_COMMAND_ID"
    "InstanceId" => "MY_INSTANCE_ID"
    "Comment" => ""
    "DocumentName" => "AWS-RunShellScript"
    "DocumentVersion" => "$DEFAULT"
    "PluginName" => "aws:runShellScript"
    "ResponseCode" => 0
    "ExecutionStartDateTime" => "2024-01-09T14:11:14.371Z"
    "ExecutionElapsedTime" => "PT0.017S"
    "ExecutionEndDateTime" => "2024-01-09T14:11:14.371Z"
    "Status" => "Success"
    "StatusDetails" => "Success"
    "StandardOutputContent" => ""
    "StandardOutputUrl" => ""
    "StandardErrorContent" => ""
    "StandardErrorUrl" => ""
    "CloudWatchOutputConfig" => array:2 [
      "CloudWatchLogGroupName" => ""
      "CloudWatchOutputEnabled" => false
    ]
    "@metadata" => array:4 [
      "statusCode" => 200
      "effectiveUri" => "https://ssm.eu-west-1.amazonaws.com"
      "headers" => array:6 [
        "server" => "Server"
        "date" => "Tue, 09 Jan 2024 14:12:44 GMT"
        "content-type" => "application/x-amz-json-1.1"
        "content-length" => "582"
        "connection" => "keep-alive"
        "x-amzn-requestid" => "SOME_ID"
      ]
      "transferStats" => array:1 [
        "http" => array:1 [
          0 => []
        ]
      ]
    ]
  ]
  -monitoringEvents: []
}

但登录实例后,找不到创建的testing.txt文件,求解决办法。


*** 更新 ***

排查后重构了命令:

return $this->awsSdk
            ->createSsm()
            ->sendCommand([
                'InstanceIds' => ['MY_INSTANCE_ID'],
                'DocumentName' => 'AWS-RunShellScript',
                'Comment' => 'Run Script',
                'Parameters' => [
                    'Commands' => ['pwd'],
                ],
                'Output' => 'text'
            ])->toArray();

注意:使用AWS-RunShellScript文档的等效CLI命令可以正常执行:

aws ssm send-command \                                                                                                   
    --instance-ids "MY_INSTANCE_ID" \
    --document-name "AWS-RunShellScript" \
    --comment "IP config" \
    --parameters commands=ifconfig \
    --output text

内容的提问来源于stack exchange,提问作者Josh Bolton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 18:23:19