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

Do Not Repeat

Yet another post about usage ofAgile manifesto

Electrification Push Reveals Fatal Flaws in California’s Energy Code

Create Oracle cloud compute instance

Local Environments Have Become Impractical - Reasons Why QA Needs A New Approach

Kubernetes Services for Absolute Beginners — ClusterIP

I Fought the Code, and the Code Won

VOTE FEATURE TO MAKE BUNICORN BY THE COMMUNITY & FOR THE COMMUNITY

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

Django Multi-User Todo App with MySQL CRUD Ops

University Catalog V1

How to Run Docker Inside Docker?

Why You Should Use Python for Front-End Web Development