diff --git a/.dockerignore b/.dockerignore index 28718547..083ae2c7 100644 --- a/.dockerignore +++ b/.dockerignore @@ -11,3 +11,4 @@ open_uprn/* land_registry/* pytest.ini */README.md +utils/tests/* diff --git a/backend/README.md b/backend/README.md index fa1d0068..1d87774d 100644 --- a/backend/README.md +++ b/backend/README.md @@ -59,7 +59,7 @@ FastAPI automatically generates interactive API documentation for your applicati server and visit /docs in your browser. Alternatively, you can go to /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: diff --git a/backend/docker/Dockerfile b/backend/docker/Dockerfile index 7d35a53a..24f319db 100644 --- a/backend/docker/Dockerfile +++ b/backend/docker/Dockerfile @@ -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"] diff --git a/backend/docker/lambda.Dockerfile b/backend/docker/lambda.Dockerfile index 9e16c21a..dab7275c 100644 --- a/backend/docker/lambda.Dockerfile +++ b/backend/docker/lambda.Dockerfile @@ -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"] diff --git a/serverless.yml b/serverless.yml index 89eb666a..3445e0f9 100644 --- a/serverless.yml +++ b/serverless.yml @@ -43,6 +43,7 @@ custom: functions: app: + handler: backend.app.main.handler image: uri: ${env:ECR_URI}:${env:GITHUB_SHA} events: