amazon web services - How to launch a task ECS on AWS with PHP -
i try launch "taskdefinition" on ecs (ec2 container service) php sdk.
- i created taskdefinition.
- i created cluster.
- i created service.
i thought next step "registercontainerinstance" when call method, have error :
[aws\ecs\exception\ecsexception]
error executing "registercontainerinstance" on "https://ecs.eu-west-1.amazonaws.com"; aws http error: client error: 400 clientexception (client): identity document provided, not valid. - {" __type":"clientexception","message":"an identity document provided, not valid."}
this because don't send "instanceidentitydocument" , "instanceidentitydocumentsignature". but, don't know how 2 parameters.
should launch ec2 manually before?
is there way do not know?
<?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 listing of resource. * * @return response */ public function index() { /* $response = [ 'photos' => [] ]; return response::json($response, $statuscode); */ echo "index\n"; } /** * show form creating 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 : task 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 newly created resource in storage. * * @param request $request * @return response */ public function store(request $request) { // } /** * display specified resource. * * @param int $id * @return response */ public function show($id) { // } /** * show form editing specified resource. * * @param int $id * @return response */ public function edit($id) { // } /** * update specified resource in storage. * * @param request $request * @param int $id * @return response */ public function update(request $request, $id) { // } /** * remove specified resource storage. * * @param int $id * @return response */ public function destroy($id) { // } }
i found solution problem :
first, need create task "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 : task not active.\n";
the file 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": [] }
then, need create cluster :
$clustername = $this->ecsclient->createcluster([ 'clustername' => 'test', ]);
then, service :
$result = $this->ecsclient->createservice([ 'desiredcount' => 1, 'servicename' => 'servicename', 'taskdefinition' => 'taskname', 'cluster' => 'test' ]);
after that, have start ec2 instance :
$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' ], ]);
when ec2 ready, can start task :
$result = $this->ecsclient->runtask([ 'taskdefinition' => 'taskname', 'cluster' => 'test' ]);
Comments
Post a Comment