💡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 🔧
Code production 💻
$ symfony new --full aws-lambda-linkedin-notifier
$ cd aws-lambda-linkedin-notifier
$ composer require eniams/linkedin-notifier
Enable the bridge (see documentation)
// config/bundles.phpreturn [
// others bundles,Eniams\Notifier\LinkedIn\LinkedInNotifierBundle::class => ['all' => true]];// .env
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!
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
$ 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...
POST - https://xxx.execute-api.eu-west-3.amazonaws.com/dev/contents
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