Show HN: LambdaPHP – Host any PHP website on AWS Lambda instantly
github.com
github.com
- 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
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.
- Static site
- Handling POST data
https://www.lambdaphp.host/examples/post.php
- Creating sessions using AWS DynamoDB
https://www.lambdaphp.host/examples/session.php
- File IO on AWS lambda (using S3 wrapper)
https://www.lambdaphp.host/examples/guestbook.php
- Simple File uploader to AWS S3
https://www.lambdaphp.host/examples/upload.php
- User signup and login using AWS Cognito
https://www.lambdaphp.host/examples/auth/
- Using Custom domains with https support
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!
https://aws.amazon.com/blogs/compute/scripting-languages-for...
Hopefully, that'll give you some clues into understanding the magic behind this!
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.
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
Seems like other people have had this idea in the past too!
https://forums.aws.amazon.com/message.jspa?messageID=624889
Are there any workarounds allowing something similar?
https://s3.amazonaws.com/sanchit-uploads/guestbook.txt
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.
Anyway, this is pretty slick and snappy. Neat idea.
- 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.
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?
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.
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...
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?
The only similar thing I used was asp pages. Are they still a thing?