SlideShare a Scribd company logo
1 of 28
Download to read offline
Shahar Evron | Zend Technologies
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
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
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
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
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
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');
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
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__)
);
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__)
);
but what's that about buckets!?!
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);
}
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'
));
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!
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);
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);
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!
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,
));
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'];
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
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'
);
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'
);
}
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
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? ;-)
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);
More AWS + ZF Goodies
●
SimpleDB
●
Simple Queue Service
●
Elastic IP Addresses
●
EC2 Images
●
EC2 Windows Instances
●
CloudWatch Monitoring
●
Reserved Instances
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/
Thank You!

More Related Content

What's hot

Lightweight wrapper for Hive on Amazon EMR
Lightweight wrapper for Hive on Amazon EMRLightweight wrapper for Hive on Amazon EMR
Lightweight wrapper for Hive on Amazon EMR
Shinji Tanaka
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkKeeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
Jeremy Kendall
 

What's hot (20)

Not your Grandma's XQuery
Not your Grandma's XQueryNot your Grandma's XQuery
Not your Grandma's XQuery
 
Deep Dive: AWS Command Line Interface
Deep Dive: AWS Command Line InterfaceDeep Dive: AWS Command Line Interface
Deep Dive: AWS Command Line Interface
 
Deep Dive into AWS CLI - the command line interface
Deep Dive into AWS CLI - the command line interfaceDeep Dive into AWS CLI - the command line interface
Deep Dive into AWS CLI - the command line interface
 
Slim RedBeanPHP and Knockout
Slim RedBeanPHP and KnockoutSlim RedBeanPHP and Knockout
Slim RedBeanPHP and Knockout
 
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Scaling Symfony2 apps with RabbitMQ - Symfony UK MeetupScaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
 
Deep Dive: AWS Command Line Interface
Deep Dive: AWS Command Line InterfaceDeep Dive: AWS Command Line Interface
Deep Dive: AWS Command Line Interface
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
 
Deep Dive: AWS Command Line Interface
Deep Dive: AWS Command Line InterfaceDeep Dive: AWS Command Line Interface
Deep Dive: AWS Command Line Interface
 
Masterclass Advanced Usage of the AWS CLI
Masterclass Advanced Usage of the AWS CLIMasterclass Advanced Usage of the AWS CLI
Masterclass Advanced Usage of the AWS CLI
 
Lightweight wrapper for Hive on Amazon EMR
Lightweight wrapper for Hive on Amazon EMRLightweight wrapper for Hive on Amazon EMR
Lightweight wrapper for Hive on Amazon EMR
 
(DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014
(DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014(DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014
(DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
 
Play á la Rails
Play á la RailsPlay á la Rails
Play á la Rails
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
Deep Dive - Advanced Usage of the AWS CLI
Deep Dive - Advanced Usage of the AWS CLIDeep Dive - Advanced Usage of the AWS CLI
Deep Dive - Advanced Usage of the AWS CLI
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkKeeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
 
Building Cloud Castles - LRUG
Building Cloud Castles - LRUGBuilding Cloud Castles - LRUG
Building Cloud Castles - LRUG
 
Great Developers Steal
Great Developers StealGreat Developers Steal
Great Developers Steal
 
Configuration Management and Provisioning Are Different
Configuration Management and Provisioning Are DifferentConfiguration Management and Provisioning Are Different
Configuration Management and Provisioning Are Different
 
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkPHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php framework
 

Similar to Amazon Cloud Services and Zend Framework

Stack kicker devopsdays-london-2013
Stack kicker devopsdays-london-2013Stack kicker devopsdays-london-2013
Stack kicker devopsdays-london-2013
Simon McCartney
 

Similar to Amazon Cloud Services and Zend Framework (20)

Mastering the AWS SDK for PHP (TLS306) | AWS re:Invent 2013
Mastering the AWS SDK for PHP (TLS306) | AWS re:Invent 2013Mastering the AWS SDK for PHP (TLS306) | AWS re:Invent 2013
Mastering the AWS SDK for PHP (TLS306) | AWS re:Invent 2013
 
Manage cloud infrastructures in PHP using Zend Framework 2 (and 1)
Manage cloud infrastructures in PHP using Zend Framework 2 (and 1)Manage cloud infrastructures in PHP using Zend Framework 2 (and 1)
Manage cloud infrastructures in PHP using Zend Framework 2 (and 1)
 
Symfony finally swiped right on envvars
Symfony finally swiped right on envvarsSymfony finally swiped right on envvars
Symfony finally swiped right on envvars
 
Manage WordPress with Awesome using wp cli
Manage WordPress with Awesome using wp cliManage WordPress with Awesome using wp cli
Manage WordPress with Awesome using wp cli
 
Manage cloud infrastructures using Zend Framework 2 (and ZF1)
Manage cloud infrastructures using Zend Framework 2 (and ZF1)Manage cloud infrastructures using Zend Framework 2 (and ZF1)
Manage cloud infrastructures using Zend Framework 2 (and ZF1)
 
Stack kicker devopsdays-london-2013
Stack kicker devopsdays-london-2013Stack kicker devopsdays-london-2013
Stack kicker devopsdays-london-2013
 
Amazon Web Services for PHP Developers
Amazon Web Services for PHP DevelopersAmazon Web Services for PHP Developers
Amazon Web Services for PHP Developers
 
WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli
WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cliWordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli
WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli
 
infra-as-code
infra-as-codeinfra-as-code
infra-as-code
 
Practical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails AppPractical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails App
 
Symfony2 revealed
Symfony2 revealedSymfony2 revealed
Symfony2 revealed
 
Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011
 
Fatc
FatcFatc
Fatc
 
Alex Casalboni - Configuration management and service discovery - Codemotion ...
Alex Casalboni - Configuration management and service discovery - Codemotion ...Alex Casalboni - Configuration management and service discovery - Codemotion ...
Alex Casalboni - Configuration management and service discovery - Codemotion ...
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Scaling in Mind (Case study of Drupal Core)
Scaling in Mind (Case study of Drupal Core)Scaling in Mind (Case study of Drupal Core)
Scaling in Mind (Case study of Drupal Core)
 
PHP SA 2014 - Releasing Your Open Source Project
PHP SA 2014 - Releasing Your Open Source ProjectPHP SA 2014 - Releasing Your Open Source Project
PHP SA 2014 - Releasing Your Open Source Project
 
eZ Publish Cluster Unleashed
eZ Publish Cluster UnleashedeZ Publish Cluster Unleashed
eZ Publish Cluster Unleashed
 
solving little problems
solving little problemssolving little problems
solving little problems
 
Running Splunk on AWS
Running Splunk on AWSRunning Splunk on AWS
Running Splunk on AWS
 

More from Shahar Evron

PHP ואבטחה - חלק שני
PHP ואבטחה - חלק שניPHP ואבטחה - חלק שני
PHP ואבטחה - חלק שני
Shahar Evron
 
PHP - עבר הווה ועתיד
PHP - עבר הווה ועתידPHP - עבר הווה ועתיד
PHP - עבר הווה ועתיד
Shahar Evron
 
Building Scalable Development Environments
Building Scalable Development EnvironmentsBuilding Scalable Development Environments
Building Scalable Development Environments
Shahar Evron
 

More from Shahar Evron (12)

Best Practices in PHP Application Deployment
Best Practices in PHP Application DeploymentBest Practices in PHP Application Deployment
Best Practices in PHP Application Deployment
 
PHP and Zend Framework on Windows
PHP and Zend Framework on WindowsPHP and Zend Framework on Windows
PHP and Zend Framework on Windows
 
Zend Server: A Guided Tour
Zend Server: A Guided TourZend Server: A Guided Tour
Zend Server: A Guided Tour
 
Zend Server: Scalability & Performance
Zend Server: Scalability & PerformanceZend Server: Scalability & Performance
Zend Server: Scalability & Performance
 
Intro To Couch Db
Intro To Couch DbIntro To Couch Db
Intro To Couch Db
 
Scaling PHP Applications with Zend Platform
Scaling PHP Applications with Zend PlatformScaling PHP Applications with Zend Platform
Scaling PHP Applications with Zend Platform
 
Zend Framework Components for non-framework Development
Zend Framework Components for non-framework DevelopmentZend Framework Components for non-framework Development
Zend Framework Components for non-framework Development
 
PHP ואבטחה - חלק שני
PHP ואבטחה - חלק שניPHP ואבטחה - חלק שני
PHP ואבטחה - חלק שני
 
PHP ואבטחה - חלק ראשון
PHP ואבטחה - חלק ראשוןPHP ואבטחה - חלק ראשון
PHP ואבטחה - חלק ראשון
 
PHP - עבר הווה ועתיד
PHP - עבר הווה ועתידPHP - עבר הווה ועתיד
PHP - עבר הווה ועתיד
 
Content Indexing with Zend_Search_Lucene
Content Indexing with Zend_Search_LuceneContent Indexing with Zend_Search_Lucene
Content Indexing with Zend_Search_Lucene
 
Building Scalable Development Environments
Building Scalable Development EnvironmentsBuilding Scalable Development Environments
Building Scalable Development Environments
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 

Amazon Cloud Services and Zend Framework

  • 1. Shahar Evron | Zend Technologies Amazon Cloud Services and Zend Framework
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. but what's that about buckets!?!
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. More AWS + ZF Goodies ● SimpleDB ● Simple Queue Service ● Elastic IP Addresses ● EC2 Images ● EC2 Windows Instances ● CloudWatch Monitoring ● Reserved Instances
  • 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/