Update: After receiving some feedback, that it is not obvious if you need to use Python 2 or 3 for this article, I decided, that I want to update it all the way for Python 3. AWS Lambda supports it, Zappa supports it and we all want Python 3!

I wanted to try out AWS Lambda for a while now. But it took some time to find a reasonable development task that fits this infrastructure. Serverless deployment is very intriguing, escpecially for someone like me that doesn’t enjoy to fiddle with servers, operating systems and stuff.

No, I won’t reveal any detals about my project yet. But I want to share my experiences building a simple webapp - the good old hello world example - and deploy it on AWS Lambda.

Lambda functions can be created using Python, node, Java and C# at the moment. (Take a look at the document about the supported execution environments.) Obviously my choice is Python.

And as I plan to deploy a whole web application and not just a simple function, I need some more incredients to build it.

Step 1: Set up the Python environment

I assume, that you are using macOS and Homebrew is installed on your development machine. Currently I am a big fan of pipenv to manage project dependencies and the virtual environment for projects.

Open a terminal and execute the following commands. Afterwards we have a nice and clean Python installation along with some essential tools.

# install python  
brew update  
brew install python3

# install and upgrade some essential packages  
pip install --upgrade pip setuptools pipenv

# optionally install some handy development tools  
pip install flake8 autopep8 ipython cookiecutter  

If you are running Windows or Linux, you need to configure the Python environment on your own.

Step 2: Setup the project and create the web application

In this step we will setup our new project and create the trivial web application. First, we have to execute some shell commands to create our new project.

# create an empty directory for our project  
mkdir lambda-hello-world  
cd lambda-hello-world

# Init the Pipfile and a virtual environment for the project.  
pipenv --three

# activate the virtual environment  
pipenv shell

# install Zappa and Flask along with the AWS command line tools  
pipenv install zappa flask  
pipenv install --dev awscli

# optionally install some handy development tools inside the virtual
pipenv install --dev flake8 autopep8 ipython  

Next we create a file named hello_world.py inside the directory lambda-hello-world with the following content. Nothing tricky, just a trivial Flask web application to return the string “Hello, world!”.

from flask import Flask

app = Flask(__name__)

def index():  
    return "Hello, world!", 200

# We only need this for local development.  
if __name__ == '__main__':  

Finally, we can fire up our local debug server and test our simple web application.

export FLASK_APP=hello_world.py  
flask run  

You can reach it by opening the url in your browser.

Step 3: Configure your AWS credentials and default region

Before we deploy our web application, we have to make sure we have a valid AWS account and our AWS credentials and default region is properly configured on the development machine.

All this can be done with the AWS Command Line Tools as described in the document Configure the AWS command line interface. Basically we just have to run the command aws configure in our project directory and fill in the access key id, the secret access key and the default region.

Now our AWS account is setup on our development machine and we can start to configure Zappa and deploy our application to AW.

Step 4: Configure Zappa for our project

So far we have a setup our project, added a simple web application and configured our AWS account. Now, we can prepare our project to be managed and deployed by Zappa. In order to do this, we just have to run the following command inside the project directory.

zappa init  

It creates a file named zappa_settings.json inside your project directory with the following content. That is all we need to deploy our app.

    "dev": {  
        "app_function": "hello_world.app",  
        "s3_bucket": "zappa-some-random-id",  
        "runtime": "python3.6"  

Of course Zappa offers a lot more configuration options for bigger projects.

Step 5: Deploy the web application

Last but not least, we can deploy the app and enjoy our simple, (extremly :))scalable “Hello , World!” application.

zappa deploy  

What’s next?

Well, now it is about time to built something useful with this awesome stack. I really like how you can built fast and scalable web apps which access  a lot of other AWS services like Rekognition, DynamoDB and so on. You can expect some more blog posts out my adventures in Lambda land in the near future.

Image credit: Qfamily. Shared under the Creative Commons Attribution 2.0 Generic license.