The Serverless Framework – Builds applications on AWS Lambda and other next-gen cloud services, that auto-scale and only charge you when they run. This lowers the total cost of running and operating your apps, enabling you to build more and manage less.
The Serverless Framework is a command-line tool that uses easy and approachable YAML syntax to deploy both your code and cloud infrastructure needed to make tons of serverless application use-cases. It's a multi-language framework that supports Node.js, Typescript, Python, Go, Java, and more. It's also completely extensible via over 1,000 plugins that can add more serverless use-cases and workflows to the Framework.
Actively maintained by Serverless Inc.
nvm is a version manager for node.js, designed to be installed per-user, and invoked per-shell. nvm
works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.
npm install -g serverless
Here we specified that we will use aws-nodejs
as a template, which will be stored in ~/Mentorship/
, and the name of our project is SLSFunc
. handler.js
- has our JavaScript code. The Lambda function handler is the method in your function code that processes events. When your function is invoked, Lambda runs the handler method. When the handler exits or returns a response, it becomes available to handle another event. serverless.yml
- the main config file that defines your functions, events and resources. For full config options, check the docs.
When you run serverless deploy
, two steps are run behind the scenes. Serverless Framework first runs serverless package
to package your code and build a CloudFormation template. By default, the artifact is generated in the .serverless/
directory of your project root with the following contents:
- A zip file with the Lambda code for your service. Or a zip file for each Lambda function in your service if individual packaging is turned on.
- A CloudFormation template file that describes the resources defined in your
serverless.yml
. - A
serverless-state.json
file that is used internally by Serverless Framework.
After the artifact is generated, Serverless then deploys this package by:
- Uploading the zip file to S3.
- Updating the CloudFormation template with S3 paths for the Lambda zip files.
- Submitting the template to CloudFormation to kick start the deployment.
We can trigger Lambda functions from the CLI with the following syntax:
sls invoke -f <$FUNCTION> --aws-profile <$PROFILEE>
we can also test it locally before deploying within our environment:
sls invoke local -f <$FUNCTION>
We can read Cloudwatch logs from the CLI with the following syntax:
sls logs -f <$FUNCTION> --aws-profile <$PROFILE>
CloudWatch check:
To modify the code we can use pretty much any text editor, in this case, I am just going to do that using nano.
To update the code we are going to deploy again and invoke the function to see its output.
To confirm that our code is successfully modified and updated, we can also check it on CloudWatch.
After we successfully created the environment, installed Serverless Framework to create our project, deployed the code, invoked the request, read logs using the serverless CLI and CloudWatch - it is time to clean up!