我尝试使用 PHP SDK 在 ECS(Ec2 容器服务)上启动我的“TaskDefinition”。
- 我创建了任务定义。
- 我创建了集群。
- 我创建了服务。
我以为下一步是“registerContainerInstance”,但是当我调用此方法时,出现错误:
[Aws\Ecs\Exception\EcsException]在“ https://ecs.eu-west-1.amazonaws.com
” 上执行“RegisterContainerInstance”时出错;AWS HTTP 错误:客户端错误:400 ClientException(客户端):提供了身份文件,但无效。- {" __type":"ClientException","message":"提供了身份证件,但无效。"}
这是因为我不发送“instanceIdentityDocument”和“instanceIdentityDocumentSignature”。但是,我不知道如何获得这两个参数。
我之前应该手动启动 EC2 吗?
还有另一种我不知道的方法吗?
<?php
namespace App\Http\Controllers;
use Aws\Ecs\EcsClient;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
class ECSController extends Controller
{
protected $ecsClient;
function __construct() {
$config = Config::get('aws');
$this->ecsClient = new EcsClient([
'version' => 'latest',
'region' => $config['region'],
'credentials' => [
'key' => $config['credentials']['key'],
'secret' => $config['credentials']['secret'],
],
]);
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
/* $response = [
'photos' => []
];
return Response::json($response, $statusCode); */
echo "index\n";
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$file = file_get_contents('config/configEC2basic.json');
$data = json_decode($file, true /* associative arrays */);
$result = $this->ecsClient->registerTaskDefinition($data);
if ($result['taskDefinition']['status'] == "ACTIVE")
{
$taskName = $result['taskDefinition']['containerDefinitions'][0]['name'];
if ($result['taskDefinition']['revision'] == 1)
echo "Task : '".$taskName."' has been created\n";
else
echo "Task : '".$taskName."' has been updated\n";
}
else
echo "Error : The task is not active.\n";
$clusterName = $this->ecsClient->createCluster([
'clusterName' => 'kaemo',
]);
$result = $this->ecsClient->createService([
'desiredCount' => 1,
'serviceName' => 'kaedevAWS1',
'taskDefinition' => 'testkaeDEV4',
'cluster' => 'kaemo'
]);
}
public function start()
{
$result = $this->ecsClient->registerContainerInstance([
'cluster' => 'kae',
'totalResources' => [
[
'integerValue' => 1,
"longValue" => 0,
'name' => "CPU",
'type' => "INTEGER",
"doubleValue" => 0.0,
],
[
'integerValue' => 996,
"longValue" => 0,
'name' => "MEMORY",
'type' => "INTEGER",
"doubleValue" => 0.0,
],
[
'integerValue' => 0,
"longValue" => 0,
'name' => "PORTS",
'type' => "STRINGSET",
"doubleValue" => 0.0,
"stringSetValue" => [
"80",
"22"
]
]
]
]);
echo ">".print_r($result, true);
/* $result = $this->ecsClient->runTask([
'taskDefinition' => 'testkaemoDEV4',
'cluster' => 'kaemo'
]);
echo ">".print_r($result, true); */
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}