Hacker News new | comments | ask | show | jobs | submit login
Show HN: LambdaPHP – Host any PHP website on AWS Lambda instantly (github.com)
124 points by superasn 10 months ago | hide | past | web | favorite | 54 comments

For 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

Why is a NodeJS dependency necessary? PHP can handle and route requests just fine. Modern PHP even comes with its own server.

AWS 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.

Every measurement I have made of startup time puts Python 2.7 at about half the startup time of Node.js.

Now that Go is also supported it would be interesting to know what kind of start up time it has.

as sreque mentions, Python is a bit faster than node. Though both are much faster than the other options.

It'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/

Lamdba doesn't support PHP natively, so one of the natively supported options has to be used to bootstrap it.

This 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


- Handling POST data


- Creating sessions using AWS DynamoDB


- File IO on AWS lambda (using S3 wrapper)


- Simple File uploader to AWS S3


- User signup and login using AWS Cognito


- Using Custom domains with https support


If 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.

"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!

;) 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:


Hopefully, that'll give you some clues into understanding the magic behind this!

Don'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.

Thanks 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.

Excellent 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

When 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.

That'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.

It'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.

What 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?

For a practical example, you can try speed test on this page: 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

Why 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.

I'll try to research this. Another user suggested it to me before so maybe that's possible..

I'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.

It 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!


Are there any workarounds allowing something similar?

I was wrong. From what I've read, port binding == no, sockets == yes. So should be able to use fpm to achieve this.

This 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!

The guestbook demo resultant text file is a hilarious nightmare:


It looks like some Russian bots found your input. I know it's just a demo, but might want to add at least a honeypot on there or something.

Yes that was pretty quick. Right now it is all going to a text file (just wanted to demo that file_put/read_contents automatically writes to S3) but never expected this! God knows what they're doing with the file upload demo :/

Haha— tread carefully.

Anyway, this is pretty slick and snappy. Neat idea.

Great 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.

JWTs are really not a good idea for session storage. I wouldn't consider them secure by any stretch. ElastiCache would be a much better option, and relatively straightforward to configure.

Thanks. 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?

I found this JWT implementation which claims to replace $_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.

> 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/developerg...

I'd be really curious to see how something like WordPress runs on this.

Yes 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/

That's an intriguing idea, interested to see where this goes - thanks for posting about it!

Or any of the major PHP CMSes. Joomla, Drupal, Cake, MediaWiki...

This 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?

This begs the question, if you want something like PHP why not just use PHP7? Is the stigma that strong?

Python is the typical recommendation I see for beginner and able to use on Web.

Anything 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?

Hack, if being a fork of PHP counts as "not PHP."

Python, JS/Node, or ASP.Net/C# would be my suggestions. Each have their pluses and minuses.

I would say Python

I 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

That is handled by the LambdaPhp installer here:


Aha! Thanks.

Did 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/

No 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..

It looks like this technique could work for most any old-school CGI language, like Perl too.

The Go runtime for Lambda allows arbitrary binaries, with a CGI-like-but-incompatible interface.

AWS Lambda needs a generic C/Linux API instead of supporting every language out there.

Wouldn't this still incur S3 fees however?

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact