Merge pull request #148 from Hestia-Homes/main

removing CMD directive and specifying handler in serverless script
This commit is contained in:
KhalimCK 2023-08-24 13:40:37 +01:00 committed by GitHub
commit b555d162f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 8 deletions

View file

@ -11,3 +11,4 @@ open_uprn/*
land_registry/*
pytest.ini
*/README.md
utils/tests/*

View file

@ -59,7 +59,7 @@ FastAPI automatically generates interactive API documentation for your applicati
server and visit <yourlocalurl>/docs in your browser. Alternatively, you can go to
<yourlocalurl>/redoc to view the documentation in the ReDoc format.
## Building the backend docker image locally
## Building the lambda's backend docker image locally
To build the backend docker image locally, run the following command from the root of the project directory:
@ -79,6 +79,29 @@ To run a shell inside the Docker container to inspect its contents, run:
docker run -it fastapi-lambda-image:latest /bin/bash
```
Running in lambda results in running in a slightly different format compared to running the fastapi
application locally. If you want to run the fastapi application locally, in docker, we have a docker
file which builds the same environment as in lambda but runs the fast api application with uvicorn.
Run
```commandline
docker build -t fastapi-local-image:latest -f backend/docker/Dockerfile .
```
This will be the image. To run it, simply run
```commandline
docker run -p 8000:8000 -v ~/.aws:/root/.aws fastapi-local-image:latest
```
This assumes you have a ~/.aws folder with your aws credentials in it. If you don't have this, you can
run the following command with your aws access token exported into your environment.
```commandline
docker run -p 8000:8000 -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION fastapi-local-image:latest
```
## Testing
To run tests, run the following command from the root of the project directory:

View file

@ -13,14 +13,25 @@ RUN apt-get update && apt-get install -y netcat-openbsd
# Install python dependencies
COPY ./backend/requirements/base.txt ./backend/requirements/base.txt
COPY ./model_data/requirements/requirements.txt ./model_data/requirements/requirements.txt
RUN pip install --upgrade pip
RUN pip install -r backend/requirements/base.txt
RUN pip install -r model_data/requirements/requirements.txt
# Install and clean up temp caches
RUN pip install -r backend/requirements/base.txt && rm -rf /root/.cache
# Copy project
COPY ./backend ./backend
COPY ./model_data ./model_data
# When running locally, we need boto3
RUN pip install boto3
# Copy project files
COPY ./backend/ ./backend
COPY ./recommendations/ ./recommendations
COPY ./model_data/BaseUtility.py ./model_data/BaseUtility.py
COPY ./model_data/config.py ./model_data/config.py
COPY ./model_data/optimiser/ ./model_data/optimiser/
COPY ./model_data/__init__.py ./model_data/__init__.py
COPY ./model_data/EpcClean.py ./model_data/EpcClean.py
COPY ./model_data/utils.py ./model_data/utils.py
COPY ./model_data/epc_attributes/ ./model_data/epc_attributes/
COPY ./datatypes/ ./datatypes/
COPY ./utils/ ./utils/
# command to run on container start
CMD ["uvicorn", "backend.app.main:app", "--host", "0.0.0.0", "--port", "8000"]

View file

@ -28,6 +28,7 @@ COPY ./model_data/EpcClean.py ./model_data/EpcClean.py
COPY ./model_data/utils.py ./model_data/utils.py
COPY ./model_data/epc_attributes/ ./model_data/epc_attributes/
COPY ./datatypes/ ./datatypes/
COPY ./utils/ ./utils/
# Define the handler location
CMD ["backend.app.main.handler"]
#CMD ["backend.app.main.handler"]

View file

@ -43,6 +43,7 @@ custom:
functions:
app:
handler: backend.app.main.handler
image:
uri: ${env:ECR_URI}:${env:GITHUB_SHA}
events: