Skip to content

Commit 4ff5a3e

Browse files
eli-599/replacing localstack with moto for local lambda IT testing (#582)
* localstack * localstack * localstack * ELI-662: working sam * ELI-662: working sam * AWS RIE for lambda and moto for other services, Cloud watch cannot be configured for testing * moved lambda client to lambda conftest * use docker compose profiling * lint fixes * docker clean up * docker clean up * test - commit * test - commit * reduced subprocess usage * lint fix * fix services trigger * simulate API Gateway requests in python
1 parent 6f27043 commit 4ff5a3e

8 files changed

Lines changed: 437 additions & 718 deletions

File tree

poetry.lock

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

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ awscli-local = "^0.22.2"
5252
polyfactory = "^3.2.0"
5353
pyright = "^1.1.407"
5454
brunns-matchers = "^2.9.0"
55-
localstack = "^4.12.0"
5655
pytest-docker = "^3.2.3"
5756
stamina = "^25.2.0"
5857
pytest-freezer = "^0.4.9"
@@ -63,7 +62,7 @@ behave = "^1.3.3"
6362
python-dotenv = "^1.2.1"
6463
openapi-spec-validator = "^0.7.2"
6564
pip-licenses = "^5.5.0"
66-
65+
cachetools = "^7.0.1"
6766

6867
[tool.poetry-plugin-lambda-build]
6968
docker-image = "public.ecr.aws/sam/build-python3.13:1.139-x86_64" # See https://gallery.ecr.aws/search?searchTerm=%22python%22&architecture=x86-64&popularRegistries=amazon&verified=verified&operatingSystems=Linux

src/eligibility_signposting_api/config/config.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def config() -> dict[str, Any]:
2626
audit_bucket_name = BucketName(os.getenv("AUDIT_BUCKET_NAME", "test-audit-bucket"))
2727
hashing_secret_name = HashSecretName(os.getenv("HASHING_SECRET_NAME", "test_secret"))
2828
aws_default_region = AwsRegion(os.getenv("AWS_DEFAULT_REGION", "eu-west-1"))
29-
enable_xray_patching = bool(os.getenv("ENABLE_XRAY_PATCHING", "false"))
29+
enable_xray_patching = os.getenv("ENABLE_XRAY_PATCHING", "false").lower() == "true"
3030
kinesis_audit_stream_to_s3 = AwsKinesisFirehoseStreamName(
3131
os.getenv("KINESIS_AUDIT_STREAM_TO_S3", "test_kinesis_audit_stream_to_s3")
3232
)
@@ -51,21 +51,21 @@ def config() -> dict[str, Any]:
5151
"log_level": log_level,
5252
}
5353

54-
local_stack_endpoint = "http://localhost:4566"
54+
moto_server_endpoint = "http://localhost:4566"
5555
return {
5656
"aws_access_key_id": AwsAccessKey(os.getenv("AWS_ACCESS_KEY_ID", "dummy_key")),
5757
"aws_default_region": aws_default_region,
5858
"aws_secret_access_key": AwsSecretAccessKey(os.getenv("AWS_SECRET_ACCESS_KEY", "dummy_secret")),
59-
"dynamodb_endpoint": URL(os.getenv("DYNAMODB_ENDPOINT", local_stack_endpoint)),
59+
"dynamodb_endpoint": URL(os.getenv("DYNAMODB_ENDPOINT", moto_server_endpoint)),
6060
"person_table_name": person_table_name,
61-
"s3_endpoint": URL(os.getenv("S3_ENDPOINT", local_stack_endpoint)),
61+
"s3_endpoint": URL(os.getenv("S3_ENDPOINT", moto_server_endpoint)),
6262
"rules_bucket_name": rules_bucket_name,
6363
"audit_bucket_name": audit_bucket_name,
6464
"consumer_mapping_bucket_name": consumer_mapping_bucket_name,
65-
"firehose_endpoint": URL(os.getenv("FIREHOSE_ENDPOINT", local_stack_endpoint)),
65+
"firehose_endpoint": URL(os.getenv("FIREHOSE_ENDPOINT", moto_server_endpoint)),
6666
"kinesis_audit_stream_to_s3": kinesis_audit_stream_to_s3,
6767
"enable_xray_patching": enable_xray_patching,
68-
"secretsmanager_endpoint": URL(os.getenv("SECRET_MANAGER_ENDPOINT", local_stack_endpoint)),
68+
"secretsmanager_endpoint": URL(os.getenv("SECRET_MANAGER_ENDPOINT", moto_server_endpoint)),
6969
"hashing_secret_name": hashing_secret_name,
7070
"log_level": log_level,
7171
}

tests/docker-compose.mock_aws.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
services:
2+
moto-server:
3+
#used for s3, dynamodb, kinesis, secret manager
4+
# lambda cannot be used, because its 3.11 (older)
5+
image: motoserver/moto:latest
6+
container_name: moto-server
7+
ports:
8+
- "4566:5000"
9+
networks:
10+
- test-network
11+
healthcheck:
12+
test: [ "CMD", "curl", "-f", "http://localhost:5000/" ]
13+
interval: 1s
14+
timeout: 1s
15+
retries: 30
16+
lambda-api:
17+
image: public.ecr.aws/lambda/python:3.13
18+
container_name: lambda-api
19+
ports:
20+
- "4567:8080"
21+
platform: linux/amd64
22+
volumes:
23+
- ../dist/lambda.zip:/tmp/lambda.zip:ro
24+
environment:
25+
- AWS_ACCESS_KEY_ID=dummy_key
26+
- AWS_SECRET_ACCESS_KEY=dummy_secret
27+
- AWS_DEFAULT_REGION=eu-west-1
28+
- PYTHONPATH=/var/task
29+
- AWS_ENDPOINT_URL=http://moto-server:5000
30+
- DYNAMODB_ENDPOINT=http://moto-server:5000
31+
- S3_ENDPOINT=http://moto-server:5000
32+
- SECRET_MANAGER_ENDPOINT=http://moto-server:5000
33+
- FIREHOSE_ENDPOINT=http://moto-server:5000
34+
- LOG_LEVEL=INFO
35+
entrypoint: /bin/sh
36+
command:
37+
- "-c"
38+
- |
39+
mkdir -p /var/task &&
40+
python3 -m zipfile -e /tmp/lambda.zip /var/task &&
41+
exec /usr/local/bin/aws-lambda-rie python3 -m awslambdaric eligibility_signposting_api.app.lambda_handler
42+
networks:
43+
- test-network
44+
depends_on:
45+
moto-server:
46+
condition: service_healthy
47+
48+
networks:
49+
test-network:
50+
driver: bridge

tests/docker-compose.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)