我想使用 PHP SDK 在我的 EC2 实例上运行任意命令,但是,当我运行命令时似乎没有发生任何事情。
我的工作流程如下:
return $this->awsSdk
->createSsm()
->sendCommand([
'DocumentName' => 'AWS-RunShellScript',
'Targets' => [
[
'Key' => 'InstanceIds',
'Values' => [$this->getInstance($instance)] //Returns a value such as i-012345345h30 as an example
]
],
'Commands' => $commands, //An array of commands, (For testing this is ['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
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
在查阅了 AWS 文档几个小时后,我发现了我的问题。
实例化SDK后:
$this->awsSdk = new Sdk([
'region' => 'eu-west-1',
'credentials' => [
'key' => 'YOUR_ACCESS_KEY',
'secret' => 'YOUR_SECRET_KEY'
]
]);
然后我创建了一个允许我发送命令的函数,如下所示:
public function sendCommands(string $instance, array $commands)
{
return $this->awsSdk
->createSsm()
->sendCommand([
'InstanceIds' => [$this->getInstance($instance)],
'DocumentName' => 'AWS-RunShellScript',
'Comment' => 'Run Script',
'Parameters' => [
'commands' => $commands,
],
'Output' => 'text'
])->toArray();
}
这里唯一的区别是命令中的小写
C
,这似乎解决了我的问题。但是,我认为最好包括我的整个解决方案。