Skip to main content

Serverless functions with Python & AWS Lamda

Serverless Computing is a cloud computing execution model in which the cloud provider acts as the server, dynamically managing the allocation of machine resources. Prices is based on the actual amount of resources consumed by an application. Serverless applications are event-driven cloud based systems where application development relies solely on a combination of third-party services, client-side logic and cloud-hosted remote procedure callys (FaaS - Functions as a Service).

In this post, we will be setting-up and creating a basic Serverless Function using Serverless Framework & AWS Lamda.

Serverless Setup :-

To start with, we need to install Serverless Framework. Now, we can write Serverless functions directly in AWS Lamda but Serverless Framework makes things more easy for setup especially when you have to work with other services from AWS like API Gateway. There are other frameworks available in market for deploying Python serverless functions like Zappa.

Serverless Framework is written in Node.js, so you need to have npm installed on your system to install it.

npm install -g serverless

Setting AWS Account :-

Next step is to setup the AWS account and configure the credentials with Serverless Framework. In order to proceed, you need to sign-up with Amazon for AWS account.
Once you sign-up for AWS account, follow the steps below :-
  • Login into AWS Console
  • Find the Service "IAM" (Use the "Find Service" option from the console)
  • In the IAM Dashboard, click on Users
  • Click on "Add User"
  • Use any username of your choice and select the "Programatic Access" check-box in the access-type  pane. 
  • Click Next to move to Permissions tab and select "Add existing policies directly
  • Select "AdministrationAccess" check-box to assign administration permissions for this user
  • Click Next to create the user
Note the Access Key Id and Secret Access Key, we will need this to configure the Serverless Framework.

Configuring Serverless Framework with AWS Credentials :-

In this step, we will configure the Serverless Framework with the following command :-

serverless config credentials --provider aws --key <ACCESS KEY ID> --secret <SECRET KEY>

Creating Serverless Project :-

 Use the following command to create a new Serverless Project with Python3 as language :-

serverless create --template aws-python3

This command will create two files - a configuration file serverless.yml and a handler.py where we will define handlers for our lamda functions.

Replace the contents of these files as below (template project will have available configuration options commented and can be used as per your project need).

In our project, we are setting a cron as an event to trigger the lamda function. So, we are calling hello function every five minutes. Print statement in handler function will dump the message in AWS logs, which can be accessed using AWS Cloudwatch. In case, we use the HTTP GET event, then we will get the message returned from our function.

Serverless.yml 

service: aws-python3

provider:
  name: aws
  runtime: python3.7

functions:
  hello:
    handler: handler.hello
    events:
      - schedule:
          rate: cron(*/5 * * * ? *)
          enabled: true

Handler.py

from __future__ import print_function

def hello(event, context):
    print('This message will be dumped in Cloudwatch Logs')
    return {
        "message": "Hello World",
        "event": event
    }

Deployment and Test :-

Deploy the project using the following command :-

serverless deploy

In order to check the log files for the message that we are writing every five minutes, use the following command :-

serverless logs --function hello --tail

You can stop this cron by setting the enabled flag to false and re-deploying the project or use the "serverless remove" command to remove the project altogether.

You can also access the AWS Console to see the deployed functions & logs using AWS Lamda Service and AWS Cloudwatch.

Note : If you do not see the functions deployed in AWS Lamda dashboard, make sure the region setting is same in the AWS console as set in Serverless Framework (default is US-East).

Comments