💡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. 🔥
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
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.
$ symfony new --full aws-lambda-linkedin-notifier
$ cd aws-lambda-linkedin-notifier
$ composer require eniams/linkedin-notifier
<?php
// config/bundles.php
return [
// others bundles,
EniamsNotifierLinkedInLinkedInNotifierBundle::class => ['all' => true]];
// .env
LINKEDIN_DSN=
The logic is simple, we expose an API in the route /content
that 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 :
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.
Update the kernel.php for handle logs
$ npm install -g serverless
$ serverless config credentials --provider aws --key --secret
$ composer require bref/bref
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 with a curl request to our API that contains our message in the body !
Youhou !! You can see the posted message here
That’s all ! You can find the whole source code here, I hope you liked it, to share it 👏🏻👏🏼👏🏽👏🏾👏🏿