From 0883dd6c37dee8f4b2a06dddc0b1f4943fcd5712 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Thu, 24 Aug 2023 18:21:06 +0100 Subject: [PATCH 1/2] Adding doumentation on lambda emulation locally --- backend/README.md | 22 +++++++++++++++++++++- run_lambda_local.sh | 8 ++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/backend/README.md b/backend/README.md index 4d5a68ef..439618c0 100644 --- a/backend/README.md +++ b/backend/README.md @@ -120,7 +120,27 @@ Now you can run the script with ./run_lambda_local.sh ``` -Then run the script with the following command: +In order to make a request to it, there is a specific format the request must be in, to +emuate lambda. If using postman, the url you want is `http://localhost:8000/2015-03-31/functions/function/invocations` +and you need to pass a body like this: + +```json +{ + "httpMethod": "POST", + "body": "{\"portfolio_id\": 4, \"housing_type\": \"Private\", \"goal\": \"Increase EPC\", \"goal_value\": \"C\", \"trigger_file_path\": \"2/4/portfolio_plan_properties-20230724T093542483Z.csv\"}", + "path": "/v1/plan/trigger", + "resource": "/", + "headers": { + "Accept": "*/*", + "Content-Type": "application/json", + "Authorization": "Bearer YOUR_TOKEN_HERE", + "x-api-key": "YOUR_API_KEY_HERE" + }, + "requestContext": {}, + "multiValueQueryStringParameters": null +} + +``` ```commandline diff --git a/run_lambda_local.sh b/run_lambda_local.sh index b6bba656..abcd5633 100755 --- a/run_lambda_local.sh +++ b/run_lambda_local.sh @@ -4,11 +4,11 @@ IMAGE_NAME="fastapi-lambda-image" TAG="test" PORT="8000" -RIE_PATH="~/.aws-lambda-rie" +RIE_DIR="$HOME/.aws-lambda-rie" DOCKER_ENTRYPOINT="$RIE_PATH/aws-lambda-rie" DOCKER_CMD="/usr/local/bin/python -m awslambdaric backend.app.main.handler" ENV_FILE_PATH="backend/.env" -AWS_CREDENTIALS_PATH="~/.aws/credentials" +AWS_CREDENTIALS_PATH="$HOME/.aws/credentials" # Step 1: Download the AWS Lambda Runtime Interface Emulator if it doesn't exist if [ ! -f "$RIE_PATH/aws-lambda-rie" ]; then @@ -27,9 +27,9 @@ echo "Starting the Docker container..." CONTAINER_ID=$(docker run -d \ --env-file $ENV_FILE_PATH \ -v $AWS_CREDENTIALS_PATH:/root/.aws/credentials \ - -v $RIE_PATH:/aws-lambda \ + -v "$RIE_DIR:/aws-lambda" \ -p $PORT:8080 \ - --entrypoint $DOCKER_ENTRYPOINT \ + --entrypoint "/aws-lambda/aws-lambda-rie" \ $IMAGE_NAME:$TAG $DOCKER_CMD) # Output information From 33a6587e7001849a40b5c05a57c6661f1a318a79 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Thu, 24 Aug 2023 20:12:37 +0100 Subject: [PATCH 2/2] revert back to x86 --- backend/docker/lambda.Dockerfile | 18 +++--------------- serverless.yml | 2 +- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/backend/docker/lambda.Dockerfile b/backend/docker/lambda.Dockerfile index 35c62260..db9bad40 100644 --- a/backend/docker/lambda.Dockerfile +++ b/backend/docker/lambda.Dockerfile @@ -3,7 +3,7 @@ # https://python-mip.readthedocs.io/en/latest/install.html # Pull base image -FROM arm64v8/python:3.10.12-slim-bullseye as build-image +FROM arm64v8/python:3.10.12-slim-buster as build-image # Set environment variables ENV PYTHONDONTWRITEBYTECODE 1 @@ -13,14 +13,7 @@ ENV PYTHONUNBUFFERED 1 WORKDIR /var/task/Model # Install system dependencies -RUN apt-get update && apt-get install -y make gcc g++ gfortran libgfortran-9-dev liblapack-dev libamd2 libcholmod3 \ - libmetis-dev libsuitesparse-dev libnauty2-dev git wget pkgconf - -# Compile CBC for arm64 -RUN mkdir -p ~/build && cd ~/build \ - && wget -nH https://raw.githubusercontent.com/coin-or/coinbrew/master/coinbrew \ - && bash coinbrew fetch Cbc@master --no-prompt \ - && bash coinbrew build Cbc@master --no-prompt --prefix=/home/root/prog/ --tests=none --enable-cbc-parallel --enable-relocatable +# RUN apt-get update && apt-get install -y netcat-openbsd # Install python dependencies COPY ./backend/requirements/base.txt ./backend/requirements/base.txt @@ -31,18 +24,13 @@ RUN pip install --upgrade pip \ RUN pip install awslambdaric # Second stage: "runtime-image" -FROM arm64v8/python:3.10.12-slim-bullseye +FROM arm64v8/python:3.10.12-slim-buster # Set work directory to the root of your project WORKDIR /var/task/Model # Copy the python dependencies from the build-image COPY --from=build-image /usr/local/lib/python3.10/site-packages/ /usr/local/lib/python3.10/site-packages/ -COPY --from=build-image /home/root/prog/lib/ /usr/local/lib/ - -# Set environment variable for mip to use custom CBC binaries -ENV PMIP_CBC_LIBRARY="/usr/local/lib/libCbcSolver.so" -ENV LD_LIBRARY_PATH="/usr/local/lib/":$LD_LIBRARY_PATH # Copy project files COPY ./backend/ ./backend diff --git a/serverless.yml b/serverless.yml index 163731cf..89eb666a 100644 --- a/serverless.yml +++ b/serverless.yml @@ -3,7 +3,7 @@ service: fastapi-lambda provider: name: aws region: eu-west-2 - architecture: arm64 + architecture: x86_64 environment: API_KEY: ${env:API_KEY} ENVIRONMENT: ${env:ENVIRONMENT}