public function startInstance($instanceId) { $ec2Client = new \Aws\Ec2\Ec2Client($this->config); fprintf(STDERR, "Stopping instance\n"); $ec2Client->stopInstances(array('Force' => true, 'InstanceIds' => array($instanceId))); $ec2Client->waitUntil('InstanceStopped', array('InstanceIds' => array($instanceId))); fprintf(STDERR, "Stopped\n"); $start = microtime(true); fprintf(STDERR, "Starting instance\n"); $ec2Client->startInstances(array('InstanceIds' => array($instanceId))); $ec2Client->waitUntil('InstanceRunning', array('InstanceIds' => array($instanceId))); fprintf(STDERR, "Started\n"); return array('timeElapsed' => microtime(true) - $start); }
include __DIR__ . '/vendor/autoload.php'; /** * Usage * * cli.php describe * cli.php create */ if (empty($argv[1])) { die('Empty cli command.'); } $configFile = __DIR__ . '/config.php'; if (!file_exists($configFile)) { die('Missing config file'); } $AWS_KEY = $AWS_SECRET = $AWS_VOLUME_ID = $AWS_VERSION = $AWS_REGION = $SNAPSHOT_DESCRIPTION = null; extract(include $configFile, EXTR_OVERWRITE); $clientConfig = ['version' => $AWS_VERSION, 'region' => $AWS_REGION, 'credentials' => ['key' => $AWS_KEY, 'secret' => $AWS_SECRET]]; $ec2 = new Aws\Ec2\Ec2Client($clientConfig); switch ($argv[1]) { case 'describe': $describeConfig = ['Filters' => [['Name' => 'volume-id', 'Values' => [$AWS_VOLUME_ID]]]]; var_export($ec2->describeSnapshots($describeConfig)->toArray()['Snapshots']); break; case 'create': $createConfig = ['DryRun' => false, 'VolumeId' => $AWS_VOLUME_ID, 'Description' => $SNAPSHOT_DESCRIPTION]; var_export($ec2->createSnapshot($createConfig)->toArray()); break; default: echo 'Invalid command'; break; }
<?php require $_SERVER["DOCUMENT_ROOT"] . "amazon/aws-autoloader.php"; require "credentials.php"; $ec2 = new Aws\Ec2\Ec2Client($credentials); $command = filter_input(INPUT_POST, "command", FILTER_SANITIZE_STRING); $ids = filter_input(INPUT_POST, "ids", FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY); $error = null; if ($command === "Start") { try { $result = $ec2->startInstances(["InstanceIds" => $ids]); } catch (Ec2Exception $e) { $error = $e->getMessage(); exit($error); } echo $result; } if ($command === "Stop") { try { $result = $ec2->stopInstances(["InstanceIds" => $ids]); } catch (Ec2Exception $e) { $error = $e->getMessage(); exit($error); } } #$ids = ["i-8992db20"]; try { $result = $ec2->describeInstances(["InstanceIds" => $ids]); } catch (Ec2Exception $e) { $error = $e->getMessage(); exit($error);
public function init($creds) { $ec2Client = new \Aws\Ec2\Ec2Client(['version' => 'latest', 'region' => 'us-east-1', 'credentials' => new \Aws\Credentials\Credentials($creds->getKey(), $creds->getSecret())]); $result = $ec2Client->describeInstances(['InstanceIds' => [$this->instanceID], 'DryRun' => false]); $instance = $result['Reservations'][0]['Instances'][0]; $this->launchTime = $instance['LaunchTime']; $this->keyName = $instance['KeyName']; return true; }
/** * Get the source snapshot for a given EBS volume id. * * @param string $volumeId Id of EBS volume to get the source snapshot of. * @return string Source snapshto id of the specified EBS volume. */ public function getEbsVolumeSourceSnapshot($volumeId) { if (!strlen($volumeId)) { throw new InvalidArgumentException(sprintf('Volume id passed to "%s" must be a non-zero length string', __FUNCTION__)); } $response = $this->ec2->describeVolumes(['VolumeIds' => [$volumeId]]); if (!count($response['Volumes']) || !$response['Volumes'][0]['SnapshotId']) { return null; } return $response['Volumes'][0]['SnapshotId']; }
/** * Return the latest EC2 image with the specified owner and description. * * @param string $ownerAlias Alias of the image owner. * @param string $description Description of the image. * @return string Latest image id with the specified owner and description. */ public function getLatestEc2ImageByOwnerAndDescription($ownerAlias, $description) { if (!strlen($ownerAlias)) { throw new InvalidArgumentException(sprintf('Owner alias passed to "%s" must be a non-zero length string', __FUNCTION__)); } if (!strlen($description)) { throw new InvalidArgumentException(sprintf('Description passed to "%s" must be a non-zero length string', __FUNCTION__)); } $response = $this->ec2->describeImages(['Filters' => [['Name' => 'owner-alias', 'Values' => [$ownerAlias]], ['Name' => 'description', 'Values' => [$description]]]]); return $this->getLatestEc2ImageFromResponse($response); }
function checkInstanceStatus($instanceInfo) { $keySecret = array('key' => $instanceInfo['key'], 'secret' => $instanceInfo['secret'], 'region' => $instanceInfo['instName']); $get_stat_info = array(); $client = Aws\Ec2\Ec2Client::factory($keySecret); $result = $client->describeInstances(array('InstanceIds' => array($instanceInfo['instance_id']))); $reservations = $result->toArray(); return $reservations['Reservations'][0]['Instances'][0]['State']['Name']; }
<?php require 'vendor/autoload.php'; # use Aws\Common\Aws; $tag = 'architecture'; $values = array('i386', 'x86_64'); $client = new Aws\Ec2\Ec2Client(array('region' => 'us-west-2', 'debug' => false, 'version' => 'latest', 'profile' => 'default')); $owner = "B.Gates"; $param = array('Filters' => array(array('Name' => $tag, 'Values' => $values), array('Name' => 'tag:Name', 'Values' => array('EU *')), array('Name' => 'tag:Owners', 'Values' => array($owner, $owner . ",*", "*," . $owner . ",*", "*," . $owner)))); $result = $client->describeInstances($param); foreach ($result['Reservations'] as $reservation) { foreach ($reservation['Instances'] as $instance) { foreach ($instance['Tags'] as $instanceTag) { $instanceName = ''; if ($instanceTag['Key'] == 'Name') { $instanceName = $instanceTag['Value']; } } print "Instance Name: " . $instanceName . "\n"; print "Instance ID: " . $instance['InstanceId'] . "\n"; print "----> State: " . $instance['State']['Name'] . "\n"; print "----> Instance Type: " . $instance['InstanceType'] . "\n"; foreach ($instance['NetworkInterfaces'] as $networkinterface) { print "----> Network InterfaceId: " . $networkinterface['NetworkInterfaceId'] . "\n"; print "----> Network Interface MAC Address: " . $networkinterface['macAddress'] . "\n"; $num_ipaddress = sizeof($networkinterface['PrivateIpAddresses']) . "\n"; for ($i = 0; $i < $num_ipaddress; $i++) { if ($i == 0) { print "----> Network Interface Primary Private Ip Address: "; } else { print "----> Instance Other Private Address: ";
}); $app->get('/api/getInstanceList', function () { global $instances; $response = array(); foreach ($instances as $eachInstName) { foreach ($eachInstName['instance_id'] as $eachInstanceId) { $instanceInfo = array('instName' => $eachInstName['instName'], 'instance_id' => $eachInstanceId, 'key' => $eachInstName['key'], 'secret' => $eachInstName['secret']); // $status = checkInstanceStatus($instanceInfo); // $instanceInfo['status'] = $status; $response[] = $instanceInfo; } } echo json_encode($response); }); $app->post('/api/startInstance', function () { global $input; $instanceInfo = $input; $keySecret = array('key' => $instanceInfo['key'], 'secret' => $instanceInfo['secret'], 'region' => $instanceInfo['instName']); $client = Aws\Ec2\Ec2Client::factory($keySecret); $result = $client->startInstances(array('InstanceIds' => array($instanceInfo['instance_id']))); echo json_encode($result->toArray()); }); $app->post('/api/stopInstance', function () { global $input; $instanceInfo = $input; $keySecret = array('key' => $instanceInfo['key'], 'secret' => $instanceInfo['secret'], 'region' => $instanceInfo['instName']); $client = Aws\Ec2\Ec2Client::factory($keySecret); $result = $client->stopInstances(array('InstanceIds' => array($instanceInfo['instance_id']))); echo json_encode($result->toArray()); }); $app->run();