gpt4 book ai didi

amazon-web-services - 如何使用 PHP 在 AWS 上启动任务 ECS

转载 作者:行者123 更新时间:2023-12-02 14:36:29 25 4
gpt4 key购买 nike

我尝试使用 PHP SDK 在 ECS(Ec2 容器服务)上启动我的“TaskDefinition”。

  1. 我创建了任务定义。
  2. 我创建了集群。
  3. 我创建了该服务。

我以为下一步是“registerContainerInstance”,但是当我调用此方法时,出现错误:

[Aws\Ecs\Exception\EcsException]
Error executing "RegisterContainerInstance" on "https://ecs.eu-west-1.amazonaws.com"; AWS HTTP error: Client error: 400 ClientException (client): An identity document was provided, but not valid. - {" __type":"ClientException","message":"An identity document was provided, but not valid."}

这是因为我没有发送“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)
{
//
}
}

最佳答案

我终于找到了解决问题的方法:

首先,您需要使用“registerTaskDefinition”创建任务:

$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";

文件 config/configEC2basic.json :

{
"containerDefinitions": [{
"command": [],
"cpu": 1,
"entryPoint": ["\/usr\/sbin\/apache2ctl -D FOREGROUND"],
"environment": [],
"essential": true,
"image": "usernamedockerhub\/dockercontainer",
"links": [],
"memory": 500,
"mountPoints": [],
"name": "nameTask",
"portMappings": [{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}],
"volumesFrom": []
}],
"family": "familyTask",
"volumes": []
}

然后,您需要创建集群:

$clusterName = $this->ecsClient->createCluster([
'clusterName' => 'test',
]);

然后,服务:

$result = $this->ecsClient->createService([
'desiredCount' => 1,
'serviceName' => 'serviceName',
'taskDefinition' => 'taskName',
'cluster' => 'test'
]);

之后,您必须启动 EC2 实例:

$result = $this->ec2Client->runInstances([
'ImageId' => 'ami-2aaef35d',
'MinCount' => 1,
'MaxCount' => 1,
'InstanceType' => 't2.micro',
'UserData' => base64_encode("#!/bin/bash \r\n echo ECS_CLUSTER=test >> /etc/ecs/ecs.config"),
'IamInstanceProfile' => [
'Arn' => 'ARNEC2'
],

]);

当您的 EC2 准备就绪时,您可以开始您的任务:

$result = $this->ecsClient->runTask([
'taskDefinition' => 'taskName',
'cluster' => 'test'
]);

关于amazon-web-services - 如何使用 PHP 在 AWS 上启动任务 ECS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32692245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com