Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Running your Symfony project on AWS Lambda

Da2d2829b89cde136392973a35b68959?s=47 nealio82
September 28, 2018

Running your Symfony project on AWS Lambda

Given during Symfony Live London 2018, this presentation showcases a new tool called Bref, which takes the pain out of deploying PHP projects on AWS Lambda, allowing you to focus on your application code while saving money by cutting down on wasted server resources.

See https://github.com/mnapoli/bref for more info

Da2d2829b89cde136392973a35b68959?s=128

nealio82

September 28, 2018
Tweet

Transcript

  1. Running your Symfony project on AWS Lambda

  2. None
  3. None
  4. None
  5. None
  6. None
  7. Running your Symfony project on AWS Lambda

  8. (coupling yourself to AWS for fun & profit)

  9. fun

  10. profit

  11. EC2

  12. EC2 Data Store

  13. EC2 Data Store HTTPS

  14. EC2 Data Store Load Balancer HTTPS

  15. EC2 Data Store Load Balancer EC2 EC2

  16. EC2 Data Store Load Balancer EC2 EC2 $$$ $$ $$$

    $$$ $$$
  17. EC2 Data Store Load Balancer EC2 EC2

  18. Lambda is a piece of code that only costs you

    money while it’s being executed
  19. Lambda & PHP from scratch is a PITA

  20. None
  21. Lambda function

  22. Lambda function JS Handler

  23. Lambda function JS Handler PHP Binary

  24. Lambda function JS Handler PHP Binary

  25. Lambda function JS Handler PHP Binary

  26. Lambda function JS Handler PHP Binary

  27. None
  28. mnapoli/bref

  29. • Well documented • Serverless framework plugin (easy to get

    running) • Built on top of FIG standards (PSR-7) mnapoli/bref
  30. Lambda function JS Handler PHP Binary

  31. Lambda function JS Handler PHP Binary PSR-7 bref.php

  32. Lambda function JS Handler PHP Binary PSR-7

  33. Lambda function JS Handler PHP Binary PSR-7 API Gateway

  34. Lambda function JS Handler PHP Binary API Gateway HTTPS FREE

    SSL!!! PSR-7
  35. Things to remember

  36. • Application logs are sent to Cloudwatch • PHP is

    running through the CLI SAPI
  37. What we’ll cover

  38. • Setting up a new ‘Hello World’ SF 4 site

    • Setting up a REST API with database migrations • Converting an existing site to Lambda • Retro-fitting to an older Symfony site • Cron tasks • Performance
  39. Hello World

  40. • Install Symfony • Install Bref • Set up some

    config • Deploy • Explore the environment • Fix any issues
  41. None
  42. None
  43. None
  44. None
  45. None
  46. None
  47. None
  48. None
  49. None
  50. None
  51. None
  52. None
  53. None
  54. use the Symfony adapter

  55. None
  56. includes Lambda env vars package name

  57. None
  58. None
  59. create .bref.yml with build hooks

  60. None
  61. None
  62. None
  63. None
  64. None
  65. None
  66. None
  67. None
  68. None
  69. None
  70. None
  71. None
  72. None
  73. click me

  74. oh noes

  75. None
  76. None
  77. None
  78. let’s refresh…

  79. oh noes again!

  80. None
  81. None
  82. let’s refresh again…

  83. None
  84. None
  85. • Installed Symfony • Installed Bref • Copied config from

    the docs • Deployed • Had a look around • Used Cloudwatch to help us debug
  86. That’s great, but how about doing something useful?

  87. None
  88. • Install Api-Platform • Install Bref • Add a database

    connection • Run Symfony commands • Get rid of the ugly Cloudfront hostname
  89. None
  90. None
  91. None
  92. None
  93. None
  94. None
  95. None
  96. None
  97. None
  98. None
  99. • Change logs directory in Kernel class • Configure includes

    in serverless.yml • Replace contents of bref.php with Symfony adapter • Add cache warmup to .bref.yml
  100. add /dev prefix to routing

  101. Setting up the database connection

  102. Lambda function JS Handler PHP Binary PSR-7 API Gateway

  103. Lambda function JS Handler PHP Binary PSR-7 API Gateway

  104. Lambda function JS Handler PHP Binary PSR-7 API Gateway Data

    Store
  105. Lambda function JS Handler PHP Binary PSR-7 API Gateway Data

    Store VPC
  106. ${env:DATABASE_URL}

  107. None
  108. None
  109. Running Symfony commands with bref

  110. None
  111. invoke Lambda function

  112. Symfony command

  113. None
  114. None
  115. entry point works!

  116. get empty collection

  117. post a new book

  118. yay!!!

  119. That’s great, but I have a really cool domain name…

  120. None
  121. None
  122. None
  123. None
  124. None
  125. we can remove /dev route prefix :)

  126. w00t!

  127. • Installed Symfony • Installed Api-Platform • Installed Bref •

    Configured the standard files • Added a database connection through environment vars • Ran Symfony commands • Added a custom domain name via the AWS GUI
  128. That’s great, but my site is more than just a

    REST API…
  129. None
  130. None
  131. None
  132. None
  133. vich/uploader-bundle

  134. • Install Bref to an existing site • Move assets

    to a CDN • Move session storage to a centralised location • Add image uploading
  135. None
  136. None
  137. None
  138. • Change logs directory in Kernel class • Configure includes

    in serverless.yml • Replace contents of bref.php with Symfony adapter • Add cache warmup to .bref.yml
  139. None
  140. None
  141. None
  142. None
  143. None
  144. webpack.config.js

  145. process.env.ASSETS_BUCKET_NAME

  146. None
  147. None
  148. None
  149. ${env:ASSETS_BUCKET_NAME}

  150. None
  151. None
  152. Symlinking bug :(

  153. None
  154. None
  155. None
  156. None
  157. %env(ASSETS_BUCKET_NAME)%

  158. refresh… :)

  159. None
  160. got kicked, lol

  161. Lambda function JS Handler PHP Binary PSR-7 API Gateway Data

    Store
  162. Lambda function JS Handler PHP Binary PSR-7 API Gateway Data

    Store Sessions
  163. Lambda function JS Handler PHP Binary PSR-7 API Gateway Data

    Store JS Handler PHP Binary PSR-7 Sessions Sessions
  164. Lambda function JS Handler PHP Binary PSR-7 API Gateway Data

    Store JS Handler PHP Binary PSR-7 Sessions
  165. None
  166. None
  167. None
  168. None
  169. None
  170. None
  171. None
  172. None
  173. None
  174. migrations worked!

  175. where are my images?

  176. Lambda function JS Handler PHP Binary PSR-7 API Gateway Data

    Store
  177. Lambda function JS Handler PHP Binary PSR-7 API Gateway Data

    Store
  178. Lambda function JS Handler PHP Binary PSR-7 API Gateway Data

    Store File Store
  179. original value

  180. point to CDN instead

  181. None
  182. None
  183. create a global twig variable

  184. None
  185. pretty kitties!

  186. Uploading new images

  187. Use pre-signed URLs

  188. Client Server Amazon S3

  189. Client Server Amazon S3 Get Pre-Signed URL

  190. Client Server Amazon S3 Get Pre-Signed URL Get Pre-Signed URL

  191. Client Server Amazon S3 Get Pre-Signed URL Get Pre-Signed URL

    One-time use URL
  192. Client Server Amazon S3 Get Pre-Signed URL Get Pre-Signed URL

    One-time use URL One-time use URL
  193. Client Server Amazon S3 Get Pre-Signed URL Get Pre-Signed URL

    One-time use URL One-time use URL PUT file to One-time use URL 200 OK
  194. Client Server Amazon S3 Get Pre-Signed URL Get Pre-Signed URL

    One-time use URL One-time use URL PUT file to One-time use URL 200 OK Update record
  195. Client Server Amazon S3 Get Pre-Signed URL Get Pre-Signed URL

    One-time use URL One-time use URL PUT file to One-time use URL 200 OK Update record Delete previous file
  196. we can delete all of this

  197. and this

  198. None
  199. None
  200. we only want /uploads to be writeable

  201. let’s re-create this in yaml

  202. None
  203. None
  204. None
  205. None
  206. None
  207. None
  208. None
  209. None
  210. None
  211. None
  212. • Added Bref to an existing site • Fixed assets

    by moving them to a CDN • Added serverless-s3-uploads plugin • Configured CORS & bucket security in serverless.yml • Moved session storage to the database • Used pre-signed URLs for image uploading
  213. That’s really cool, but I need to run cron tasks

    in PHP
  214. None
  215. None
  216. None
  217. None
  218. { “cli”: “app:dump-lambda — message=Hello” }

  219. None
  220. None
  221. But my site isn’t Symfony 4!

  222. None
  223. None
  224. Performance

  225. source: https://github.com/mnapoli/bref-benchmark JS vs PHP

  226. None
  227. Lambda function JS Handler PHP Binary PSR-7 API Gateway Data

    Store VPC
  228. source: https://medium.freecodecamp.org/lambda-vpc-cold-starts-a-latency-killer-5408323278dd

  229. source: https://medium.freecodecamp.org/lambda-vpc-cold-starts-a-latency-killer-5408323278dd

  230. source: https://medium.freecodecamp.org/lambda-vpc-cold-starts-a-latency-killer-5408323278dd keep VPCs warm

  231. Recap

  232. • Setting up a new ‘Hello World’ SF 4 site

    • Setting up an Api-Platform site with database migrations • Converting an existing site to Lambda • Retro-fitting to an older Symfony site • Cron tasks • Performance
  233. Win a Nintendo Switch Enter our competition at symfony2018.mybuilder.co.uk

  234. joind.in/talk/02a33 symfony2018.mybuilder.co.uk