Skip to content

Fabiokleis/python_lambda_function_template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lambda-template

python lambda function template

Requirements

Install docker, jq, curl, build-essential etc...

Template lambda function

## lambda function

import logging

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)


# function called in Dockerfile
def lambda_handler(event, context):
    logger.info('lambda handler invoked')
    return {'message': 'Hello, World!'}

Docker image

used image dockerhub

FROM public.ecr.aws/lambda/python:3.9

COPY requirements.txt .
RUN  pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"

COPY . ${LAMBDA_TASK_ROOT}

CMD [ "lambda_function.__main__.lambda_handler" ] 

Build image

docker build . -t lambda-function

Run container

docker run --name python-lambda -d -p 9000:8080 lambda-function

Testing

Invoke lambda function

curl -s -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}' | jq

Makefile

all: build up test
.PHONY: all

Build image

make build

Run container

make up

Invoke lambda function

make test

Remove container and image

make clean