Controlling the Cloud with the AWS SDK for PHP

Controlling the Cloud with the AWS SDK for PHP

Amazon Web Services (AWS) offers a broad set of global compute, storage, database, analytics, application, and deployment services that can help PHP developers build scalable applications in the cloud. These services provide APIs that allow you to control all of your resources programmatically, even through your PHP code. Let's talk about how to use Version 3 of the open source AWS SDK for PHP (built on the Guzzle library) to control your AWS resources and use the AWS services from within your applications. (Given at ZendCon 2015)

Ca57a7cfac69ba3abf517470f3770aae?s=128

Jeremy Lindblom

October 22, 2015
Tweet

Transcript

  1. By Jeremy Lindblom (@jeremeamia)

  2. Jeremy Lindblom (@jeremeamia)

  3. None
  4. None
  5. None
  6. None
  7. None
  8. None
  9. None
  10. None
  11. The Clooouuud

  12. None
  13. "Cloud Computing" on-demand IT resources

  14. AWS services each have an API, so you can control

    your resources through programs and scripts.
  15. 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  
  16. $ec2 = 'region' => 'us-east-1' 'version' => '2015-10-01' $ec2->runInstances 'ImageId'

    => 'ami-6a6dcc02' 'MinCount' => 'MaxCount' => 'InstanceType' => 't2.micro'
  17. None
  18. None
  19. None
  20. Amazon S3 Amazon SES Amazon SimpleDB Amazon SNS Amazon SQS

    Amazon SWF Amazon VPC Auto Scaling AWS CloudHSM AWS Data Pipeline AWS Elastic Beanstalk AWS Import/Export AWS OpsWorks AWS Marketplace AWS Storage Gateway AWS Support Elastic Load Balancing Amazon CloudFormation Amazon CloudFront Amazon CloudSearch Amazon CloudWatch Amazon Direct Connect Amazon DynamoDB Amazon EBS Amazon EC2 Amazon ElastiCache Amazon Elastic Transcoder Amazon EMR Amazon Glacier Amazon IAM Amazon Mechanical Turk Amazon RDS Amazon Redshift Amazon Route53
  21. None
  22. None
  23. None
  24. None
  25. $ec2 = ::factory 'region' => 'us-east-1' $ec2->runInstances 'ImageId' => 'ami-6a6dcc02'

    'MinCount' => 'MaxCount' => 'InstanceType' => 't2.micro'
  26. $ec2 = ::factory 'region' => 'us-east-1' $ec2->runInstances 'ImageId' => 'ami-6a6dcc02'

    'MinCount' => 'MaxCount' => 'InstanceType' => 't2.micro' 'version' => '2015-10-01’
  27. $ec2 = ::factory 'region' => 'us-east-1' $ec2->runInstances 'ImageId' => 'ami-6a6dcc02'

    'MinCount' => 'MaxCount' => 'InstanceType' => 't2.micro'
  28. $ec2 = ::factory 'region' => 'us-east-1' $ec2->runInstances 'ImageId' => 'ami-6a6dcc02'

    'MinCount' => 'MaxCount' => 'InstanceType' => 't2.micro' 'version' => '2015-10-01'
  29. $ec2 = ::factory 'region' => 'us-east-1' $ec2->runInstances 'ImageId' => 'ami-6a6dcc02'

    'MinCount' => 'MaxCount' => 'InstanceType' => 't2.micro' 'version' => '2015-10-01'
  30. •  Async requests with a Promise API •  Support for

    custom HTTP adapters –  cURL no longer required (still the default) –  Possible to implement with non-blocking event loops •  Result "Paginators" for iterating paginated data •  JMESPath querying of result data •  "debug" client option for easy debugging
  31. None
  32. PHP PHP #nofilter #selphpie #instagood

  33. PHP PHP

  34. (Storage of selPHPies) (Storage of URLs/captions) PHP PHP

  35. None
  36. "require": { "aws/aws-sdk-php": "~3.0", "silex/silex": "~1.2", "twig/twig": "~1.16", } getcomposer.org

  37. •  Instance profile credentials •  Credentials file •  Environment variables

    •  Hard coding
  38. •  Instance profile credentials •  Credentials file •  Environment variables

    •  Client configuration
  39. •  Instance profile credentials •  Credentials file •  Environment variables

    •  Client configuration FYI: Also supported by the AWS CLI and other SDKs.
  40. •  Instance profile credentials •  Credentials file •  Environment variables

    •  Client configuration
  41. •  Instance profile credentials •  Credentials file •  Environment variables

    •  Client Configuration (BEWARE) 'credentials' => 'key' => $yourAccessKeyId 'secret' => $yourSecretAccessKey
  42. None
  43. php bin/setup.php S3 bucket DynamoDB table Amazon S3 Amazon DynamoDB

  44. $s3->createBucket 'Bucket' => $bucket $s3->waitUntil 'BucketExists' 'Bucket' => $bucket $dynamoDb->createTable(['TableName'

    => $table ... $dynamoDb->waitUntil 'TableExists' 'TableName' => $table echo "Done.\n"
  45. $s3->createBucket 'Bucket' => $bucket $s3->waitUntil 'BucketExists' 'Bucket' => $bucket $dynamoDb->createTable(['TableName'

    => $table ... $dynamoDb->waitUntil 'TableExists' 'TableName' => $table echo "Done.\n"
  46. None
  47. $s3->createBucket 'Bucket' => $bucket $s3->waitUntil 'BucketExists' 'Bucket' => $bucket $dynamoDb->createTable(['TableName'

    => $table ... $dynamoDb->waitUntil 'TableExists' 'TableName' => $table echo "Done.\n"
  48. None
  49. None
  50. None
  51. $app = new $app 'aws' = function return new 'region'

    => 'us-east-1' 'version' => 'latest' // ROUTES AND OTHER APPLICATION LOGIC $app->run
  52. $app = new $app 'aws' = function return new 'region'

    => 'us-east-1' 'version' => 'latest' // ROUTES AND OTHER APPLICATION LOGIC $app->run
  53. $app = new $app 'aws' = function return new 'region'

    => 'us-east-1' 'S3' => 'version' => '2006-03-01' 'DynamoDb' => 'version' => '2012-08-10'
  54. None
  55. $app->get '/' function ...

  56. $dynamoDb = $app 'aws' ->createDynamoDb $result = $dynamoDb->query 'TableName' =>

    'selphpies' 'Limit' => // ... $items = $result 'Items'
  57. $results = $dynamoDb->getPaginator 'Query' 'TableName' => 'selphpies' // ... $items

    = $results->search 'Items[]'
  58. $results = $s3->getPaginator 'ListObjects' ... $files = $results->search 'Contents[].Key'

  59. # With Paginators $results = $s3->getPaginator 'ListObjects' 'Bucket' => 'my-bucket'

    $keys = $results->search 'Contents[].Key' foreach $keys as $key echo $key . "\n" # Without Paginators $marker = do $args = 'Bucket' => 'my-bucket' if $marker $args 'Marker' = $marker $result = $s3->listObjects $args $objects = array $result 'Contents' foreach $objects as $object echo $object 'Key' . "\n" $marker $result->search 'NextMarker || Contents[-1].Key' while $result 'IsTruncated'
  60. http://jmespath.org/ $result->search '<JMESPath Expression>' > 'Contents[].Key' > '[CommonPrefixes[].Prefix, Contents[].Key][]' >

    'NextMarker || Contents[-1].Key'
  61. $app->post '/upload' function ...

  62. try { $caption = $request->request->get('selphpieCaption', '...'); $file = $request->files->get('selphpieImage'); if

    (!$file instanceof UploadedFile || $file->getError()) { throw new \RuntimeException('...'); } #1. UPLOAD THE IMAGE TO S3 #2. SAVE THE IMAGE DATA TO DYNAMODB $app['session']->getFlashBag()->add('alerts', 'success'); return $app->redirect('/'); } catch (\Exception $e) { $app['session']->getFlashBag()->add('alerts', 'danger'); return $app->redirect('/upload'); }
  63. $s3 = $app['aws']->createS3(); $result = $s3->putObject([ 'Bucket' => 'selphpies', 'Key'

    => $file->getClientOriginalName(), 'Body' => fopen($file->getFileName(), 'r'), 'ACL' => 'public-read', ]);
  64. // Automatically switches to multipart uploads // if the file

    is larger than default threshold. $result = $s3->upload( 'selphpies', $file->getClientOriginalName(), fopen($file->getPathname(), 'r'), 'public-read' );
  65. $dynamoDb->putItem([ 'TableName' => 'selphpies', 'Item' => [ // ... 'src'

    => ['S' => $result['ObjectURL']], 'caption' => ['S' => $caption], ], ]);
  66. $m = new (); $dynamoDb->putItem([ 'TableName' => 'selphpies', 'Item' =>

    $m->marshalItem([ // ... 'src' => $result['ObjectURL’], 'caption' => $caption, ]), ]);
  67. PHP such php so cloud wow very selfie much app

    #phpdoge
  68. github.com/aws/aws-sdk-php/releases @awsforphp blogs.aws.amazon.com/php

  69. github.com/aws/aws-sdk-php forums.aws.amazon.com

  70. By Jeremy Lindblom (@jeremeamia)