Serving Dynamic Web Pages using Python and AWS Lambda

AWS Lambda + Python + Jinja

Step 0 — Optional

FROM amazonlinux:latest
RUN mkdir -p /mnt/app
ADD . /mnt/app
WORKDIR /mnt/app
RUN yum update -y
RUN yum install gcc -y
RUN yum install gcc-c++ -y
RUN yum install findutils -y
RUN yum install zip -y
RUN amazon-linux-extras install python3=3.6.2
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt -t aws_layer/python
import lambda_function
event = {
"queryStringParameters": {
"param1": "value1"
"path": "/api",
"requestContext": {
"param2": "value2"
res = lambda_function.lambda_handler(event=event, context={})
assert 200 == int(res["statusCode"])

Step 1 — Write html template

Sample HTML page

Step 2 — Write Lambda function to serve your html page

import os
import sys
from jinja2 import Environment, FileSystemLoader
def lambda_handler(event, context):
env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates"), encoding="utf8"))
my_name = False
if event["queryStringParameters"] and "my_name" in event["queryStringParameters"]:
my_name_query = event["queryStringParameters"]["my_name"]
template = env.get_template("index.html")
html = template.render(
return response(html)
def response(myhtml):
return {
"statusCode": 200,
"body": myhtml,
"headers": {
"Content-Type": "text/html",
  • jinja2 loads your previously created index.html using class “FileSystemLoader” and we store it as variable “env”
  • variable “my_name” is parsed from the URL query parameters as explained above and stored as the Python variable my_name_query
  • the jinja2 render function then passes my_name_query to the template and returns the html page




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Linux Backup: 10 Best Solutions

Automatically track click events from Webflow in Google Analytics

How to enhance the digitalization of your company?

Building code from a Front-end point of view

Break Out of Tutorial Loops

Java Math pow() Method

Secure .NET CORE 6 Web API with JWT from Duente IdentityServer

Lawsuits and Trials

lawsuits and trials

Get the Medium app

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
Adam Novotny

Adam Novotny

More from Medium

A Brief Intro: Python and Django

JAVA program to print “Hello, World!”

How to Create a Sign-In/Sign-Up Page in Django with Authorization Configurations

Build any Kind of Website using Django. . .

What is a web API… and how can I create one with Python?