diff --git a/.dockerignore b/.dockerignore index f5c7b106..48f026fa 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,12 @@ model_data/local_data/* +backend/tests/* +recommendations/tests/* +model_data/tests/* +infrastructure/* +data_collection/* +node_modules/* +conservation_areas/* +open_uprn/* +land_registry/* +pytest.ini +*/README.md diff --git a/.github/workflows/deploy_fastapi_backend.yml b/.github/workflows/deploy_fastapi_backend.yml index b8c533f9..0b068440 100644 --- a/.github/workflows/deploy_fastapi_backend.yml +++ b/.github/workflows/deploy_fastapi_backend.yml @@ -59,16 +59,26 @@ jobs: echo "::set-output name=db_port::${{ secrets[format('{0}_DB_PORT', github.ref_name)] }}" echo "::set-output name=db_name::${{ secrets[format('{0}_DB_NAME', github.ref_name)] }}" - # - name: Build Lambda Layer - # run: | - # cd backend - # pip install -r requirements/lambda.txt -t python - # zip -r layer.zip python - # - # - name: Publish Lambda Layer - # run: | - # LAYER_ARN=$(aws lambda publish-layer-version --layer-name LambdaDependenciesLayer --zip-file fileb://backend/layer.zip | jq -r '.LayerVersionArn') - # aws ssm put-parameter --name "/${{ github.ref_name }}/LambdaDependenciesLayerArn" --value "$LAYER_ARN" --type String --overwrite + - name: Set ECR credentials + id: set_ecr_credentials + run: | + echo "::set-output name=ecr_uri::${{ secrets[format('{0}_ECR_URI', github.ref_name)] }}" + + - name: Setup Docker + uses: docker/setup-buildx-action@v1 + + - name: Build Docker Image + run: | + docker build -t my-lambda-image:latest -f backend/docker/lambda.Dockerfile . + + - name: Login to ECR + run: | + aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin ${{ steps.set_ecr_credentials.outputs.ecr_uri }} + + - name: Tag and Push Docker Image to ECR + run: | + docker tag my-lambda-image:latest ${{ steps.set_ecr_credentials.outputs.ecr_uri }}:latest + docker push ${{ steps.set_ecr_credentials.outputs.ecr_uri }}:latest - name: Deploy to AWS Lambda via Serverless env: @@ -81,6 +91,7 @@ jobs: DB_HOST: ${{ steps.set_db_credentials.outputs.db_host }} DB_PORT: ${{ steps.set_db_credentials.outputs.db_port }} DB_NAME: ${{ steps.set_db_credentials.outputs.db_name }} + ECR_URI: ${{ steps.set_ecr_credentials.outputs.ecr_uri }} run: | # Fetch database credentials from AWS Secrets Manager SECRET_VALUE=$(aws secretsmanager get-secret-value --secret-id dev/assessment_model/db_credentials --query SecretString) diff --git a/backend/docker/lambda.Dockerfile b/backend/docker/lambda.Dockerfile index 40a1e927..2c338015 100644 --- a/backend/docker/lambda.Dockerfile +++ b/backend/docker/lambda.Dockerfile @@ -9,4 +9,24 @@ ENV PYTHONUNBUFFERED 1 WORKDIR /Model # Install system dependencies -RUN apt-get update && apt-get install -y netcat-openbsd \ No newline at end of file +RUN apt-get update && apt-get install -y netcat-openbsd + +# Install python dependencies +COPY ./backend/requirements/base.txt ./backend/requirements/base.txt +RUN pip install --upgrade pip +RUN pip install -r backend/requirements/base.txt + +# 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/ + +# Define the handler location +CMD ["backend.app.main.handler"] diff --git a/serverless.yml b/serverless.yml index afd76c81..c6cecba5 100644 --- a/serverless.yml +++ b/serverless.yml @@ -17,6 +17,7 @@ provider: DB_USERNAME: ${env:DB_USERNAME} DB_PASSWORD: ${env:DB_PASSWORD} DB_PORT: ${env:DB_PORT} + ECR_URI: ${env:ECR_URI} # Give lambda access to read from the bucket iam: role: