Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to start and stop ec2 instances using php aws sdk

pretty new with the aws sdk, looking to start. i've installed the sdk and everything but how do I start the ec2 instances using the php sdk? Some code samples would really be useful.

like image 814
Patrick Avatar asked Jul 03 '12 09:07

Patrick


1 Answers

Here is a basic example of starting a machine from a defined AMI:

$image_id  = 'ami-3d4ff254'; //Ubuntu 12.04
$min       = 1;              //the minimum number of instances to start
$max       = 1;              //the maximum number of instances to start
$options   = array(
    'SecurityGroupId' => 'default',  //replace with your security group id
    'InstanceType'    => 't1.micro',
    'KeyName'         => 'keypair',  //the name of your keypair for auth
    'InstanceInitiatedShutdownBehavior' => 'terminate' //terminate on shutdown
);

require_once('AWSSDKforPHP/sdk.class.php');

$ec2 = new AmazonEC2();

$response = $ec2->run_instances($image_id, $min, $max, $options);

if(!$response->isOK()){
    echo "Start failed\n";
}

This is assuming you have your AWS credentials setup properly ... Hopefully this gets you pointed in the right direction ...

like image 80
keithhatfield Avatar answered Sep 22 '22 21:09

keithhatfield