How to easily set up “serverless server” for IoT devices using Amazon AWS Lambda, Python, Flask & Zappa


Why Amazon AWS Lambda?

Before we begin…


Set up Work Environment

Amazon AWS Account & Credentials


Create Python Working Environment

# Create working directory
mkdir my-first-lambda-project
cd my-first-lambda-project
# Update pip and install virtualenv
python3 -m pip install --user --upgrade pip
python3 -m pip install --user virtualenv
# Create virtual environment and activate it
python3 -m venv <name_of_your_virtual_environment>
source <name_of_your_virtual_environment>/bin/activate
pip3 install awscli --upgrade
aws configure
pip install flask zappa

Create RESTful Web Services using Flask & Zappa

cd my-first-lambda-project
from flask import Flask
app = Flask(__name__)
def deviceId(deviceId):
return {'deviceId': deviceId}
if __name__ == '__main__':

Upload and Run Zappa on AWS Lambda

cd my-first-lambda-project
zappa init
zappa deploy dev
zappa update dev

REST API Testing

Option 1
"message": "deviceId-1"

Option 2

UI of the Restlet

Set up IoT device

Request info from AWS Lambda

pip install requests
import requests
import json
from requests.exceptions import HTTPError
URL = ''
API_GET_DEVICE = 'device'

def get_device(deviceId):
"""Request device information given deviceId
deviceId {string} -- requested information
responseJson [json] -- API request reponse in the JSON format
url = '{}/{}/{}'.format(URL, API_GET_DEVICE, deviceId)
response = requests.get(url)
responseJson = json.dumps(response.json()) response.raise_for_status() except HTTPError as http_err:
print('HTTP error occurred: {}'.format(http_err))
except Exception as err:
print('Other error occurred: {}'.format(err))

return responseJson
def main(): deviceId = 'deviceId-1'
responseJson = get_device(deviceId)
if __name__ == "__main__":


Process summary

Part 2

Thank you

Software Developer & Data Scientist

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store