Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Amazon Cloud Services and Zend Framework

5,794 views

Published on

Published in: Technology
  • Be the first to comment

Amazon Cloud Services and Zend Framework

  1. 1. Shahar Evron | Zend Technologies Amazon Cloud Services and Zend Framework
  2. 2. A few things about me... ● Shahar Evron (‫ֹן‬‫ו‬‫ְר‬‫ב‬ֶ‫ע‬ ‫ַר‬‫ח‬ָ‫ש‬) ● Working with PHP since 2002 ● In Zend since 2005 ● “Technical Product Manager” ● I'm not a programmer, I decide stuff ;) ● I get to play with cool technical sh!t ● My boss is in marketing (but I do not talk about it) ● I also try to contribute to Zend Framework
  3. 3. Agenda ● This is a code talk! ● A quick introduction to the AWS platform ● Showcase some of the Amazon services through ZF-based examples ● Amazon Simple Storage Service ● Amazon Elastic Compute Cloud ● Elastic Block Storage ● Elastic Load Balancing
  4. 4. Usual Zend Framework Mantras ● ZF is also a component library ...you all already know this right? ● Everything I'll show today can be used with any PHP 5.2+ code
  5. 5. Hype Aside, Why Cloud? ● Lets take a deep breath for a minute.. ● We're not all moving to the cloud ...right? ● It's not the solution for everything! ● It's not only a hosting platform ● It can be used at will
  6. 6. Amazon Cloud Services ● The biggest, most complete and best known public cloud platform today ● Amazon started externalizing (and selling) internal infrastructures ● EC2 public beta in 2006 ● Includes services for computing, file storage, data storage, queueing and more ● ...no free development offering
  7. 7. Common Initialization Code All code samples in this tutorial assume: // Have ZF in your include_path set_include_path('/path/to/zf/library'); // Load and register the ZF autoloader require_once 'Zend/Loader/Autoloader.php'; $loader = Zend_Loader_Autoloader::getInstance(); // Set AWS access keys define('AWS_ACCESS_KEY', 'YOURACCESSKEYID'); define('AWS_SECRET_KEY', '5om3R4Ndom53Cr3757uFf');
  8. 8. Amazon S3 ● “Simple Storage Service” ● Object (file, blob) storage ● Allows access control on files ● Simple HTTP REST interface ● Public objects can be accessed directly using any regular web browser ● Fault-tolerant ● Not to be confused with: EBS, CloudFront
  9. 9. Amazon S3 The Basics: /* Keys can also be set globally using the static Zend_Service_Amazon_S3::setKeys() method */ $s3 = new Zend_Service_Amazon_S3( AWS_ACCESS_KEY, AWS_SECRET_KEY ); // Create a bucket $bucket = 'shahar-talks'; $s3->createBucket($bucket); // Store the current file in our bucket $s3->putFile( __FILE__, $bucket . '/' . basename(__FILE__) );
  10. 10. Amazon S3 ● The object we have just created is at http://s3.amazonaws.com/shahar-talks/filename ● ...but it's private (objects are private by default) so you can't access it without authenticating ● You can access it using API after authenticating, but not using a browser ● More about access control later on // Get the file we just uploadad $file = $s3->getObject( $bucket . '/' . basename(__FILE__) );
  11. 11. but what's that about buckets!?!
  12. 12. Amazon S3 - Buckets ● S3 stores files in “buckets” ● Think of them as storage namespaces ● Buckets are globally unique! /** DON'T TRY THIS AT HOME, KIDS **/ $buckets = $s3->getBuckets(); foreach ($buckets as $bucket) { $objects = $s3->getObjectsByBucket($bucket); echo "Deleting " . count($objects) . " from bucket $bucket...n"; $s3->cleanBucket($bucket); $s3->removeBucket($bucket); }
  13. 13. Amazon S3 – Access Control ● Access Control can be set using the 3rd parameter of putObject(): $location = 'shahar-talks/picture.jpg'; $s3->putObject('picture.jpg', $location, array( // Make the picture publicly readable Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ, // Specify content type Zend_Service_Amazon_S3::S3_CONTENT_TYPE_HEADER => 'image/jpeg' ));
  14. 14. Amazon S3 – Streaming ● By default, the getObject and putObject methods load entire files into memory ● Dealing with large files, you will very quickly hit PHP's memory limit ● (or you'll just crash your server) ● Solution: use the streaming methods!
  15. 15. Amazon S3 - Streaming // Store a potentially large object $localFile = '/home/shahar/pirated-movie-dvd.iso'; $destination = 'my-stuff/' . basename($localFile); $s3->putFileStream($localFile, $destination, $meta); // Or: $fp = fopen($localFile); $s3->putObject($destination, $fp, $meta); // Get a potentially large object $s3->getObjectStream($destination, $localFile); // Or: $response = $s3->getObjectStream($destination); $fp = fopen($response->getStreamName(), 'r'); while ($data = fread($fp, 4096)) { file_put_contents('/dev/null', $data); } fclose($fp);
  16. 16. The Amazon S3 Stream Wrapper ● Even Cooler: ZF provides a user-space stream wrapper for S3! // Register the stream wrapper $s3->registerStreamWrapper("s3"); // Now we can use S3 files more or less as regular files $bucket = "my-secret-stuff"; mkdir("s3://$bucket"); $localFp = fopen($localFile, 'r'); $remoteFp = fopen("s3://$bucket/secret-dvd.iso", 'w'); stream_filter_append($remoteFp, 'mcrypt.tripledes', STREAM_FILTER_WRITE, $filterOpts); // Copy my local file to S3, encrypting it as we go stream_copy_to_stream($localFp, $remoteFp);
  17. 17. Amazon EC2 ● Amazon Elastic Compute Cloud ● On-demand virtual servers in the cloud ● You are “guaranteed” CPU and memory ● You are not-so-guaranteed I/O and network throughput ● Different machine types are available, pricing varies ● Useful for hosting, and for other stuff!
  18. 18. Amazon EC2 - Instances $ec2 = new Zend_Service_Amazon_Ec2_Instance( AWS_SECRET_KEY, AWS_ACCESS_KEY ); // Set EC2 region $ec2->setRegion('us-west-1'); // Start an EC2 machine instance $result = $ec2->run(array( 'instanceType' => 'm1.large', 'imageId' => 'ami-123456', 'securityGroup' => array('default', 'ssh'), 'keyName' => 'my-ssh-key', 'userData' => $myUserData, ));
  19. 19. Amazon EC2 - Instances // ... continued // Wait for machine to be up and running $instance = $result['instances'][0]; $state = $instance['instanceState']['name']; while ($state != 'running') { sleep(5); $result = $ec2->describe($instance['instanceId']); $instance = $result['instances'][0]; $state = $instance['instanceState']['name']; } // Print out the machine's public hostname echo $instance['dnsName'];
  20. 20. Amazon EC2 - EBS ● Allows you to create permanent storage for EC2 instances ● EBS volumes - block devices that can be attached to any EC2 instance ● Can be attached to a single EC2 machine at a time, and detached while not in use ● You can create EBS snapshots of volumes ● These are stored in S3 ● You can quickly create multiple volumes from a single snapshot
  21. 21. Amazon EC2 - EBS $ebs = new Zend_Service_Amazon_Ec2_Ebs( AWS_ACCESS_KEY, AWS_SECRET_KEY ); /* Create and attach a 10gb volume */ $result = $ebs->createNewVolume(10, 'eu-west-1a'); while($result['status'] != 'available') { sleep(1); $result = array_shift( $ebs->describeVolume($result['volumeId']) ); } $ebs->attachVolume( $result['volumeId'], $instanceId, '/dev/sdf' );
  22. 22. Amazon EC2 - EBS /* Create a snapshot of a volume */ $snapInfo = $ebs->createSnapshot($volumeId); while ($snapInfo['status'] != 'completed') { sleep(1); $snapInfo = array_shift( $ebs->describeSnapshot($snapInfo['snapshotId']) ); } /* Create volumes from snapshot and attach to machines */ foreach($myInstances as $instInfo) { $result = $ebs->createVolumeFromSnapshot( $snapInfo['snapshotId'],$instInfo['availabilityZone'] ); $ebs->attachVolume( $result['volumeId'], $instInfo['instanceId'], '/dev/sdf' ); }
  23. 23. Amazon EC2 - ELB ● Elastic Load Balancing – load balancing service for EC2 machine clusters ● Can do TCP or HTTP load balancing ● Optionally provides session stickiness ● Either TCP or HTTP based stickiness ● You can set up health pings and add / remove machines based on health ● Can distribute load across availability zones
  24. 24. Amazon EC2 - ELB ● Ok, not really in ZF... but still useful http://github.com/shevron/zf-amazonaws-elasticlb ● I needed access to Amazon ELB, so I wrote a ZF-compatible class for it ● I got too busy / lazy / bored so it never made it to ZF ● Maybe you wanna finish it? ;-)
  25. 25. Amazon EC2 - EBS /* NOTE: this class is NOT in ZF! */ $elb = new Zend_Service_Amazon_Ec2_ElasticLB( AWS_ACCESS_KEY, AWS_SECRET_KEY ); /* Create a listener on port 80, HTTP */ $listener = new Zend_Service_Amazon_Ec2_ElasticLB_Listener( 80, 80, 'HTTP' ); /* Create a load balancer */ $lbInfo = $elb->create( 'my-http-lb', 'eu-west-1b', $listener ); /* Register instances with load balancer */ $instanceIds = array(); foreach($myInstances as $instInfo) { $instanceIds[] = $instInfo['instanceId']; } $elb->registerInstances('my-http-lb', $instanceIds);
  26. 26. More AWS + ZF Goodies ● SimpleDB ● Simple Queue Service ● Elastic IP Addresses ● EC2 Images ● EC2 Windows Instances ● CloudWatch Monitoring ● Reserved Instances
  27. 27. Where Next? ● Last Questions? ● Email me: shahar.e@zend.com ● IRC: #zftalk, #zendserver @ FreeNode ● http://framework.zend.com/manual/ ● http://aws.amazon.com/documentation/ ● Pay-per-use Zend Server images: http://zend.com/products/server/amazon/
  28. 28. Thank You!

×