Skip to content

Commit 8e1da43

Browse files
Lucas BeloLucas Belo
Lucas Belo
authored and
Lucas Belo
committed
DynamoDB locally with Docker
1 parent 096e999 commit 8e1da43

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,23 @@ The easiest way to manage servers, subnets, load balancers, managed databases, e
66

77
## Services
88

9-
Anything that is long-running or talks to our end users is considered a service. It can be monolithic Django application, Vue UI, or a couple of microservice APIs -- all will reside in this folder.
9+
Anything that is long-running or talks to our end users is considered a service. It can be monolithic Django application, Vue UI, or a couple of microservice APIs -- all will reside in this folder.
10+
11+
## Running code-quality locally
12+
13+
Run the code-quality tools before commiting code. There's a validation in the CI/CD that might brake.
14+
15+
```bash
16+
$ poetry run black .
17+
$ poetry run isort . --profile black
18+
$ poetry run flake8 .
19+
```
20+
21+
## Running tests locally
22+
23+
You need to set AWS_DEFAULT_REGION environment variable since Boto3 requires it to be set. Then, run the tests to see the new one fail:
24+
25+
``` bash
26+
$ export AWS_DEFAULT_REGION=eu-west-1
27+
$ poetry run pytest tests.py
28+
```

services/tasks_api/docker-compose.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: '3.8'
2+
3+
services:
4+
dynamodb-local:
5+
command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
6+
image: "amazon/dynamodb-local:latest"
7+
container_name: dynamodb-local
8+
ports:
9+
- "9999:8000"
10+
volumes:
11+
- "./docker/dynamodb:/home/dynamodblocal/data"
12+
working_dir: /home/dynamodblocal

services/tasks_api/store.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99

1010
class TaskStore:
11-
def __init__(self, table_name):
11+
def __init__(self, table_name, dynamodb_url=None):
1212
self.table_name = table_name
13+
self.dynamodb_url = dynamodb_url
1314

1415
def add(self, task):
15-
dynamodb = boto3.resource("dynamodb")
16+
dynamodb = boto3.resource("dynamodb", endpoint_url=self.dynamodb_url)
1617
table = dynamodb.Table(self.table_name)
1718
table.put_item(
1819
Item={
@@ -28,7 +29,7 @@ def add(self, task):
2829
)
2930

3031
def get_by_id(self, task_id, owner):
31-
dynamodb = boto3.resource("dynamodb")
32+
dynamodb = boto3.resource("dynamodb", endpoint_url=self.dynamodb_url)
3233
table = dynamodb.Table(self.table_name)
3334
record = table.get_item(
3435
Key={
@@ -50,7 +51,7 @@ def list_closed(self, owner):
5051
return self._list_by_status(owner, TaskStatus.CLOSED)
5152

5253
def _list_by_status(self, owner, status):
53-
dynamodb = boto3.resource("dynamodb")
54+
dynamodb = boto3.resource("dynamodb", endpoint_url=self.dynamodb_url)
5455
table = dynamodb.Table(self.table_name)
5556
last_key = None
5657
query_kwargs = {

0 commit comments

Comments
 (0)