Getting Started with AWS Lambda and PHP

Getting Started with AWS Lambda and PHP

Everything you need to know about running PHP on AWS Lambda with Bref — from local setup and serverless.yml configuration to database connections, queue workers, deployment pipelines, and real-world performance tuning.

Serverless computing has transformed how we build and deploy applications. AWS Lambda lets you run code without provisioning or managing servers — and yes, you can run PHP on it. Thanks to Bref, deploying Laravel and PHP applications to Lambda is straightforward.

In this guide, I'll walk you through everything: from understanding Lambda's execution model to deploying a full Laravel application with database connections, queue workers, and automated CI/CD.


Why Serverless PHP?

Traditional PHP hosting requires managing web servers, PHP-FPM processes, and scaling infrastructure. Serverless flips this model:

  • Zero server management — AWS handles the infrastructure
  • Auto-scaling — from zero to thousands of concurrent executions
  • Pay-per-use — you're billed only for actual execution time
  • Built-in high availability — Lambda runs across multiple availability zones

With Bref, deploying PHP to Lambda is as simple as deploying a Node.js function.

Matthieu Napoli, Bref Creator

Setting Up Bref

npm install -g serverless
composer require bref/bref bref/laravel-bridge
php artisan vendor:publish --tag=serverless-config

Here's a production-ready serverless.yml configuration:

service: my-laravel-app

provider:
  name: aws
  region: us-east-1
  runtime: provided.al2
  environment:
    APP_ENV: production
    CACHE_DRIVER: dynamodb
    SESSION_DRIVER: dynamodb
    QUEUE_CONNECTION: sqs

functions:
  web:
    handler: public/index.php
    runtime: php-83-fpm
    timeout: 28
    events:
      - httpApi: '*'

  artisan:
    handler: artisan
    runtime: php-83-console
    timeout: 720

  queue:
    handler: Bref\LaravelBridge\Queue\QueueHandler
    runtime: php-83
    timeout: 120
    events:
      - sqs:
          arn: !GetAtt Queue.Arn

Database Connections

Lambda functions run in a VPC-isolated environment. To connect to RDS:

  • Create an RDS instance in a private subnet
  • Configure Lambda's VPC settings in serverless.yml
  • Use RDS Proxy to manage connection pooling

Always use RDS Proxy with Lambda. Without it, each invocation opens a new database connection, which can exhaust your connection limit.


Handling Cold Starts

Cold starts are the most common concern. Strategies to minimize them:

  1. Provisioned Concurrency — Keep warm instances
  2. Optimize deployment package — Smaller = faster cold starts
  3. Use PHP 8.3 — Performance improvements each version
  4. Cache config and routes during deployment
functions:
  web:
    handler: public/index.php
    runtime: php-83-fpm
    provisionedConcurrency: 5

Deployment

composer install --no-dev --optimize-autoloader
php artisan config:cache
php artisan route:cache
php artisan view:cache
serverless deploy --stage production

Conclusion

Running PHP on AWS Lambda is production-ready. Key takeaways:

  • Use Bref as the bridge between PHP and Lambda
  • Cache everything — config, routes, views
  • Use RDS Proxy for database connections
  • Monitor cold starts and use provisioned concurrency
  • Automate deployments with CI/CD

Start small, deploy a simple endpoint, and gradually migrate your workloads.

Share This Article

Did you find this helpful?

💬 Comments

No comments yet. Be the first to share your thoughts!

Leave a Comment

Get In Touch

I'm always open to discussing new projects and opportunities.

Location Yassa/Douala, Cameroon
Availability Open for opportunities

Connect With Me

Send a Message

Have a project in mind? Let's talk about it.