Serverless
Framework
Amazon AWS Lambda
Definition:
Build auto-scaling, pay-per-execution, event-driven apps on AWS Lambda
no charges if never called
Framework
comand line tool written in node.js
is multi-language and can be
used to deploy: python, java etc.
Installation
make sure you have node.js V6.6.0 installed
(to check it run: node -v)
run: npm install serverless -g
make a directory for your first serverless project
(run: mkdir restapi && cd restapi)
run the template creator
(run: serverless create -t aws-node.js > it creates a scaffold of your project)
to deploy it (make sure you hava AWS credetials)
then run: serverless deploy -r us-east-1 dev
then run: serverless deploy
Project structure
- handler.js
- event.json
- serverless-yml
handler.js
- contains the code of your functions
event.json
- whrere you keep your test data to trigger your functions
serverless.yml (most important file)
- defines your project functions and the events who triggers them.
- additionally you can define other infrastructure resources for you to use
with your functions like AWS dynamo, AWS Bucket and more. this yml takes it all
and deploys it as single unit. the serverless framework does it all behind the scenes for us.
Once updated project
- Use serverless deploy function -f hello
(it will just update your function) - we can log with:
serverless logs -f hello -t
Why use it?
The Serverless Framework is a core component of The Coca-Cola Company's initiative to reduce IT operational costs and deploy services faster
- Patrick Brandt, Solution Arquitect
Diving deeper
Function
independent unit of deployment
like a microservice. It is merely code deployed in the cloud, that is most often written to perform a single job such as:
- saving a user to the database
- processing a file in a database
- performing a scheduled task
Events
anything that triggers the functions. Events are:
- endpoint (e.g. REST API)
- AWS bucket upload (e.g. image)
- CloudWatch timer (e.g. run every 5 minutes)
- AWS SNS (e.g. a message)
- CloudWatch alert (e.g. something happened)
- and more...
Cool
When you create an event, the framework will automatically create the necessary infrastructure for that event and configure your function to listen for it.
Resources
are AWS infrastructure components that your function uses such as:
- An AWS DynamoDB Table (e.g., for saving Users/Posts/Comments data)
- An AWS S3 Bucket (e.g., for saving images or files)
- An AWS SNS Topic (e.g., for sending messages asynchronously)
- Anything that can be defined in CloudFormation is supported by the Serverless Framework
Services
It's where you define your functions,
events that trigger them, resources
your function use. serverless.yml