Running native PHP in AWS Lambda

Last year, i involved in agent commission system development like how insurance company or any other multilevel company does. End up, i stuck at a level where all big processes not optimized and not that stable.

Current configuration is using Google Cloud App Engine (GAE) with auto scaling. The main objective is to use big amount of RAM to complete all processes. By the time the commission process starts, GAE service will auto scale with CPU with matrix 20%. Means that, the service scalling once the CPU usage reach 20%. Just because it almost impossible to handle it in GAE properly as GAE Flex not allow zero minimum instance, i keep looking new solution until now.

Today, i found that Lambda can have 3GB RAM/15 minutes timeout to be used each invocation. Plus it able to run native PHP.

You can head straight to my repo or down here the summary what or how to do.

Optional Prerequisite Steps

1. Prepare Instance

prepare instance to compile/upgrade your php version
  • AWS > EC2 > Instances > Launch Instance > choose "Amazon Linux AMI XXXX.XX.X (HVM), SSD Volume Type"
  • SSH into the instance

2. Compile PHP

  • Choose your php version from
  • Modify accordingly (line 12-15)
  • Run $ sudo sh

Publishing Lambda Steps

1. Getting your AWS Credential

  • Top Right hand corner > click your account name > click 'My Security Credentials'
  • Click 'Access keys' > create and download

2. Configure AWS CLI

  • $ sudo pip install --upgrade awscli
  • $ aws configure > set AWSAccessKeyId and AWSSecretKey and region name and output format

3. Create Layers

  • $ cd ./php-example
  • $ sh

3. Create Functions

stay working inside the ./php-example
  • $ sh

4. Invoke Functions

stay working inside the ./php-example
  • $ sh
To see invocation output, open hello-output.txt and goodbye-output.txt
  • $ cat hello-ouput.txt
  • $ cat goodbye-output.txt

You also can refer official tutorial from aws

Soon i will reveal how to use lambda instead of using traditional Laravel Queue.  It is exciting 2019 for me!


Popular Posts