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.

PHP as a Service TDC2019

140 views

Published on

PHP as a Service Talking about running PHP on AWS Lambda using Bref

Published in: Technology
  • Be the first to comment

  • Be the first to like this

PHP as a Service TDC2019

  1. 1. Globalcode – Open4education PHP as a Service Serverless PHP applications
  2. 2. Globalcode – Open4education gitlab.com/pvgomes Paulo Victor Gomes - pvgomes Tech Lead at Z-Tech - AB Inbev/AmBev @_pvgomes former CTO at Mundo Verde and Natue, former software engineering at Dafiti, Kanui, Tricae and Rocket Internet github.com/pvgomes pvgomes
  3. 3. Globalcode – Open4education What is Serverless? Serverless Computing still uses servers, but you no longer have to worry about managing them
  4. 4. Globalcode – Open4education Key Features and Benefits
  5. 5. Globalcode – Open4education Key Features and Benefits ● No Server Management
  6. 6. Globalcode – Open4education Key Features and Benefits ● No Server Management ● Easy & Efficient Scaling
  7. 7. Globalcode – Open4education Key Features and Benefits ● No Server Management ● Easy & Efficient Scaling ● Built in High Availability and Fault Tolerance
  8. 8. Globalcode – Open4education Key Features and Benefits ● No Server Management ● Easy & Efficient Scaling ● Built in High Availability and Fault Tolerance ● Service Integration
  9. 9. Globalcode – Open4education Key Features and Benefits ● No Server Management ● Easy & Efficient Scaling ● Built in High Availability and Fault Tolerance ● Service Integration ● Pay as you Go
  10. 10. Globalcode – Open4education How Serverless Works?
  11. 11. Globalcode – Open4education Serverless Infrastructure Providers
  12. 12. Globalcode – Open4education The Major players google trends
  13. 13. Globalcode – Open4education Some Serverless Services in AWS
  14. 14. Globalcode – Open4education AWS Lambda Hello World
  15. 15. Globalcode – Open4education AWS Lambda Hello World
  16. 16. Globalcode – Open4education AWS Lambda Hello World
  17. 17. Globalcode – Open4education A simples AWS Lambda - Example https://github.com/pvgomes/slack-lambda
  18. 18. Globalcode – Open4education A simples AWS Lambda - Code
  19. 19. Globalcode – Open4education A simples AWS Lambda - Code
  20. 20. Globalcode – Open4education A simples AWS Lambda - Example /natuelabs wiki
  21. 21. Globalcode – Open4education A simples AWS Lambda - Code class Wiki(Command): def execute(self, argument): try: scopes = ["https://www.googleapis.com/auth/drive",...] secret_file = os.path.join(parent_dir, '../client.json') credentials = Credentials.from_service_account_file(secret_file, scopes=scopes) service = discovery.build('sheets', 'v4', credentials=credentials) range_name = "wiki!B2:B40" request = service.spreadsheets().values().get(spreadsheetId=id, range=range_name) response = request.execute() wiki = "" values = response.get('values') for value in values: wiki = wiki + ''.join(value) + "n" return wiki except OSError as e: return str(e)
  22. 22. Globalcode – Open4education A simples AWS Lambda - Example
  23. 23. Globalcode – Open4education A simples AWS Lambda - “Deploy” Package the code Upload with aws cli
  24. 24. Globalcode – Open4education Serverless Frameworks https://serverless.com AWS Serverless Application Model https://aws.amazon.com/serverless/sam
  25. 25. Globalcode – Open4education Serverless + AWS Lambda
  26. 26. Globalcode – Open4education Serverless + AWS Lambda
  27. 27. Globalcode – Open4education Serverless + AWS Lambda
  28. 28. Globalcode – Open4education What about PHP ?
  29. 29. Globalcode – Open4education PHP as a Service 1 request = 1 process
  30. 30. Globalcode – Open4education PHP as a Service PHP is faas-ready
  31. 31. Globalcode – Open4education What about AWS Lambda and PHP?
  32. 32. Globalcode – Open4education AWS lambda does not support PHP
  33. 33. Globalcode – Open4education NodeJS PHP Handler
  34. 34. Globalcode – Open4education Is that ok for you?
  35. 35. Globalcode – Open4education AWS Lambda Layers
  36. 36. Globalcode – Open4education Compile the desired version of PHP
  37. 37. Globalcode – Open4education Compile the desired version of PHP
  38. 38. Globalcode – Open4education Compile the desired version of PHP
  39. 39. Globalcode – Open4education Compile the desired version of PHP
  40. 40. Globalcode – Open4education Upload your layer
  41. 41. Globalcode – Open4education Upload your layer
  42. 42. Globalcode – Open4education Create your functions Install using composer Init a sample function <?php function handler($event) { return "Hello, {$event['name']}!"; }
  43. 43. Globalcode – Open4education Create AWS S3 deployment bucket aws s3 mb s3://phptdc
  44. 44. Globalcode – Open4education Package artifacts sam package --template-file template.yaml --output-template-file serverless-output.yaml --s3-bucket phptdc
  45. 45. Globalcode – Open4education Deploy package sam deploy --template-file serverless-output.yaml --stack-name my-first-php-lambda-layer --capabilities CAPABILITY_IAM
  46. 46. Globalcode – Open4education What?
  47. 47. Globalcode – Open4education PHP community never sleeps
  48. 48. Globalcode – Open4education Bref.sh
  49. 49. Globalcode – Open4education Bref Installation Install using composer Init a sample function Install Bref composer require bref/bref:0.4.1 Init a sample function vendor/bin/bref init
  50. 50. Globalcode – Open4education Bref Sample
  51. 51. Globalcode – Open4education Bref Sample
  52. 52. Globalcode – Open4education template.yaml AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: '' Resources: TdcFunction: Type: AWS::Serverless::Function Properties: FunctionName: 'tdc-function' Description: '' CodeUri: . Handler: index.php Timeout: 30 # in seconds (API Gateway has a timeout of 30 seconds) MemorySize: 1024 # The memory size is related to the pricing and CPU power Runtime: provided Layers: - 'arn:aws:lambda:us-east-1:209497400698:layer:php-73-fpm:7'
  53. 53. Globalcode – Open4education template.yaml - events ... Events: # The function will match all HTTP URLs HttpRoot: Type: Api Properties: Path: / Method: ANY HttpSubPaths: Type: Api Properties: Path: /{proxy+} Method: ANY
  54. 54. Globalcode – Open4education index.php <?php $date = date('d/m/Y H:i:s'); $title = <<<EOT <h1>PHP as a Service</h1> <p> TDC 2019 %s </p> <img src="https://s3-sa-east- 1.amazonaws.com/thedevconf/2019/img/divulgacao/saopaulo/banner- TDC2019-sao-paulo-180x150.png"> EOT; echo sprintf($title, $date);
  55. 55. Globalcode – Open4education Lambda run locally sam local start-api
  56. 56. Globalcode – Open4education Lambda run locally
  57. 57. Globalcode – Open4education SAM uses docker to simulate Lambda Before/After request During request
  58. 58. Globalcode – Open4education SAM uses docker to simulate Lambda
  59. 59. Globalcode – Open4education Bref deployment - package artifacts sam package --template-file template.yaml --output-template-file serverless-output.yaml --s3-bucket phptdc
  60. 60. Globalcode – Open4education Bref deployment - deploy package sam deploy --template-file serverless-output.yaml --stack-name tdc-functions --capabilities CAPABILITY_IAM
  61. 61. Globalcode – Open4education Our lambda is deployed
  62. 62. Globalcode – Open4education AWS cli - stack information aws cloudformation describe-stacks --stack-name tdc-functions
  63. 63. Globalcode – Open4education AWS cli - stack information { "Stacks": [ { "StackName": "tdc-functions", ….. "Outputs": [ { "OutputValue":"https://z6lppb8vl9.execute-api.us-east- 1.amazonaws.com/Prod/", } ]} ] }
  64. 64. Globalcode – Open4education API Gateway URL / test me https://z6lppb8vl9.execute-api.us-east-1.amazonaws.com/Prod/
  65. 65. Globalcode – Open4education Bref with Symfony https://bref.sh/docs/frameworks/symfony.html
  66. 66. Globalcode – Open4education Create from scratch Create path mkdir symfony-serverless Install symfony composer create-project symfony/website-skeleton symfony-serverless Start server symfony-serverless/bin/console server:start
  67. 67. Globalcode – Open4education Create from scratch
  68. 68. Globalcode – Open4education First page - Lucky Number Create Lucky controller (src/Controller/LuckyController.php) <?php namespace AppController; use SymfonyComponentRoutingAnnotationRoute; use SymfonyBundleFrameworkBundleControllerAbstractController; class LuckyController extends AbstractController { /** * @Route("/") */ public function number() { $number = random_int(0, 100); return $this->render('lucky/number.html.twig', [ 'number' => $number, ]); } }
  69. 69. Globalcode – Open4education Again - Built in
  70. 70. Globalcode – Open4education Setup Bref Install bref into project composer required bref/bref:0.4.1 Create lambda template template.yaml Content https://gist.github.com/pvgomes/59632c00d3216fd78769492d6f82e130
  71. 71. Globalcode – Open4education Change log path into src/Kernel.php ... public function getLogDir() { if (getenv('LAMBDA_TASK_ROOT') !== false) { return '/tmp/log/'; } return $this->getProjectDir().'/var/log'; } public function getCacheDir() { if (getenv('LAMBDA_TASK_ROOT') !== false) { return '/tmp/cache/'.$this->environment; } return $this->getProjectDir().'/var/cache/'.$this->environment; }
  72. 72. Globalcode – Open4education Run it locally with SAM
  73. 73. Globalcode – Open4education Bref deployment - package artifacts sam package --template-file template.yaml --output-template-file serverless-output.yaml --s3-bucket phptdc
  74. 74. Globalcode – Open4education Bref deployment - deploy package sam deploy --template-file serverless-output.yaml --stack-name symfony-serverless --capabilities CAPABILITY_IAM
  75. 75. Globalcode – Open4education Done / test me https://j0znkm50u0.execute-api.us-east-1.amazonaws.com/Prod/
  76. 76. Globalcode – Open4education Bref new features - 0.5.0 Bref now recommends Serverless instead of SAM Changes: https://github.com/brefphp/bref/releases/tag/0.5.0
  77. 77. Thank you! @_pvgomes pvgomes.com/videos

×