Slide 1

Slide 1 text

aws for artisans by @jeremeamia #aws #laracon

Slide 2

Slide 2 text

Hi, I’m Jeremy. or @jeremeamia Seattle PHP User Group or @seaphp AWS SDK for PHP @awsforphp

Slide 3

Slide 3 text

•  aws/aws-­‐sdk-­‐php   Used in Laravel Queues for SQS driver. •  aws/aws-­‐sdk-­‐php-­‐laravel   AWS Service Provider for Laravel. •  jeremeamia/super_closure   Allows you to serialize/queue closures.

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

“The Cloud”

Slide 7

Slide 7 text

The Clooouuud

Slide 8

Slide 8 text

The term "Cloud Computing" refers to the on-demand delivery of IT resources via the Internet with pay-as-you-go pricing.

Slide 9

Slide 9 text

IT Resources? Servers • Computing Nodes Distributed Queues • Databases Push Notifications File Storage • Load Balancers Content Delivery Networks Cache Clusters • more…

Slide 10

Slide 10 text

AWS services each have an API, so you can control your resources through programs and scripts.

Slide 11

Slide 11 text

So let's provision a web server from PHP by using the AWS SDK for PHP to talk to the Amazon EC2 API Amazon  EC2  

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

With the AWS Service Provider for Laravel:

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

STORAGE   VOLUMES   KEY  PAIRS   STATIC  IPs   FIREWALLS  

Slide 19

Slide 19 text

Amazon  EC2  

Slide 20

Slide 20 text

Amazon  EC2   Amazon  S3   Amazon  RDS   Amazon  SQS   Elas?c   Transcoder  

Slide 21

Slide 21 text

Amazon  S3   Amazon  SES   Amazon  SimpleDB   Amazon  SNS   Amazon  SQS   Amazon  SWF   Amazon  VPC   Auto  Scaling   AWS  CloudHSM   AWS  Data  Pipeline   AWS  Elas?c  Beanstalk   AWS  Import/Export   AWS  OpsWorks   AWS  Marketplace   AWS  Storage  Gateway   AWS  Support   Elas?c  Load  Balancing   Amazon  CloudForma?on   Amazon  CloudFront   Amazon  CloudSearch   Amazon  CloudWatch   Amazon  Direct  Connect   Amazon  DynamoDB   Amazon  EBS   Amazon  EC2   Amazon  Elas?Cache   Amazon  Elas?c  Transcoder   Amazon  EMR   Amazon  Glacier   Amazon  IAM   Amazon  Mechanical  Turk   Amazon  RDS   Amazon  RedshiR   Amazon  Route53  

Slide 22

Slide 22 text

PHP • Java • Python • .NET • Ruby JavaScript • Node • iOS • Android SDKs AWS CLI • Visual Studio Plugin Eclipse Plugin • PowerShell Management Console • iOS App Tools https://github.com/aws AWS SDKs & Tools

Slide 23

Slide 23 text

Hundreds of Thousands of Customers in 190 Countries

Slide 24

Slide 24 text

Let’s Build Something!

Slide 25

Slide 25 text

Funny Face App

Slide 26

Slide 26 text

Funny Face App

Slide 27

Slide 27 text

Funny Face App Level #1 Wherein we setup our dependencies

Slide 28

Slide 28 text

Add the AWS SDK for PHP In your composer.json: {      ...      "aws/aws-­‐sdk-­‐php":  "~2.6.2",      "aws/aws-­‐sdk-­‐php-­‐laravel":  "~1.1",      ...   }  

Slide 29

Slide 29 text

Add the AWS Service Provider In your app/config/app.php:    'providers'  =>  array(          ...          'Aws\Laravel\AwsServiceProvider',      )            'aliases'  =>  array(          ...          'Aws\Laravel\AwsFacade',      )  

Slide 30

Slide 30 text

Setup AWS Credentials In your ~/.aws/credentials file:     [default]   aws_access_key_id=AKIAEXAMPLE3A…   aws_secret_access_key=wFooBarTn…   Available in aws/aws-­‐sdk-­‐php:>=2.6.2    

Slide 31

Slide 31 text

Setup AWS Resources $  php  artisan  funnyface:setup   Config  file  written  to  app/config/funnyfaces.php.   Creating  S3  bucket  "funnyfaces-­‐xkl9syjmwkhm"…   Done.   Creating  DynamoDB  table  "funnyfaces-­‐xkl9syjmwkhm"…   Done.   The  Funny  Face  App  is  setup.   Amazon   S3   Amazon   DynamoDB  

Slide 32

Slide 32 text

