7 ms·
Show HN: LambdaPHP – Host any PHP website on AWS Lambda instantly
- superasn 9y agoThis was just a weekend project. Here are some examples of using LambdaPHP. All of these are hosted on AWS Lambda (i.e. serverlessly): - Static site https://www.lambdaphp.host https://www.lambdaphp.host - Handling POST data https://www.lambdaphp.host/examples/post.php https://www.lambdaphp.host/examples/post.php - Creating sessions using AWS DynamoDB https://www.lambdaphp.host/examples/session.php https://www.lambdaphp.host/examples/session.php - File IO on AWS lambda (using S3 wrapper) https://www.lambdaphp.host/examples/guestbook.php https://www.lambdaphp.host/examples/guestbook.php - Simple File uploader to AWS S3 https://www.lambdaphp.host/examples/upload.php https://www.lambdaphp.host/examples/upload.php - User signup and login using AWS Cognito https://www.lambdaphp.host/examples/auth/ https://www.lambdaphp.host/examples/auth/ - Using Custom domains with https support https://www.lambdaphp.host/ https://www.lambdaphp.host/
- WalterGR 9y agoIf this post to HN gets your example pages some traffic, it'd be awesome if you shared the stats and performance metrics available to you.
- tyingq 9y agoWhat do the page response times look like, since it seems like the included php opcache wouldn’t function, nor would user data caches like apcu?
- superasn 9y agoFor a practical example, you can try speed test on this page: https://www.lambdaphp.host/ https://www.lambdaphp.host/ (hosted with AWS Lambda). It does use external CDN for CSS framework (cdn.rawgit.com) so you will have to factor loading time for that as well[1]. Yes, PHP opcache and optimizations don't work since it gets cold started every time. [1] Pingdom speed test results: https://i.imgur.com/9zg9l3g.png https://i.imgur.com/9zg9l3g.png
- fahrradflucht 9y agoWhy is that? Couldn't you keep php running for the lambda lifetime? That way only when the lambda cold starts there would be no cache.
- superasn 9y agoI'll try to research this. Another user suggested it to me before so maybe that's possible..
- mjb 9y agoI'm not sure how PHP integrates with common web servers today, because I've been pretty disconnected from that world for a decade, but at least you should be able to use the same mechanism to call the PHP runtime from your Node shim while keeping it alive. If PHP supports fastcgi, for example, there are modules in NPM that can do the Node side of it.
- CurleighBraces 9y agoIt would be cool if you could launch php-fpm but it doesn't look like lambda supports port binding/sockets. Seems like other people have had this idea in the past too! https://forums.aws.amazon.com/message.jspa?messageID=624889 https://forums.aws.amazon.com/message.jspa?messageID=624889 Are there any workarounds allowing something similar?
- CurleighBraces 9y agoI was wrong. From what I've read, port binding == no, sockets == yes. So should be able to use fpm to achieve this.
- superasn 9y agoThis is the best pointer I have recieved from anyone. A lot of people have told me to do it but nobody can tell me how to get started. This looks something worthwhile that I can research and implement. Thanks!
- superasn 9y agoFor the curious here is how it works (this is all setup for you automatically when you type lambdaphp deploy): - Your requests are sent to Amazon API Gateway - AWS API Gateway invokes the Lambda function (pretty standard stuff) - There is a NodeJS server running on Lambda that accepts incoming connections - If the request is for a static file, it just serves it directly. - If it is a php file, it sets up the environment vars ($_GET, $_POST, etc) and loads php-cgi and pipes the request to that and captures the output - There is a wrapper script in between that creates a File IO wrapper to intercept any file IO and writes them to S3 - Similarly there is another wrapper to intercept requests to $_SESSION that are written to dynamodb - It also loads the composer autoloader so it is possible to load any modules from the vendor directory
- deleted 9y ago[deleted]
- krapp 9y agoWhy is a NodeJS dependency necessary? PHP can handle and route requests just fine. Modern PHP even comes with its own server.
- superasn 9y agoIt's because Aws Lambda doesn't support PHP natively just yet[1]. Maybe if anyone from Amazon is reading this, please add support for PHP in Lambda too. It's still quite relevant and useful to a lot of users. [1] https://aws.amazon.com/lambda/faqs/ https://aws.amazon.com/lambda/faqs/
- joecot 9y agoAWS Lambda doesn't support php natively currently. It came out with Node support, and has since added Python, Java, and C#. Of them Node generally has the fastest startup time, so if you want to run an unsupported language (like PHP), you build a binary for that language, and then write a small node script to call it and handle the input and output.
- 9y ago
- jrickert 9y agoI'd be really curious to see how something like WordPress runs on this.
- dfabulich 9y agoOr any of the major PHP CMSes. Joomla, Drupal, Cake, MediaWiki...
- superasn 9y agoYes that is my next milestone too. Will try to combine it with "Amazon Aurora Serverless"[1] so that's it is possible to host a WordPress site 100% serverlessly and not pay anything or very less for low traffic blogs and websites. [1] https://aws.amazon.com/rds/aurora/serverless/ https://aws.amazon.com/rds/aurora/serverless/
- bcolflesh 9y agoThat's an intriguing idea, interested to see where this goes - thanks for posting about it!
- Willson50 9y agoGreat work! A couple things I'd like before I would use this: - Cloudfront support for static assets - JSON Web Tokens might be cheaper and faster for session storage. Using DynamoDB for user sessions severely limits the number of page loads per second without paying more for DynamoDB throughput.
- superasn 9y agoThanks. I guess if you're using cloudfront then it's best to use a seperate sub-domain and link to them directly because you don't want to redirect it through the Api gateway/lambda as it will add to the load time and exhaust your quota. You're right about JWT. It does seem like a better solution for handling sessions and I'm sure somebody must have already written the wrapper for it, so it's just a matter of integrating. Though with JWT is there a limit on the session data length? Because right now it can be anything. If iirc with Jwt the cookie contains the entire session data, right?
- Willson50 9y agoI found this JWT implementation which claims to replace $_SESSION https://github.com/byjg/jwt-session https://github.com/byjg/jwt-session Yea JWT maxes out around 8KB vs DynamoDB's 400KB Also, the CloudFront storage problem exists with many other serverless frameworks like Zappa so I don't blame you for not solving it yet haha.
- guitarbill 9y ago> This implementation save the JWT into a client cookie. Because of this do not store in the JWT Token sensible data like passwords. JWT is designed for authentication claims, and not to completely replace $_SESSION data, although technically you can stuff as much as you want into the payload. The wording makes me uneasy about the implementation - but decent advice otherwise. (Also note that getting JWT implementations/crypto right is somewhat tricky, obvs. Caveat emptor) On a quick note, storing big blobs of data in DynamoDB is asking for trouble, and AWS actually recommend S3 for this [0]. Best to not find this out the hard way (i.e. huge bill). [0] https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GuidelinesForItems.html#GuidelinesForItems.StoringInS3 https://docs.aws.amazon.com/amazondynamodb/latest/developerg...
- lemonberry 9y ago"This was just a weekend project for my own amusement but I will definitely add more features" I'm late to the programming party, started at 38 now 43, and mostly regular sites (though I am working on a couple webapps at the moment), but comments like this make me feel like taking my toys and going home. Nice work, can't wait to dig in and take a look at this both as a user and n00b dev. Congrats!
- CurleighBraces 9y ago;) This seems like magic at first glance but actually, it's not too much of a leap when you know how. In fact, there's a step by step guide on how to do something similar here: https://aws.amazon.com/blogs/compute/scripting-languages-for-aws-lambda-running-php-ruby-and-go/ https://aws.amazon.com/blogs/compute/scripting-languages-for... Hopefully, that'll give you some clues into understanding the magic behind this!
- is_true 9y agoDon't feel bad because of that. You are missing a key point, before doing this in a weekend OP probably spent at least a few months learning the technologies to make this happen.
- superasn 9y agoThanks for the kind words. There was a lot of trial and error involved I guess :) Also this was inspired by another project, so I can't take 100% credit. I don't know if I'm in a position to give advice to anyone, but if there is one thing I've learned about programming, it would be that the best and quickest way to learn something is by looking at other people's code. For creating lambdaphp this was super helpful too (esp the part about AWS roles and identity management) because even after reading their docs twice I couldn't figure out many things, but as soon as i saw code for it in another project on GitHub it was super easy to implement it in my own code too.
- indigodaddy 9y agoWouldn't this still incur S3 fees however?
- news_to_me 9y agoThis looks pretty cool, congrats! As an aside, does anyone know of a language that is as easy as PHP for programming beginners to jump into Web programming, but isn't PHP?
- krapp 9y agoHack, if being a fork of PHP counts as "not PHP."
- yvan 9y agoI would say Python
- jonmb 9y agoPython is the typical recommendation I see for beginner and able to use on Web.
- tracker1 9y agoPython, JS/Node, or ASP.Net/C# would be my suggestions. Each have their pluses and minuses.
- tylerjwilk00 9y agoThis begs the question, if you want something like PHP why not just use PHP7? Is the stigma that strong?
- pmontra 9y agoAnything interpeted (Node, Python, Ruby) but PHPs model is pretty unique. It loads a page and starts running code in it, possibly loading other code and other pages. With the other languages you usually run some code first and eventually generates the page. The result is the same but the infrastructure is very different. The only similar thing I used was asp pages. Are they still a thing?
- etaioinshrdlu 9y agoIt looks like this technique could work for most any old-school CGI language, like Perl too.
- mappu 9y agoThe Go runtime for Lambda allows arbitrary binaries, with a CGI-like-but-incompatible interface.
- samcodes 9y agoDid you look at Up by Apex for inspiration? Very similar framework, but for Lambda languages, any language that compiles to a binary, or Crystal: https://up.docs.apex.sh/ https://up.docs.apex.sh/
- superasn 9y agoNo but that looks amazing and much powerful. My focus was a bit different though. I just wanted to adapt Aws Lambda to run existing PHP code without any modifications, so LambdaPhp focuses on deployment but more on the shims that allow existing php projects to run seamlessly on the Lambda architecture, e.g. intercepting file io and redirecting it to s3, capturing sessions to dynamodb, etc..
- DAlperin 9y agoI am not a php guy so this is probably me missing something but I can understand where the source code that creates lamda functions etc is in the repo
- fapjacks 9y agoWhen cloud computing commoditized running your own hardware or datacenters, people started tightly-coupling their applications to these cloud providers. One of the great benefits of containerization is that it commoditizes the cloud providers, allowing you to decouple your service from them. I think "serverless" like Lambda is pretty obviously an attempt to "re-couple" these applications to the cloud provider in a sort of "de-commoditization". Interesting to watch.
- Phrodo_00 9y agoThat's a good point, and I think that it's cool that using this you can grab your website and dump it in any other web server running FPM or mod_php to stop using lambda.
- fapjacks 9y agoIt's interesting and brings up a yet-another-interesting consideration which is more broadly what is the "best" place to couple your applications/services to your provider... I suppose that's why I like containerization so much, because it decouples them fairly maximally from any kind of service provider (all you need is a network connection and some CPU to run them by, to sort of misquote something I heard once upon a time). You retain a lot of control with containerization, but without the overhead of a fully virtualized environment. It keeps time and money costs down, too, not having to couple into all those various AWS (or whatever) services. And I like being decoupled from those kinds of service providers because it also reduces the surface area of skills which are only useful with that service provider. Decouples expertise from a single point of failure, which is really important for me personally.
- tylerjwilk00 9y agoExcellent work superasn! For those curious the installer that this package depends on to do it's magic can been seen here [1] also by the same author. [1] https://github.com/san-kumar/installer https://github.com/san-kumar/installer
- crb002 9y agoAWS Lambda needs a generic C/Linux API instead of supporting every language out there.