Skip to content

Instantly share code, notes, and snippets.

@maimai-swap
Last active November 4, 2016 19:30
Show Gist options
  • Save maimai-swap/5945919 to your computer and use it in GitHub Desktop.
Save maimai-swap/5945919 to your computer and use it in GitHub Desktop.
aws sdk php2, ec2 describe and start and stop.
<?php
/**
* Created by JetBrains PhpStorm.
* User: ishimaru
* Date: 2013/07/05
* Time: 17:31
* To change this template use File | Settings | File Templates.
*/
require 'vendor/autoload.php';
/**
* 'access_keys/config.php'
* $access_token = array('key' => 'change-your-id',
* 'secret' => 'change-your-secret',
* 'region' => 'ap-northeast-1'
* );
*/
include 'access_keys/config.php';
$client = \Aws\Ec2\Ec2Client::factory($access_token);
$result = $client->describeInstances(
['Filters' => [
['Name' => "instance-id" , 'Values' => ["i-b27bfdb1"]]
// ['Name' => 'instance-type', 'Values' => ['m1.small']],
// ['Name' => 'tag-key', 'Values' => ['Stage']],
// ['Name' => 'tag-value', 'Values' => ['Development', 'Development-Tools']],
]
]
);
$reservations = $result['Reservations'];
foreach ($reservations as $reservation) {
$instances = $reservation['Instances'];
foreach ($instances as $instance) {
$instanceName = '';
$stage = '';
foreach ($instance['Tags'] as $tag) {
if ($tag['Key'] == 'Name') {
$instanceName = $tag['Value'];
}
if (@$tag['Key'] == 'Stage') {
$stage = $tag['Value'];
}
}
echo 'Instance Name: ' . $instanceName . PHP_EOL;
echo '---> Stage: ' . $stage . PHP_EOL;
echo '---> State: ' . $instance['State']['Name'] . PHP_EOL;
echo '---> Instance ID: ' . $instance['InstanceId'] . PHP_EOL;
echo '---> Image ID: ' . $instance['ImageId'] . PHP_EOL;
echo '---> Instance Type: ' . $instance['InstanceType'] . PHP_EOL;
echo '---> Security Group: ' . $instance['SecurityGroups'][0]['GroupName'] . PHP_EOL;
}
}
$target_instance_id = "i-b27bfdb1";
if ( false ) {
$return = $client->startInstances(
["InstanceIds" => [$target_instance_id]]
);
var_dump($return["StartingInstances"][0]["InstanceId"]);
var_dump($return["StartingInstances"][0]["CurrentState"]);
var_dump($return["StartingInstances"][0]["PreviousState"]);
}
if ( false ) {
$return = $client->stopInstances(
["InstanceIds" => [$target_instance_id]],
["Force" => false]
);
var_dump($return["StoppingInstances"][0]["InstanceId"]);
var_dump($return["StoppingInstances"][0]["CurrentState"]);
var_dump($return["StoppingInstances"][0]["PreviousState"]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment