Created
November 8, 2017 20:24
-
-
Save jasonbronson/315b38d9316c781ed15adff7cfd4f559 to your computer and use it in GitHub Desktop.
Amazon ec2 laravel launch image instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Libraries; | |
use Aws; | |
use Ec2Client; | |
use App\Models\ProjectsModel; | |
class AmazonEc2{ | |
protected $client; | |
protected $imageId; | |
protected $instanceType; | |
protected $publicIP; | |
protected $privateIP; | |
protected $tagDateStamp; | |
protected $terminationProtection; | |
protected $instanceId; | |
public function __construct(){ | |
$config = array( | |
'region' => 'us-east-1', | |
'version' => '2015-10-01', | |
'credentials' => array( | |
'key' => env('AMAZON_KEY'), | |
'secret' => env('AMAZON_SECRET') | |
), | |
'http' => array( | |
'verify' => false | |
) | |
); | |
$this->client = new Aws\Ec2\Ec2Client( $config ); | |
$this->setImageId('ami-d42b02be'); | |
$this->setInstanceType('t2.micro'); | |
$this->terminationProtection = array('i-7ed63fab', 'i-074dac7f614b9cec0'); | |
} | |
public function DestroyMachines($instances){ | |
//$instances = $result->search('Reservations[].Instances[].InstanceId'); | |
//remove protected instances | |
foreach($instances as $instance){ | |
//save certain amazon web servers from termination. | |
if(in_array($instance, $this->terminationProtection)){ | |
unset($instances[$instance]); | |
echo "Removing protected Instance ".$instance."\n"; | |
continue; | |
} | |
try{ | |
// Terminate instances | |
$result = $this->client->terminateInstances(array( | |
'InstanceIds' => array($instance) | |
)); | |
var_dump($result); | |
}catch(\Aws\Ec2\Exception\Ec2Exception $e){ | |
echo "Exception Thrown \n"; | |
} | |
} | |
return true; | |
} | |
public function createEc2($count = 1){ | |
$response = $this->client->runInstances( array( | |
'ImageId' => $this->getImageId(), // REQUIRED | |
'InstanceType' => $this->getInstanceType(), | |
'MaxCount' => $count, // REQUIRED | |
'MinCount' => 1, // REQUIRED | |
'KeyName' => 'mysshkeys', | |
'SecurityGroupIds' => ['launch-wizard-1'], | |
//'DryRun' => true | |
) ); | |
var_dump($response); | |
$instances = $response->get('Instances'); | |
$instanceId = $instances[0]['InstanceId']; | |
$this->setInstanceId($instanceId); | |
$this->tagDateStamp = "AUTO-".date("H:iA m-d-y"); | |
$createTagsResult = $this->client->createTags(array( | |
'Tags' => array( | |
array( | |
'Key' => 'Name', | |
'Value' => $this->tagDateStamp | |
) | |
), | |
'Resources' => array($instances[0]['InstanceId']) | |
)); | |
$this->client->waitUntil('InstanceRunning', ['InstanceIds' => array($instanceId)]); | |
$result = $this->client->describeInstances(array('InstanceIds' => array($instanceId) )); | |
$this->publicIP = $result['Reservations'][0]['Instances'][0]['PublicIpAddress']; | |
$this->privateIP = $result['Reservations'][0]['Instances'][0]['PrivateIpAddress']; | |
$projects = $this->projects = new ProjectsModel(0); | |
$projects->saveCrawlerSpawned($this); | |
//echo " Instance ID: $instanceId IPADDRESS: $privateIP PUBLICIP: $publicIP"; | |
return true; | |
} | |
public function setInstanceId($value){ | |
$this->instanceId = $value; | |
} | |
public function getInstanceId(){ | |
return $this->instanceId; | |
} | |
public function setImageId($value){ | |
$this->imageId = $value; | |
} | |
public function getImageId(){ | |
return $this->imageId; | |
} | |
public function setInstanceType($value){ | |
$this->instanceType = $value; | |
} | |
public function getInstanceType(){ | |
return $this->instanceType; | |
} | |
public function setTagDateStamp($value){ | |
$this->tagDateStamp = $value; | |
} | |
public function getTagDateStamp(){ | |
return $this->tagDateStamp; | |
} | |
public function setPublicIP($value){ | |
$this->publicIP = $value; | |
} | |
public function getPublicIP(){ | |
return $this->publicIP; | |
} | |
public function setPrivateIP($value){ | |
$this->privateIP = $value; | |
} | |
public function getPrivateIP(){ | |
return $this->privateIP; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment