Introducing the CDK construct library for the serverless LAMP stack
Description
In this post, you learn how the new CDK construct library for the serverless LAMP stack is helping developers build serverless PHP applications.
The AWS Cloud Development Kit (AWS CDK) is an open source software development framework for defining cloud application resources in code. It allows developers to define their infrastructure in familiar programming languages such as TypeScript, Python, C# or Java. Developers benefit from the features those languages provide such as Interfaces, Generics, Inheritance, and Method Access Modifiers. The AWS Construct Library provides a broad set of modules that expose APIs for defining AWS resources in CDK applications.
The “Serverless LAMP stack” blog series provides best practices, code examples and deep dives into many serverless concepts and demonstrates how these are applied to PHP applications. It also highlights valuable contributions from the community to help spark inspiration for PHP developers.
Each component of this serverless LAMP stack is explained in detail in the blog post series:
- Part 1: Introducing the serverless LAMP stack
- Part 2: Scaling relational databases
- Part 3: Replacing the web server
- Part 4: Deploying a serverless Laravel app
The CDK construct library for the serverless LAMP stack is an abstraction created by AWS Developer Advocate, Pahud Hsieh. It offers a single high-level component for defining all resources that make up the serverless LAMP stack.

CDK construct for Serverless LAMP stack
- Amazon API Gateway HTTP API.
- AWS Lambda with Bref-FPM runtime.
- Amazon Aurora for MySQL database cluster with Amazon RDS Proxy enabled.
Why build PHP applications with AWS CDK constructs?
Building complex web applications from scratch is a time-consuming process. PHP frameworks such as Laravel and Symfony provide a structured and standardized way to build web applications. Using templates and generic components helps reduce overall development effort. Using a serverless approach helps to address some of the traditional LAMP stack challenges of scalability and infrastructure management. Defining these resources with the AWS CDK construct library allows developers to apply the same framework principles to infrastructure as code.
The AWS CDK enables fast and easy onboarding for new developers. In addition to improved readability through reduced codebase size, PHP developers can use their existing skills and tools to build cloud infrastructure. Familiar concepts such as objects, loops, and conditions help to reduce cognitive overhead. Defining the LAMP stack infrastructure for your PHP application within the same codebase reduces context switching and streamlines the provisioning process. Connect CDK constructs to deploy a serverless LAMP infrastructure quickly with minimal code.
Code is a liability and with the AWS CDK you are applying the serverless first mindset to infra code by allowing others to create abstractions they maintain so you don’t need to. I always love deleting code
Says Matt Coulter, creator of CDK patterns – An open source resource for CDK based architecture patterns.
Building a serverless Laravel application with the ServerlessLaravel construct
The cdk-serverless-lamp
construct library is built with aws/jsii and published as npm and Python modules. The stack is deployed in either TypeScript or Python and includes the ServerlessLaravel
construct. This makes it easier for PHP developers to deploy a serverless Laravel application.
First, follow the “Working with the AWS CDK with in TypeScript“ steps to prepare the AWS CDK environment for TypeScript.
Deploy the serverless LAMP stack with the following steps:
- Confirm the CDK CLI instillation:
$ cdk –version
- Create a new Laravel project with AWS CDK:
$ mkdir serverless-lamp && cd serverless-lamp
- Create directories for AWS CDK and Laravel project:
$ mkdir cdk codebase
- Create the new Laravel project with docker
$ docker run --rm -ti \
--volume $PWD:/app \
composer create-project --prefer-dist laravel/laravel ./codebase
The cdk-serverless-lamp
construct library uses the bref-FPM custom runtime to run PHP code in a Lambda function. The bref runtime performs similar functionality to Apache or NGINX by forwarding HTTP requests through the FastCGI protocol. This process is explained in detail in “The Serverless LAMP stack part 3: Replacing the web server”. In addition to this, a bref package named larval-bridge automatically configures Laravel to work on Lambda. This saves the developer from having to manually implement some of the configurations detailed in “The serverless LAMP stack part 4: Building a serverless Laravel application”
- Install bref/bref and bref/laravel-bridge packages in the vendor directories:
$ cd codebase
$ docker run --rm -ti \
--volume $PWD:/app \
composer require bref/bref bref/laravel-bridge - Initialize the AWS CDK project with typescript.
$ cd ../cdk
$ cdk init -l typescript - Install the cdk-severless-lamp npm module
$ yarn add cdk-serverless-lamp
This creates the following directory structure:
.
├── cdk
└── codebase
The cdk
directory contains the AWS CDK resource definitions. The codebase directory contains the Laravel project.
Building a Laravel Project with the AWS CDK
Replace the contents of ./lib/cdk-stack.ts
with:
import * as cdk from '@aws-cdk/core';
import * as path from 'path';
import { ServerlessLaravel } from 'cdk-serverless-lamp';
export class CdkStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new ServerlessLaravel(this, 'ServerlessLaravel', {
brefLayerVersion: 'arn:aws:lambda:us-east-1:20 9497400698:layer:php-74-fpm:12',
laravelPath: path.join(__dirname, '../../codebase'),
});
}
}
The brefLayerVersion
argument refers to the AWS Lambda layer version ARN of the Bref PHP runtime. Select the correct ARN and corresponding Region from the bref website. This example deploys the stack into the us-east-1 Region with the corresponding Lambda layer version ARN for the Region.
- Deploy the stack:
cdk deploy
Once the deployment is complete, an Amazon AP