Introduction
This article highlights what the Serverless Framework is and how to deploy a simple Python application on AWS Lambda using that Framework.
The YouTube Channels in both English (En) and French (Fr) are now accessible, feel free to subscribe by clicking here.
What Serverless Framework?
To define what a serverless framework is, we need to first understand the term serverless in itself. Serverless means the ability to have an application running in the cloud without the developer managing the underlying infrastructure such as updating or upgrading the server resources, as this is entirely handled by the cloud provider.
Serverless Framework is a NodeJS open-source web framework that helps in deploying serverless applications to main cloud providers such as AWS Lambda, Azure Functions, and Cloud functions. With Serverless Framework, we’re able to deploy applications, set up CI/CD, and provision cloud infrastructure all in one place with a simple YAML file.
Setting Up Dependencies
For us to configure and deploy the Python application on AWS lambda, we need to make sure we set our local environment by installing the following:
Node & NPM: Make sure you have the latest stable version of NodeJS installed. If not, download and install NodeJS from here
Serverless Framework: Install the serverless framework:
npm install -g serverless
Python: We need to make sure we have Python version 3.8+ installed
AWS CLI: You’ll also need to make sure you have AWS credentials set up correctly with AWS CLI on your local machine for the serverless framework to manage and provision resources for the deployment of the application.
Here is a comprehensive guide to configuring AWS CLI:
Create a Python Lambda Function using Serverless Framework
Quick Start Project Generation
As mentioned previously, we’re going to deploy a simple hello world API endpoint to demonstrate the usage of the serverless framework. To get started:
- Initialize the serverless framework by running the command:
serverless
When prompted, use the arrow down button to choose the type of application you want to deploy. In this scenario,
you’ll choose AWS - Python - HTTP API
and press Enter.
- Next, you will type in the name of the serverless project. In this case, you can call it python-api-demo
- Next, you will be prompted to What org you want to add this service to. You can skip this step.
Once that is done, the serverless framework will generate a template with the following files:
- .gitignore: Contains folder and file paths that won’t be pushed to GitHub.
- handler.py: A Python file containing the lambda function that handles events from API Gateway, and ALB.
- README.md: It’s the Markdown file with some instructions on executing your serverless project.
- Serverless.yml: Use the serverless.yml file to configure cloud resources. Learn more in the next section.
Configuring The Serverless.yml File
You’re almost set for deployment, but there is one last thing that needs to be updated. Remember, I previously mentioned that the Serverless Framework uses a YAML file to handle the management and provisioning of resources. So, for you to create the lambda function and API gateway endpoints, you’ll need to make some changes to the generated template serverless.yml file.
Let’s break down the content of the serverless.yml file:
- Service: This specifies the name of the serverless application you’re building.
- Framework Version: As the name implies, this is where the version of the serverless framework being used is defined.
- Provider: Here, you define what cloud provider you want to use. In this case, we’re using AWS, so it’s set to aws. Also, we want to specify the version of Python that will be used by the cloud provider to provision the correct environment corresponding to our application code.
- Function: In this section, you describe the lambda function that will be called via an API. In our use case, the function name will be python-api-demo. In the function section, you’ll also need to configure the handler, which is the entry point to the lambda function. The events section defines how the lambda function will be triggered, in our case, it is configured to be triggered by HTTP API events. Lastly, path: and method: define the endpoint’s URL path and the HTTP method.
Your final serverless.yml file code should be like the one below:
service: python-api-demo
frameworkVersion: '3'
provider:
name: aws
runtime: python3.9
functions:
api-demo:
handler: handler.hello
events:
- httpApi:
path: /
method: get
Sample Python Lambda Function
Here’s the content of the serverless function (handler.py file), it simply sends back the statusCode, the body, and the input of the request:
import json
def hello(event, context):
body = {
"message": "Hello Serverless Framework!",
"input": event,
}
response = {"statusCode": 200, "body": body}
return json.dumps(response)
Running The Python Application With Serverless Framework Locally
Serverless Framework lets you run and test serverless applications on your local machine, which is good for checking everything is working before deploying the application to the given cloud provider. For the demo you generated, run the serverless command below to start the application on your local machine:
serverless invoke local --function hello
The command above tells the serverless framework to invoke (run) the lambda function name hello
which is the function you generated in the template.
Deploying Python Lambda Function using Serverless Framework
When you’re done testing and everything works correctly as expected, you can now go ahead and deploy the Python lambda function to AWS with Serverless Framework.
The command below will create an API gateway endpoint and the lambda function to be invoked on AWS.
serverless deploy
You can visit the deployed serverless application by following the generated link in the output to the command above, something similar to the following:
https://xxxxxxxxxxx.execute-api.us-east-1.amazonaws.com/
Find below the screenshot of a GET response from the deployed application.
The full source code is available on GitHub.
Destroy a Lambda Function using Serverless Framework
The command "sls remove"
will destroy all the resources created as part of the demo above.
———————
We have just started our journey to build a network of professionals to grow even more our free knowledge-sharing community that’ll give you a chance to learn interesting things about topics like cloud computing, software development, and software architectures while keeping the door open to more opportunities.
Does this speak to you? If YES, feel free to Join our Discord Server to stay in touch with the community and be part of independently organized events.
———————
Conclusion
In this article, we discovered how to provision and deploy a simple API using the Serverless Framework. It’s a very powerful tool that can be used to provision other resources such as connecting a lambda function to DynamoDB, S3, SQS, and many more. Feel free to explore and learn more about the Serverless Framework in-depth via the official documentation.
Thanks for reading this article. Like, recommend, and share if you enjoyed it. Follow us on Facebook, Twitter, and LinkedIn for more content.