In traditional web application development, we usually run our code in single or multiple server to serve our end user. But in serverless architecture, we don’t have to spin up any server anymore, all we need to ship our code to a particular platform (think AWS Lambda) and they will execute it. Its seems like we don’t need any server anymore to serve our web application, but under the hood the platform provider (think AWS) will provision and manage your server on behalf of you.

In this article, we are going to create a serverless web application that will simply send email and to do that we are going to use

AWS Lambda, is an event-driven, serverless computing platform provided by Amazon as a part of the Amazon Web Services. It is a computing service that runs code in response to events and automatically scale up and out resources as per the traffic of our application.

AWS Simple Email Service(SES) is a cloud-based email sending service provided by AWS.

Chalice a microframework for writing serverless apps in python for AWS Lambda. It will simplify and help us to quickly create and deploy applications in AWS Lambda. It will also help you to manage your dependencies on production environment.

Now lets get into the coding part.

First thing first, create and activate virtual environment. I’m going to use virtualenv for my dependency management but you can use whatever you want (like pipenv ).

$ pip install virtualenv $ virtualenv -p python3 venv $ source venv/bin/activate

Now Install Chalice and some other third party packages that we are going to use through out this article.

(venv)$ pip install chalice (venv)$ pip install boto3

Since we now have Chalice installed inside our virtual environment lets create a Chalice Project by executing following command.

(venv)$ chalice new-project demo_chalice

This will create demo_chalice directory. Inside this directory we will have some boilerplate code with following directory structure.

demo_chalice ├── .chalice [Chalice will use this directory to store meta information in json format for deployment] └── config.json ├── app.py [We will mainly write our code inside the module] └── requirements.txt [specify all the python libraries that are need to run your app]

Now the project has been created successfully, so lets write our code to send email using AWS SES and Chalice. First, modify our app.py file with following code:

from chalice import Chalice, Response import boto3 from botocore.exceptions import ClientError app = Chalice(app_name='demo_chalice') @app.route('/') def index(): # This address must be verified with Amazon SES. SENDER = "sender@test.com" # If your SES account is still in the sandbox, this address need to be verified as well. RECIPIENT = "receiver@test.com" # Specify the Region you're using for Amazon SES. AWS_REGION = "us-east-1" # The subject line for the email. SUBJECT = "Amazon SES Test (SDK for Python)" # The email body for recipients with non-HTML email clients. You could've use HTML instead plain text as well. BODY_TEXT = ("Amazon SES Test (Python)\r

" "This email was sent with Amazon SES using the " "AWS SDK for Python (Boto)." ) # The character encoding for the email body. CHARSET = "UTF-8" # Create a new SES resource and specify a region. client = boto3.client('ses',region_name=AWS_REGION) try: response = client.send_email( Destination={ 'ToAddresses': [ RECIPIENT, ], }, Message={ 'Body': { 'Text': { 'Charset': CHARSET, 'Data': BODY_TEXT, } }, 'Subject': { 'Charset': CHARSET, 'Data': SUBJECT, }, }, Source=SENDER, ) except ClientError as e: return {'status':'fail' + str(e.response['Error']['Message'])} else: return {'status':'success'}

Build & Run Locally

To run your app on your local machine and verify whether our above snippet is working properly or not by executing following command.

(venv)$ chalice local

By default, Chalice runs on port 8000, so if you put http://127.0.0.1:8000/ address on your browser then you will get a success message if everything goes fine. Or you can even execute following curl command in your terminal.

$ curl -X GET http://127.0.0.1:8000/

Deploy on AWS Lambda

This is the area where Chalice helps us most, it makes deploying our serverless app completely effortless. We usually write our code locally then copy and paste it to Lambda and configure API Gateway accordingly in order to make it accessible via REST API but with Chalice we don’t have to do it manually anymore. Chalice will install all of your 3rd party python packages, deploy it to AWS lambda and configure API Gateway.

Before you deploy an application, be sure you have credentials configured in place. If you have previously configured your machine then you can skip this section. But if this is your first time configuring credentials for AWS you can follow these steps to quickly get started:

$ mkdir ~/.aws $ nano ~/.aws/config

Now modify config file with following line with appropriate data.

[default] aws_access_key_id=YOUR_ACCESS_KEY_HERE aws_secret_access_key=YOUR_SECRET_ACCESS_KEY region=YOUR_REGION (such as us-west-2, us-west-1, etc)

With AWS credentials in place, let’s begin our deployment process by executing following command:

(venv)$ chalice deploy

You now have an API up and running using API Gateway and Lambda. To verify simply make a curl request on the generated Rest API URL:

$ curl -X GET https://generated_url

To learn more about Chalice, please refer to the following link.

[1] https://github.com/aws/chalice/