Dynamic HTML with Python, AWS Lambda, and Containers

Deploying AWS Lambda using a container

Dockerfile

FROM public.ecr.aws/lambda/python:3.8
RUN mkdir -p /mnt/app
ADD app.py /mnt/app
ADD index.html /mnt/app
WORKDIR /mnt/app
RUN pip install --upgrade pip
RUN pip install Jinja2==2.11.*
CMD ["/mnt/app/app.handler"]

App code

import os
from jinja2 import Environment, FileSystemLoader
def handler(event, context):
env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), "."), encoding="utf8"))
my_name_from_query = False
if event["queryStringParameters"] and "my_name" in event["queryStringParameters"]:
my_name_from_query = event["queryStringParameters"]["my_name"]
template = env.get_template("index.html")
html = template.render(
my_name=my_name_from_query
)
return {
"statusCode": 200,
"body": html,
"headers": {
"Content-Type": "text/html",
}
}
index.html

Calling and testing the app locally

docker-compose -f docker-compose.yml up
version: '3'
services:
cont_name:
container_name: cont_name
image: cont_name_img
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/mnt/app
ports:
- "9000:8080"
stdin_open: true
tty: true
restart: always
import requests
r = requests.get(
"http://localhost:9000/2015-03-31/functions/function/invocations",
data=open("event.json", "rb")
)
print(r.json())
{
"queryStringParameters": {
"my_name": "Adam"
}
}

Cost

Lambda duration

--

--

--

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

Recommended from Medium

What’s the Use of Pi Breadboard Breakout? & Where to Buy in UK?

What’s the Use of Pi Breadboard Breakout? & Where to Buy in UK?

Uno Re Joins Berry Club

What even is a framework?

Memos, Caches and Coins

Responsive HTML Tables

A Guide to Monitoring Servers/Services with Nagios — Part 2

Optimizing docker images for a faster development workflow

Getting Hardcore — Creating a Repo From Scratch

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

FOSS and learning Linux CLI: An early experience and just how it all started!

5 Commands Line Every Programmer Should Know

How to debug Flask (running in docker-compose) in VS Code

[Tracebacks] Find some Common Python Traceback Error with Possible Fix

Common Python Traceback Error