Skip to content

Commit 1303f87

Browse files
committed
shared tests pass
1 parent 7b2be5e commit 1303f87

File tree

15 files changed

+1248
-3
lines changed

15 files changed

+1248
-3
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ updates:
5959
- "/recordprocessor"
6060
- "/redis_sync"
6161
- "/lambdas/id_sync"
62+
- "/lambdas/shared"
6263
schedule:
6364
interval: "daily"
6465
open-pull-requests-limit: 1

.github/workflows/sonarcloud.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ jobs:
124124
poetry run coverage run -m unittest discover || echo "redis_sync tests failed" >> ../failed_tests.txt
125125
poetry run coverage xml -o ../redis_sync-coverage.xml
126126
127+
- name: Run unittest with shared
128+
working-directory: lambdas/shared
129+
id: shared
130+
env:
131+
PYTHONPATH: ${{ github.workspace }}/lambdas/shared/src:${{ github.workspace }}/lambdas/shared/tests
132+
continue-on-error: true
133+
run: |
134+
poetry env use 3.11
135+
poetry install
136+
poetry run coverage run -m unittest discover || echo "shared tests failed" >> ../../failed_tests.txt
137+
poetry run coverage xml -o ../../shared-coverage.xml
138+
127139
- name: Run unittest with id_sync
128140
working-directory: lambdas/id_sync
129141
id: id_sync

immunisation-fhir-api.code-workspace

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
},
3333
{
3434
"path": "lambdas/id_sync"
35+
},
36+
{
37+
"path": "lambdas/shared"
3538
}
3639
],
3740
"settings": {},
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"python.analysis.extraPaths": [
3+
"./src"
4+
],
5+
"python.testing.unittestArgs": [
6+
"-v",
7+
"-s",
8+
"./",
9+
"-p",
10+
"test_*.py"
11+
],
12+
"python.testing.pytestEnabled": false,
13+
"python.testing.unittestEnabled": true,
14+
"pylint.args": [
15+
"--init-hook",
16+
"import sys; sys.path.append('./src')"
17+
]
18+
}

lambdas/shared/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
test:
3+
@PYTHONPATH=src python -m unittest discover -s tests -p "test_*.py" -v
4+
5+
test-list:
6+
@PYTHONPATH=src:tests:tests python -m unittest discover -s tests/common -p "test_*.py" --verbose | grep test_
7+
8+
coverage-run:
9+
@PYTHONPATH=src:tests coverage run -m unittest discover -v
10+
11+
coverage-report:
12+
coverage report -m
13+
14+
coverage-html:
15+
coverage html
16+
17+
.PHONY: build package test

lambdas/shared/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Redis Sync Lambda
2+
3+
## Overview
4+
5+
**Redis Sync** is an AWS Lambda function designed to monitor a configuration S3 bucket and synchronize its contents with an ElastiCache Redis instance. Whenever a new configuration file is uploaded or updated in the S3 bucket, the Lambda function processes the file, applies any required transformations, and uploads the result to the Redis cache.
6+
7+
## Features
8+
9+
- **S3 Event Driven:** Automatically triggered by S3 events (e.g., file uploads or updates) in the config bucket.
10+
- **Transformation Support:** Applies custom transformation logic to configuration files before caching.
11+
- **Redis Integration:** Uploads processed configuration data to ElastiCache Redis for fast, centralized access.
12+
- **Logging:** Provides detailed logging for monitoring and troubleshooting.
13+
14+
## How It Works
15+
16+
1. **S3 Event Trigger:** The Lambda is triggered by S3 events on the config bucket.
17+
2. **File Processing:** The Lambda reads the new or updated file from S3.
18+
3. **Transformation:** If required, the file content is transformed to the appropriate format.
19+
4. **Redis Upload:** The transformed data is uploaded to the Redis cache under a key corresponding to the file.
20+
5. **Monitoring:** Logs are generated for each step, aiding in monitoring and debugging.
21+
22+
## Configuration
23+
24+
- **Environment Variables:**
25+
- `CONFIG_BUCKET_NAME`: Name of the S3 bucket to monitor.
26+
- `AWS_REGION`: AWS region for S3 and Redis.
27+
- `REDIS_HOST`: Redis endpoint.
28+
- `REDIS_PORT`: Redis port (default: 6379).
29+
30+
## Usage
31+
32+
1. **Deploy the Lambda** using your preferred IaC tool (e.g., Terraform, AWS SAM).
33+
2. **Configure S3 event notifications** to trigger the Lambda on object creation or update.
34+
3. **Ensure Redis and S3 permissions** are set for the Lambda execution role.
35+
36+
## Development
37+
38+
- Code is located in the `src/` directory.
39+
- Unit tests are in the `tests/` directory.
40+
- Use the provided Makefile and Dockerfile for building, testing, and packaging.
41+
42+
## License
43+
44+
This project is maintained by NHS. See [LICENSE](../LICENSE) for details.

lambdas/shared/poetry.lock

Lines changed: 829 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lambdas/shared/pyproject.toml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[project]
2+
name = "shared"
3+
version = "0.1.0"
4+
description = "Shared utilities for NHS Immunisation FHIR API"
5+
authors = [
6+
{name = "nhsdevws",email = "[email protected]"}
7+
]
8+
9+
[tool.poetry]
10+
name = "shared"
11+
version = "0.1.0"
12+
description = ""
13+
authors = ["s.wates <[email protected]>"]
14+
readme = "README.md"
15+
packages = [
16+
{include = "src"},
17+
{include = "common", from = "src"}
18+
]
19+
20+
[tool.poetry.dependencies]
21+
python = ">=3.11"
22+
boto3 = "~1.38.29"
23+
mypy-boto3-dynamodb = "^1.26.164"
24+
moto = "~5.1.5"
25+
python-stdnum = "^2.1"
26+
coverage = "^7.8.0"
27+
redis = "^4.6.0"
28+
29+
[tool.poetry.group.dev.dependencies]
30+
coverage = "^7.8.0"
31+
32+
[build-system]
33+
requires = ["poetry-core"]
34+
build-backend = "poetry.core.masonry.api"

lambdas/shared/src/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import os
22
import logging
3+
import redis
34
from boto3 import client as boto3_client
45

6+
57
logging.basicConfig(level="INFO")
68
logger = logging.getLogger()
79
logger.setLevel("INFO")
810

11+
STREAM_NAME = os.getenv("SPLUNK_FIREHOSE_NAME", "firehose-name-not-defined")
12+
CONFIG_BUCKET_NAME = os.getenv("CONFIG_BUCKET_NAME", "variconfig-bucketable-not-defined")
13+
914
REGION_NAME = os.getenv("AWS_REGION", "eu-west-2")
15+
REDIS_HOST = os.getenv("REDIS_HOST", "")
16+
REDIS_PORT = os.getenv("REDIS_PORT", 6379)
17+
1018
s3_client = boto3_client("s3", region_name=REGION_NAME)
1119
firehose_client = boto3_client("firehose", region_name=REGION_NAME)
20+
logger.info(f"Connecting to Redis at {REDIS_HOST}:{REDIS_PORT}")
21+
redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, decode_responses=True)

0 commit comments

Comments
 (0)