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

Things I learned in 2021 as a software developer

Lock Statistics: Diagnose performance issues in Cloud Spanner

This Is Not True For Software Engineers

Metal as a Service: Intro to MAAS provisioning tool

Here are 4 Ways SRE Helps New Employees Onboard

1 Month Across the Tides

2nd bounty from facebook($XXX).

Fixing the Problem of Software Comprehension

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

DATABASE IN PYTHON

Python or Filtering in Queries: sorting JSON responses from an Oracle Database

URI with filtering in query from ORDS REST API enabled table, Chris Hoina, ORDS, Database Tools, Oracle Autonomous Database

Simple Approach to Error Handling in Python (Application in GUI Programming)

Django ORM(Object–relational mapping) ‘OR’ and ‘AND’ query