Creating an Amazon EC2 instance using the PHP SDK

11 Jan

Recently I had to write a script in PHP to start up a EC2 instance and then return its hostname. I used the AWS PHP SDK for this.The AWS SDK in PHP was of great help. The documentation was enough, but I had to do some testing and debugging in order to get my script to completely work.

So without much ado, here is the PHP script to set up an instance and get its hostname, as well as output these as messages using the AWS PHP SDK.

	// Sleep time to allow EC2 instance to start up
	$sleeptime = 15;
	$username = "ec2-user";

	// For AWS PHP SDK
	putenv('HOME=/home/ec2-user/');
	require_once 'AWSSDKforPHP/sdk.class.php';

	// Get data from HTTP POST
	$ami = $_POST['amis'];
	$instancetype = $_POST['instancetype'];
	$keyname = $_POST['key'];
	$securitygroup = $_POST['securitygroups'];

	// Instantiate the AmazonEC2 class
	$ec2 = new AmazonEC2();

	// Boot an instance of the image
	$response = $ec2->run_instances($ami, 1, 1, array(
	    'KeyName' => $keyname,
	    'InstanceType' => $instancetype,
	    'SecurityGroupId' => $securitygroup,
	));
	if (!($response->isOK())) {
		echo "<p class='error'>ERROR! Could not create new instance!</p>";
		return;	
	}
	$instance = $response->body->instancesSet->item->instanceId;
	$message = "<p>Your instance has been successfully created.</p>";
	$message .= ("<p>Instance ID is: <b>$instance</b></p>");

	// Give instance some time to start up
	sleep ($sleeptime);

	// Get the hostname from a call to the DescribeImages operation.
	$response = $ec2->describe_instances(array(
		'Filter' => array(
        array('Name' => 'instance-id', 'Value' => "$instance"),
		)
	));
	if (!($response->isOK())) {
		echo "<p class='error'>ERROR! Could not retrieve hostname for instance!</p>";
		return;	
	}
	$hostname = $response->body->reservationSet->item->instancesSet->item->dnsName;

	// Output the message
	$message .= "<p>Your instance hostname is: <b>$hostname</b></p>";
	$message .= "<p>You can connect to your instance using this command:<br>" .
		"<b>ssh -i $keyname.pem $username@" . $hostname .  "</b></p>";

	echo $message;
Advertisements

3 Responses to “Creating an Amazon EC2 instance using the PHP SDK”

  1. Mihai August 27, 2012 at 12:06 pm #

    great tutorial! can you run this script locally?

    • sumanrs August 27, 2012 at 2:00 pm #

      I believe so, as long as you have Amazon’s PHP SDK installed and configured correctly.

  2. Josef February 18, 2013 at 6:53 am #

    This is not “Creating” an instance but “Starting” an instance. The SDK is more obscure with creating an instance than simply starting it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: