
Show HN: LambdaPHP – Host any PHP website on AWS Lambda instantly - superasn
https://github.com/san-kumar/lambdaphp
======
superasn
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

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

~~~
joecot
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.

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

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

------
superasn
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

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

------
lemonberry
"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
;) 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...](https://aws.amazon.com/blogs/compute/scripting-languages-for-aws-
lambda-running-php-ruby-and-go/)

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

------
tylerjwilk00
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](https://github.com/san-
kumar/installer)

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

~~~
Phrodo_00
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.

~~~
fapjacks
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.

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

~~~
superasn
For 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
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.

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

~~~
CurleighBraces
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!

[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
I was wrong. From what I've read, port binding == no, sockets == yes. So
should be able to use fpm to achieve this.

~~~
superasn
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!

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

~~~
superasn
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?

~~~
Willson50
I 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
> 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...](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GuidelinesForItems.html#GuidelinesForItems.StoringInS3)

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

~~~
superasn
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/](https://aws.amazon.com/rds/aurora/serverless/)

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

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

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

------
DAlperin
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

~~~
superasn
That is handled by the LambdaPhp installer here:

[https://github.com/san-kumar/installer](https://github.com/san-
kumar/installer)

~~~
DAlperin
Aha! Thanks.

------
samcodes
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/](https://up.docs.apex.sh/)

~~~
superasn
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..

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

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

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

------
indigodaddy
Wouldn't this still incur S3 fees however?

