Deploy a Symfony ❤️ application in AWS Lambda 💭

Smaine Milianni
3 min readAug 8, 2020

(Accéder à la version francaise)

💡Before going more further we should know what is serverless and when we should use this architecture.

📚A serverless architecture helps you you to execute a piece of code without managing any kind of infrastructure, the cloud provider will take care of all the physical hardware, web server management… You just have to be focused on code, not on infrastructure nor administrative tasks.

AWS Lambda provides high availability and You pay only for the compute time you consume. It can be very useful for some tasks like CRON, send a notification in real-time, expose an API, deal with some events when an operation occurs… You can found more use cases on the web. 🔥

Our use case 🔧

Expose an API built with Symfony that post a message on LinkedIn, a developer journey is composed of successive task from code production to code deployment

Code production 💻

The version 5 of Symfony comes with a brand new component called “Notifier” that provides a way to sent notification trough different providers (Slack, Twitter, Twilio…)

Symfony does not provide a built-in provider for LinkedIn so few months ago I created a bridge to publish content on top of the Notifier, check the source code. We will use it in our demo.

Let’s code

$ symfony new --full aws-lambda-linkedin-notifier
$ cd aws-lambda-linkedin-notifier
$ composer require eniams/linkedin-notifier

Enable the bridge (see documentation)

<?php
// config/bundles.php
return [
// others bundles,
Eniams\Notifier\LinkedIn\LinkedInNotifierBundle::class => ['all' => true]];// .env
LINKEDIN_DSN=

Logic to publish content

The logic is simple, we expose an API in the route /contentthat should receive POST request with a message in the body.

Line 11, we send the posted message to LinkedIn, thanks to Symfony and the bridge it’s very easy!

As a professional developer this code is tested :

The code is ready! It’s Time to deploy 🚀

Whaaaat? AWS Lambda does not support PHP !

Yep, AWS Lambda does not support all programming language but few of them such like GO, Java, Python, Ruby, NodeJS, DotNet

Do we have to learn a new language and rewrite our code? Hopefully not!

Thanks to Matthieu Nappoli that created Bref.sh and the amazing team that help him to maintain this open source project. 💪🏽

Bref help us to deploy PHP applications to AWS and run them on AWS Lambda.

Deploy Configuration

Update the kernel.php for handle logs

Prepare the serverless (documentation)

$ npm install -g serverless
$ serverless config credentials --provider aws --key --secret
$ composer require bref/bref

Configure the serverless.yaml

Deploy !

$ serverless deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service notifier-linkedin-api.zip file to S3 (10.05 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
....................
Serverless: Stack update finished...
Service Information
service: notifier-linkedin-api
stage: dev
region: eu-west-3
stack: notifier-linkedin-api-dev
resources: 15
api keys:
None
endpoints:
POST - https://xxx.execute-api.eu-west-3.amazonaws.com/dev/contents
functions:
website: notifier-linkedin-api-dev-website
layers:
None
Serverless: Removing old service artifacts from S3...
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.

Now our code is deployed in AWS Lambda and the API is available at https://xxx.execute-api.eu-west-3.amazonaws.com/dev/contents

Let’s try

Result (see notification)

That’s all !

You can find the whole source code here, I hope you liked it, don’t forget to clap it and to share it 👏🏻👏🏼👏🏽👏🏾👏🏿

--

--

Smaine Milianni

Fullstack Developer- certified Symfony 4,5 and certified AWS Solution Architect - Freelancer - Remote Worker