Skip to content

Commit

Permalink
adding new lambda function, new tests and updated builds
Browse files Browse the repository at this point in the history
  • Loading branch information
JackLewis-digirati committed Aug 31, 2023
1 parent ac5d00b commit 634a050
Show file tree
Hide file tree
Showing 11 changed files with 665 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .env-dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ CLOUDWATCH_CUSTOMER_DIFFERENCE_METRIC_NAME=
CLOUDWATCH_SPACE_DIFFERENCE_METRIC_NAME=
CLOUDWATCH_SPACE_DELETE_METRIC_NAME=
CLOUDWATCH_CUSTOMER_DELETE_METRIC_NAME=
CLOUDWATCH_CUSTOMER_IMAGE_SIZE_DIFFERENCE_METRIC_NAME=CustomerImageSizeDelta
CLOUDWATCH_CUSTOMER_IMAGE_NUMBER_DIFFERENCE_METRIC_NAME=CustomerImageNumberDelta
CLOUDWATCH_CUSTOMER_THUMBNAIL_SIZE_DIFFERENCE_METRIC_NAME=CustomerThumbnailSizeDelta
CLOUDWATCH_SPACE_IMAGE_SIZE_DIFFERENCE_METRIC_NAME=SpaceImageSizeDelta
CLOUDWATCH_SPACE_IMAGE_NUMBER_DIFFERENCE_METRIC_NAME=SpaceImageNumberDelta
CLOUDWATCH_SPACE_THUMBNAIL_SIZE_DIFFERENCE_METRIC_NAME=SpaceThumbnailSizeDelta
27 changes: 27 additions & 0 deletions .github/workflows/build-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ jobs:
working-directory: ./entity-counter-recalculator
run: python -m unittest test_main.py

test-customer-storage-recalculator:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup python
uses: actions/[email protected]
with:
python-version: 3.11
cache: 'pip' # caching pip dependencies
- name: Install dependencies
run: python -m pip install -r requirements.txt
- name: Run python tests
working-directory: ./customer-storage-recalculator
run: python -m unittest test_main.py

build-push-dlcs-entity-counter-recalculator:
runs-on: ubuntu-latest
needs: test-entity-counter-recalculator
Expand All @@ -36,4 +51,16 @@ jobs:
image-name: "entity-counter-recalculator"
dockerfile: "entity-counter-recalculator/Dockerfile"
context: "./entity-counter-recalculator"
github-token: ${{ secrets.GITHUB_TOKEN }}

build-push-dlcs-customer-storage-recalculator:
runs-on: ubuntu-latest
needs: test-entity-counter-recalculator
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/docker-build-and-push
with:
image-name: "customer-storage-recalculator"
dockerfile: "customer-storage-recalculator/Dockerfile"
context: "./customer-storage-recalculator"
github-token: ${{ secrets.GITHUB_TOKEN }}
37 changes: 37 additions & 0 deletions customer-storage-recalculator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Stage 1 - grab the image

FROM python:3.11
LABEL authors="JackLewis"

RUN apt-get update

# Stage 2 - build function and dependencies
FROM python AS build-image

# Create function directory
RUN mkdir -p /home/app/
# Copy handler function
COPY main.py /home/app/
COPY /app /home/app/app

# Install Lambda Runtime Interface Client for Python
RUN pip install awslambdaric --target /home/app/

# Copy and install other requirements
COPY minimalRequirements.txt ./
RUN pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir -r minimalRequirements.txt --target /home/app/

# Stage 3 - final runtime image
# Grab a fresh copy of the Python image
FROM python
# Set working directory to function root directory
WORKDIR /home/app
# Copy in the built dependencies
COPY --from=build-image /home/app /home/app
# (Optional) Add Lambda Runtime Interface Emulator and use a script in the ENTRYPOINT for simpler local runs
ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie /usr/bin/aws-lambda-rie
COPY entry.sh /
RUN chmod 755 /usr/bin/aws-lambda-rie /entry.sh
ENTRYPOINT [ "/entry.sh" ]
CMD [ "main.handler" ]
19 changes: 19 additions & 0 deletions customer-storage-recalculator/app/aws_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import boto3

from app.settings import LOCALSTACK, REGION, LOCALSTACK_ADDRESS
from logzero import logger


def get_aws_client(resource_type: str):
"""Get an aws resource configured to use LocalStack if env var is set"""
if LOCALSTACK:
logger.warn(f"Using localstack for {resource_type} resource")
return boto3.client(
resource_type,
region_name=REGION,
endpoint_url=LOCALSTACK_ADDRESS,
aws_access_key_id="foo",
aws_secret_access_key="bar", # pragma: allowlist secret
)
else:
return boto3.client(resource_type, REGION)
27 changes: 27 additions & 0 deletions customer-storage-recalculator/app/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os


def _get_boolean(env_name: str, fallback: str) -> bool:
return os.environ.get(env_name, fallback).lower() in ("true", "t", "1")


# AWS
REGION = os.environ.get("AWS_REGION", "eu-west-1")
ENABLE_CLOUDWATCH_INTEGRATION = _get_boolean("ENABLE_CLOUDWATCH_INTEGRATION", "True")
CLOUDWATCH_CUSTOMER_IMAGE_SIZE_DIFFERENCE_METRIC_NAME = os.environ.get("CLOUDWATCH_CUSTOMER_IMAGE_SIZE_DIFFERENCE_METRIC_NAME")
CLOUDWATCH_CUSTOMER_IMAGE_NUMBER_DIFFERENCE_METRIC_NAME = os.environ.get("CLOUDWATCH_CUSTOMER_IMAGE_NUMBER_DIFFERENCE_METRIC_NAME")
CLOUDWATCH_CUSTOMER_THUMBNAIL_SIZE_DIFFERENCE_METRIC_NAME = os.environ.get("CLOUDWATCH_CUSTOMER_THUMBNAIL_SIZE_DIFFERENCE_METRIC_NAME")
CLOUDWATCH_SPACE_IMAGE_SIZE_DIFFERENCE_METRIC_NAME = os.environ.get("CLOUDWATCH_SPACE_IMAGE_SIZE_DIFFERENCE_METRIC_NAME")
CLOUDWATCH_SPACE_IMAGE_NUMBER_DIFFERENCE_METRIC_NAME = os.environ.get("CLOUDWATCH_SPACE_IMAGE_NUMBER_DIFFERENCE_METRIC_NAME")
CLOUDWATCH_SPACE_THUMBNAIL_SIZE_DIFFERENCE_METRIC_NAME = os.environ.get("CLOUDWATCH_SPACE_THUMBNAIL_SIZE_DIFFERENCE_METRIC_NAME")
APP_VERSION = os.environ.get('APP_VERSION', "1.0")
AWS_CONNECTION_STRING_LOCATION = os.environ.get("AWS_CONNECTION_STRING_LOCATION")

# LocalStack
LOCALSTACK = _get_boolean("LOCALSTACK", "False")
LOCALSTACK_ADDRESS = os.environ.get("LOCALSTACK_ADDRESS", "http://localhost:4566")

# Postgres
CONNECTION_STRING = os.environ.get("CONNECTION_STRING")
CONNECTION_TIMEOUT = os.environ.get("CONNECTION_TIMEOUT")
DRY_RUN = os.environ.get("DRY_RUN", False)
6 changes: 6 additions & 0 deletions customer-storage-recalculator/entry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
exec /usr/bin/aws-lambda-rie /usr/local/bin/python -m awslambdaric $1
else
exec /usr/local/bin/python -m awslambdaric $1
fi
Loading

0 comments on commit 634a050

Please sign in to comment.