Amazon Cloud Services and Zend Framework

28
Shahar Evron | Zend Technologies Amazon Cloud Services and Zend Framework

description

 

Transcript of Amazon Cloud Services and Zend Framework

Page 1: Amazon Cloud Services and Zend Framework

Shahar Evron | Zend Technologies

Amazon Cloud Servicesand Zend Framework

Page 2: Amazon Cloud Services and Zend Framework

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

Page 3: Amazon Cloud Services and Zend Framework

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

Page 4: Amazon Cloud Services and Zend Framework

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

Page 5: Amazon Cloud Services and Zend Framework

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

Page 6: Amazon Cloud Services and Zend Framework

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

Page 7: Amazon Cloud Services and Zend Framework

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');

Page 8: Amazon Cloud Services and Zend Framework

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

Page 9: Amazon Cloud Services and Zend Framework

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__));

Page 10: Amazon Cloud Services and Zend Framework

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__));

Page 11: Amazon Cloud Services and Zend Framework

but what's that about buckets!?!

Page 12: Amazon Cloud Services and Zend Framework

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); }

Page 13: Amazon Cloud Services and Zend Framework

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' ));

Page 14: Amazon Cloud Services and Zend Framework

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!

Page 15: Amazon Cloud Services and Zend Framework

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);

Page 16: Amazon Cloud Services and Zend Framework

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);

Page 17: Amazon Cloud Services and Zend Framework

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!

Page 18: Amazon Cloud Services and Zend Framework

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, ));

Page 19: Amazon Cloud Services and Zend Framework

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'];

Page 20: Amazon Cloud Services and Zend Framework

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

Page 21: Amazon Cloud Services and Zend Framework

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' );

Page 22: Amazon Cloud Services and Zend Framework

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' ); }

Page 23: Amazon Cloud Services and Zend Framework

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

Page 24: Amazon Cloud Services and Zend Framework

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? ;-)

Page 25: Amazon Cloud Services and Zend Framework

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);

Page 26: Amazon Cloud Services and Zend Framework

More AWS + ZF Goodies

● SimpleDB

● Simple Queue Service

● Elastic IP Addresses

● EC2 Images

● EC2 Windows Instances

● CloudWatch Monitoring

● Reserved Instances

Page 27: Amazon Cloud Services and Zend Framework

Where Next?

● Last Questions?● Email me: [email protected]● 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/

Page 28: Amazon Cloud Services and Zend Framework

Thank You!