Setup S3 Bucket function  createBucket(S3Client  $s3,  $bucket)   {    if  (!$s3-­‐>doesBucketExist($bucket))  {      $s3-­‐>createBucket(['Bucket'  =>  $bucket]);      $s3-­‐>waitUntil('BucketExists',  [        'Bucket'  =>  $bucket      ]);    }   }  

Slide 33

Slide 33 text

Setup S3 Bucket function  createBucket(S3Client  $s3,  $bucket)   {    if  (!$s3-­‐>doesBucketExist($bucket))  {      $s3-­‐>createBucket(['Bucket'  =>  $bucket]);      $s3-­‐>waitUntil('BucketExists',  [        'Bucket'  =>  $bucket      ]);    }   }        if  (!$s3-­‐>doesBucketExist($bucket))  {   Helper method

Slide 34

Slide 34 text

Setup S3 Bucket function  createBucket(S3Client  $s3,  $bucket)   {    if  (!$s3-­‐>doesBucketExist($bucket))  {      $s3-­‐>createBucket(['Bucket'  =>  $bucket]);      $s3-­‐>waitUntil('BucketExists',  [        'Bucket'  =>  $bucket      ]);    }   }            $s3-­‐>createBucket(['Bucket'  =>  $bucket]);   Operation ( or “Command”) Parameters

Slide 35

Slide 35 text

Setup S3 Bucket function  createBucket(S3Client  $s3,  $bucket)   {    if  (!$s3-­‐>doesBucketExist($bucket))  {      $s3-­‐>createBucket(['Bucket'  =>  $bucket]);      $s3-­‐>waitUntil('BucketExists',  [        'Bucket'  =>  $bucket      ]);    }   }              $s3-­‐>waitUntil('BucketExists',  [        'Bucket'  =>  $bucket      ]);   Waiter

Slide 36

Slide 36 text

AWS SDK – Waiters •  Poll resources until they’re available •  Handle asynchronous operations $db-­‐>waitUntil('TableExists',  [          'TableName'  =>  $table,   ]);    

Slide 37

Slide 37 text

Setup DynamoDB Table function  createTable(DynamoDbClient  $db,  $table)   {      $db-­‐>createTable(['TableName'  =>  $table,  ...]);      $db-­‐>waitUntil('TableExists',  [          'TableName'  =>  $table      ]);   }    

Slide 38

Slide 38 text

Setup DynamoDB Table 'TableName'  =>  $table,   'AttributeDefinitions'  =>  [      ['AttributeName'=>'app',    'AttributeType'=>'S'],      ['AttributeName'=>'time’,  'AttributeType'=>'N’]],   'KeySchema'  =>  [      ['AttributeName'=>'app',    'KeyType'=>'HASH'],      ['AttributeName'=>'time',  'KeyType'=>'RANGE’]],   'ProvisionedThroughput'  =>  [      'ReadCapacityUnits'    =>  5,      'WriteCapacityUnits'  =>  1],      

Slide 39

Slide 39 text

Funny Face App Level #2 Wherein we build our app logic

Slide 40

Slide 40 text

Routes – “/” (GET) Route::get('/',  function()  {      return  View::make('show',  [          'faces'  =>  App::make('funnyfaces')-­‐>latest(),          'success'  =>  Session::get('success'),      ]);   });  

Slide 41

Slide 41 text

Getting the Funny Faces public  function  latest($limit  =  25)   {      $db  =  $this-­‐>sdk-­‐>get('dynamodb');      $items  =  $db-­‐>getIterator('Query',  [          'TableName'  =>  $this-­‐>cfg['table'],          'ScanIndexForward'  =>  false,          'Limit'  =>  $limit,          'KeyConditions'  =>  [...]      ]);      return  new  ItemIterator($items);   }  

Slide 42

Slide 42 text

AWS SDK – Iterators •  No handling of markers or tokens •  Compatible with SPL iterators •  Enumerate entire result sets •  Lazy loads results $items  =  $db-­‐>getIterator('Query',  […]);   foreach  ($items  as  $item)  {    echo  $item['key']['S'];   }  

Slide 43

Slide 43 text

Routes – “upload” (GET) Route::get('upload',  function()  {      return  View::make('upload',  [          'success'  =>  Session::get('success'),      ]);   });  

Slide 44

Slide 44 text

Routes – “upload” (POST) try  {      $caption  =  Input::get('caption');      $file  =  Input::file('photo');      if  (!$caption  ||  !$file)  {          throw  new  Exception('File  not  uploaded.');      }      App::make('funnyfaces')-­‐>add($file,  $caption);      return  Redirect::to('show')-­‐>with('success',  1);   }  catch  (Exception  $e)  {      Log::error($e-­‐>getMessage());      return  Redirect::to('upload')-­‐>with('success’,0);   }  

Slide 45

Slide 45 text

Adding a Funny Face (part 1) $s3  =  $this-­‐>sdk-­‐>get('s3');   $result  =  $s3-­‐>putObject([      'Bucket'  =>  $this-­‐>cfg['bucket'],      'Key'  =>  $file-­‐>getFileName(),      'Body'  =>  fopen($file-­‐>getPathname(),  'r'),      'ACL'  =>  'public-­‐read',   ]);     $url  =  $result['ObjectURL'];  

Slide 46

Slide 46 text

Adding a Funny Face (part 2) $db  =  $this-­‐>sdk-­‐>get('dynamodb');   $result  =  $db-­‐>putItem([      'TableName'  =>  $this-­‐>cfg['table'],      'Item'  =>  [          'app'  =>  ['S'  =>  $this-­‐>cfg['hashkey']],          'time'  =>  ['N'  =>  (string)  time()],          'src'  =>  ['S'  =>  $url],          'caption'  =>  ['S'  =>  $caption],      ],   ]);  

Slide 47

Slide 47 text

Funny Face App Level #3 Wherein we deploy to the cloud

Slide 48

Slide 48 text

More AWS Services AWS Elastic Beanstalk An easy way to deploy/manage applications in the Cloud. AWS IAM (Identity and Access Management) Enables you to control access to your AWS resources

Slide 49

Slide 49 text

IAM Roles & Instance Profiles

Slide 50

Slide 50 text

Elastic Beanstalk CLI Tool Step  1  –  Sets  up  project  for  EB   $  eb  init     Step  2  –  Creates  EB  environment   $  eb  start   Step  3+  –  Deploys  new  versions   $  git  aws.push  

Slide 51

Slide 51 text

EB Console and API $eb-­‐>createEnvironment([…]);   $eb-­‐>createApplication([…]);   $eb-­‐>createApplicationVersion([…]);    

Slide 52

Slide 52 text

.ebextensions You may need to do composer self-update   In  .ebextensions/01_update_composer.config:   commands:      01_update_composer:          command:  export  COMPOSER_HOME=/root  &&              /usr/bin/composer.phar  self-­‐update     option_settings:      -­‐  namespace:  aws:elasticbeanstalk:application:environment          option_name:  COMPOSER_HOME          value:  /root    

Slide 53

Slide 53 text

What Have We Made?

Slide 54

Slide 54 text

Funny Face App Step #4 Wherein we add more cool stuff

Slide 55

Slide 55 text

Add Amazon Route 53

Slide 56

Slide 56 text

Add Amazon CloudFront

Slide 57

Slide 57 text

Add Amazon SQS Resize/process photos in the background?   Queue::push('ProcessPhoto',  [      'bucket'  =>  Config::get('ff.bucket'),      'key'  =>  $file-­‐>getFileName(),      'caption'  =>  Input::get('caption'),   ]);  

Slide 58

Slide 58 text

Configuring SQS for Laravel In app/config/queue.php: 'connections'  =>  array(      'sqs'  =>  array(      'driver'  =>  'sqs',      'queue'    =>  'your-­‐queue-­‐url',      'region'  =>  'us-­‐east-­‐1',    )   )  

Slide 59

Slide 59 text

Adding SQS to Architecture

Slide 60

Slide 60 text

Funny Face App Nice Work! J https://github.com/jeremeamia/FunnyFacesL4ExampleApp

Slide 61

Slide 61 text

aws for artisans by @jeremeamia @awsforphp #aws #laracon Feedback: https://joind.in/11330

Slide 62

Slide 62 text

Resources •  h"ps://github.com/aws/aws-­‐sdk-­‐php   •  h"p://docs.aws.amazon.com/aws-­‐sdk-­‐php/guide/latest/index.html   •  h"p://blogs.aws.amazon.com/php   •  h"ps://github.com/jeremeamia/FunnyFacesL4ExampleApp   •  h"ps://twi"er.com/awsforphp    (@awsforphp)   •  h"ps://twi"er.com/aws_eb    (@aws_eb)   •  h"p://docs.aws.amazon.com/elasHcbeanstalk/latest/dg/create_deploy_PHP_eb.html   •  h"p://docs.aws.amazon.com/elasHcbeanstalk/latest/dg/customize-­‐containers.html   •  h"p://www.michaelgallego.fr/blog/2013/08/19/solving-­‐the-­‐elasHc-­‐beanstalk-­‐ composer-­‐deployment-­‐problems/