Today I'll show you how to use API in order to create virtual server in the cloud directly from your code.
We're going write a script in PHP language and use Amazon SDK API to connect with e24cloud.com, which is the biggest and most modern hosting provider in Poland.
First we need to install Amazon SDK http://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/installation.html
One important note:
We can use the latest version of amazon SDK but we also need to do one trick.
We'll use signatureV2 instead ov Signaturev4 as e24cloud doesn't support SitnatureV4 yet. You can download it here: https://github.com/aws/aws-sdk-php-v3-bridge
We need to download SDK and the bridge which allows us to use Signaturev2 to the same folder, so that require instruction work seamlessly in the form that I provided in the example below.
Then go to your admin panel in e24cloud.com to find two values: a key and a secret. In the following code you need to paste those 2 values. I used 2 placeholders which you have to change: KEY and SECRET.
This is the script with a few functionalities:
- connecting to e24cloud.com
- creating new instance of virtual machine
- showing list of all machines
- stopping virtual machine
- deleting virtual machine
In most cases you won't need all of those functionalities in your code so you can choose only those lines that you need.
");
$client = new \Aws\Ec2\Ec2Client([
'version' => '2015-04-15',
"credentials" => [
"key" => "KEY",
"secret" => "SECRET"
],
'region' => 'eu-poland-1warszawa',
'endpoint_provider' => function (array $params) {
return ['endpoint' => 'https://'.$params['region'].'.api.e24cloud.com'];
},
'signature_provider' => function ($version, $service, $region) {
return new \Aws\Signature\SignatureV2($region, $service);
},
]);
echo(" Connection succeeded! ");
echo(" creating new instance... ");
$result = $client->runInstances(array(
'ImageId' => 'ami-000006d8',
'MinCount' => 1,
'MaxCount' => 1,
'InstanceType' => 'm1.xlarge',
'Placement' => [
'AvailabilityZone' => 'eu-poland-1warszawa1',
]
));
echo(" stopping VM instance... ");
$client->stopInstances(array(
'InstanceIds' => array('67f0c6bc-03f0-45a9-9a13-d49884eccbb4')
));
echo(" Deleting VM instance... ");
$client->terminateInstances(array(
'InstanceIds' => array('67f0c6bc-03f0-45a9-9a13-d49884eccbb4')
));
echo(" Showing all instances... ");
$result = $client->DescribeInstances();
foreach($result['Reservations'] as $reservation) {
foreach($reservation['Instances'] as $instance) {
echo 'id: '.$instance['InstanceId'].', type: '.$instance['InstanceType'].', State: '.$instance['State']['Name'].', PublicIp: '.$instance['PublicIpAddress'], PHP_EOL;
}
}
I think the code is straightforward so everyone with basic programming skills in any language can understand it.
Let's focus on main benefits of using e24cloud API. Imagin that 20 new hires joins your company development department. Then your boss asks you to create 20 instances of virtual machine - one machine for each new employee. You get the specification of operating system, demanded processor power, physical memory, and drive size that each machine needs. You say to your boss "OK, it's one day work to create all those instances manually. It will be boring and error prone and need double checking but I can do it. No problem!" I agree that you can do it by hand. But next month your company acquires a startup with 200 employees (such things happen). Then your boss comes to you again and says: "Hey, we need 200 instances for tomorrow, it would be great if you can create them." And what now? It's obvious that you can't crteate such amount of virtual machines in one day. You need at least 10 days. And this is where e24cloud API comes into play. As a software engineer you can and you should to automate all repetitive tasks. API lets you create all 200 instances in 5 minutes. You've just saved 10 days of work! Moreover you'r ready for next, maybe even bigger acquisition.
Yes, it sounds impressive but it's only a single example when you can take advantage of your programming skills and e24cloud.com API.
I hope this post helped you to understand the benefits of cloud computing platforms and how you can use API to automate work.
We're going write a script in PHP language and use Amazon SDK API to connect with e24cloud.com, which is the biggest and most modern hosting provider in Poland.
First we need to install Amazon SDK http://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/installation.html
One important note:
We can use the latest version of amazon SDK but we also need to do one trick.
We'll use signatureV2 instead ov Signaturev4 as e24cloud doesn't support SitnatureV4 yet. You can download it here: https://github.com/aws/aws-sdk-php-v3-bridge
We need to download SDK and the bridge which allows us to use Signaturev2 to the same folder, so that require instruction work seamlessly in the form that I provided in the example below.
Then go to your admin panel in e24cloud.com to find two values: a key and a secret. In the following code you need to paste those 2 values. I used 2 placeholders which you have to change: KEY and SECRET.
This is the script with a few functionalities:
- connecting to e24cloud.com
- creating new instance of virtual machine
- showing list of all machines
- stopping virtual machine
- deleting virtual machine
In most cases you won't need all of those functionalities in your code so you can choose only those lines that you need.
");
$client = new \Aws\Ec2\Ec2Client([
'version' => '2015-04-15',
"credentials" => [
"key" => "KEY",
"secret" => "SECRET"
],
'region' => 'eu-poland-1warszawa',
'endpoint_provider' => function (array $params) {
return ['endpoint' => 'https://'.$params['region'].'.api.e24cloud.com'];
},
'signature_provider' => function ($version, $service, $region) {
return new \Aws\Signature\SignatureV2($region, $service);
},
]);
echo(" Connection succeeded! ");
echo(" creating new instance... ");
$result = $client->runInstances(array(
'ImageId' => 'ami-000006d8',
'MinCount' => 1,
'MaxCount' => 1,
'InstanceType' => 'm1.xlarge',
'Placement' => [
'AvailabilityZone' => 'eu-poland-1warszawa1',
]
));
echo(" stopping VM instance... ");
$client->stopInstances(array(
'InstanceIds' => array('67f0c6bc-03f0-45a9-9a13-d49884eccbb4')
));
echo(" Deleting VM instance... ");
$client->terminateInstances(array(
'InstanceIds' => array('67f0c6bc-03f0-45a9-9a13-d49884eccbb4')
));
echo(" Showing all instances... ");
$result = $client->DescribeInstances();
foreach($result['Reservations'] as $reservation) {
foreach($reservation['Instances'] as $instance) {
echo 'id: '.$instance['InstanceId'].', type: '.$instance['InstanceType'].', State: '.$instance['State']['Name'].', PublicIp: '.$instance['PublicIpAddress'], PHP_EOL;
}
}
I think the code is straightforward so everyone with basic programming skills in any language can understand it.
Let's focus on main benefits of using e24cloud API. Imagin that 20 new hires joins your company development department. Then your boss asks you to create 20 instances of virtual machine - one machine for each new employee. You get the specification of operating system, demanded processor power, physical memory, and drive size that each machine needs. You say to your boss "OK, it's one day work to create all those instances manually. It will be boring and error prone and need double checking but I can do it. No problem!" I agree that you can do it by hand. But next month your company acquires a startup with 200 employees (such things happen). Then your boss comes to you again and says: "Hey, we need 200 instances for tomorrow, it would be great if you can create them." And what now? It's obvious that you can't crteate such amount of virtual machines in one day. You need at least 10 days. And this is where e24cloud API comes into play. As a software engineer you can and you should to automate all repetitive tasks. API lets you create all 200 instances in 5 minutes. You've just saved 10 days of work! Moreover you'r ready for next, maybe even bigger acquisition.
Yes, it sounds impressive but it's only a single example when you can take advantage of your programming skills and e24cloud.com API.
I hope this post helped you to understand the benefits of cloud computing platforms and how you can use API to automate work.
Komentarze
Prześlij komentarz