Serverless Python With AWS Lambda
Published on November 6, 2020

Using Python in serverless environments can be more automated than what I shared in my previous post using PHP and Bref. At AWS, there are already pre-existing environments making initial deployment easier without using additional tools. However, using tools like the Serverless framework still carries value.

This example will create an AWS Lambda Function for Python using Serverless. Then, we will add an API Gateway to make it available using an HTTP client or browser of your choosing.

Prerequisites

Here are things you will need to follow all examples:

AWS Console Approach

For basic function creation, the AWS Console can be used to navigate to the Lambda management area and click Create Function in the upper right. Following that, you will name and select the environment desired.

AWS Console Lambda Python Function CreationAWS Console Lambda Python Function Creation

Serverless Framework

Though the AWS Console can perform the essential tasks of creating a function, we will use the Serverless Framework going forward, which is a "complete solution for building and operating serverless applications." By using the Serverless framework, we gain scripts to automate any potential repetition of the process, as well as some starter code for immediate validation. Thus, our first step is to get this framework installed on the development system you're using. We recommend installing it globally, to allow usage from anywhere via CLI.

npm install -g serverless

AWS Credentials

With Serverless installed, ensure you've also set up the AWS credentials needed for Serverless to interact with the various AWS services (S3, Lambda, and potentially EC2 or databases as required). In this example, we will only be using S3 and Lambda. You can read more about this at serverless.com.

Create Base

Serverless will guide you through a few prompts to help it create a beginning structure. To do so, call Serverless from within the project directory via CLI.

serverless

Follow Prompts

Serverless will begin walking you through creating a new project, the type of project you want to create (Node.js, Python, or Other), a name for this project, and whether you would like to activate added Serverless services (requiring a Serverless account). Once completed, the base structure will generate.

AWS Lambda Python Project Creation With ServerlessAWS Lambda Python Project Creation With Serverless

Example YML File

As an example, below is a beginning serverless.yml file created by the process:

service: app

provider:
  name: aws
  runtime: python3.8

functions:
  hello:
    handler: handler.hello

In the file, observe the name of the service, which you can alter to fit the desired naming conventions of your functions. By default, it will carry the name you specified during project creation.

Following this, we can see the provider information for AWS Lambda with the Python runtime.

Then, there is the section outlining the details of the function. Included is the initial handler for testing both as a function or HTTP (which requires an added step shown later).

Deploying to AWS Lambda

For either Function or HTTP use cases, the default serverless project provides enough of a skeleton to deploy straight to AWS Lambda and test using Serverless. Leveraging the AWS credentials configuration added prior, Serverless will deploy straight to Lambda using the contents of serverless.yml as criteria.

serverless deploy

IMPORTANT: The example application, as-is, does not carry any authentication or verification. Anyone with access to the URL provided after deployment can access it. Doing so could cause unexpected charges to your accounts. Therefore, please secure the app if you intend to leave it active.

Testing the Function

In the case of a function, there are two ways to test. One way is to use the AWS Console, while the second is using Serverless via CLI from the system used to deploy.

AWS Console

In the AWS Console, select the Lambda item from the list of Compute services.

AWS Console-Lambda DashboardAWS Console-Lambda Dashboard

On the Lambda dashboard, you can select the function created and create a new test by clicking Configure test events. Alternatively, you can click the Test button to receive the same prompt.

Lambda Create TestLambda Create Test

For a test of the skeleton created, add a name and slightly alter the JSON in the body.

Configure Test EventsConfigure Test Events

Upon clicking the Create button at the bottom of the dialog, you can then click the Test button to execute the test. Running should result in a green dialog area you can expand to show the results.

Lambda Test ResultsLambda Test Results

Serverless Function Execution

As stated above, you can also use the Serverless CLI to test the function by using the following command from within the local application root:

serverless invoke -f function --data='{"name": "Adam"}'

Example result: Serverless Lambda Function ExecutionServerless Lambda Function Execution

Testing An HTTP Function

You can also test as an HTTP function. However, doing this will require the addition of an API Gateway trigger in the AWS Lambda Console. The trigger could have been specified in the serverless.yml as well, but for this basic example, it is good for you to know how to do it manually.

From within the Lambda function properties page, click Add Trigger in the Designer area. In the following prompt, select API Gateway from the list.

Add an API Gateway TriggerAdd an API Gateway Trigger

Next, define the trigger to Create a new API and select HTTP API before clicking the Add button.

Define the API Gateway TriggerDefine the API Gateway Trigger

After adding the API Gateway Trigger, it is now possible to request the URL provided by the trigger using an HTTP client or a standard browser.

Request the Function in a BrowserRequest the Function in a Browser

What Next

With an AWS Lambda function created, you can continue adding more code and creating more robust Python apps leveraging the serverless technologies available today. Watch for future posts showing how to build useful functions to leverage Vonage APIs and services using AWS Lambda.

Adam CulpVonage Alumni

Adam is a developer and consultant who enjoys ultra-running, blogging/vlogging, and helping others tame technology to accomplish amazing things with an insatiable desire to mentor and help.

Ready to start building?

Experience seamless connectivity, real-time messaging, and crystal-clear voice and video calls-all at your fingertips.