Skip to content

Commit f386635

Browse files
authored
1 parent 8b6244f commit f386635

6 files changed

Lines changed: 145 additions & 0 deletions

File tree

doc/dynamodb-local.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# DynamoDB Local
2+
3+
[DynamoDB Local](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html)
4+
is a local version of AWS DynamoDB for testing and development without accessing the cloud.
5+
6+
## Usage example
7+
8+
<https://github.com/juspay/services-flake/blob/main/nix/services/dynamodb-local_test.nix>

doc/services.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ short-title: Services
88
- [[azurite]]#
99
- [[cassandra]]#
1010
- [[clickhouse]]#
11+
- [[dynamodb-local]]#
1112
- [[elasticsearch]]#
1213
- [[grafana]]#
1314
- [[tempo]]

nix/services/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ in
66
./apache-kafka.nix
77
./azurite.nix
88
./clickhouse
9+
./dynamodb-local.nix
910
./elasticsearch.nix
1011
./mongodb.nix
1112
./mysql

nix/services/dynamodb-local.nix

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{ pkgs
2+
, lib
3+
, name
4+
, config
5+
, ...
6+
}:
7+
{
8+
options = {
9+
package = lib.mkPackageOption pkgs "dynamodb-local" { };
10+
11+
port = lib.mkOption {
12+
type = lib.types.port;
13+
default = 8000;
14+
description = ''
15+
The port number that DynamoDB uses to communicate with your application.
16+
'';
17+
};
18+
19+
dbPath = lib.mkOption {
20+
type = lib.types.nullOr lib.types.str;
21+
default = null;
22+
description = ''
23+
The directory where DynamoDB writes its database file. If you don't specify this option, the file
24+
is written to the current directory.
25+
'';
26+
};
27+
28+
inMemory = lib.mkOption {
29+
type = lib.types.bool;
30+
default = false;
31+
description = ''
32+
DynamoDB runs in memory instead of using a database file. When you stop DynamoDB, none of the
33+
data is saved.
34+
'';
35+
apply =
36+
v:
37+
lib.throwIf (config.dbPath != null) ''
38+
You can't specify both -dbPath and -inMemory at once.
39+
''
40+
v;
41+
};
42+
43+
extraArgs = lib.mkOption {
44+
type = lib.types.listOf lib.types.str;
45+
default = [ ];
46+
description = ''
47+
Extra args to pass down to DynamoDB.
48+
'';
49+
};
50+
51+
disableTelemetry = lib.mkOption {
52+
type = lib.types.bool;
53+
default = true;
54+
description = ''
55+
Specify if DynamoDB local should send telemetry.
56+
'';
57+
};
58+
};
59+
60+
config.outputs.settings.processes.${name} =
61+
let
62+
startCommand = pkgs.writeShellApplication {
63+
name = "start-dynamodb";
64+
runtimeInputs = [ config.package ];
65+
text = ''
66+
${lib.optionalString (config.dbPath != null) ''
67+
mkdir -p ${lib.escapeShellArg config.dbPath}
68+
''}
69+
exec dynamodb-local \
70+
-port ${toString config.port} \
71+
${lib.optionalString (config.dbPath != null) "-dbPath ${lib.escapeShellArg config.dbPath}"} \
72+
${lib.optionalString config.inMemory "-inMemory"} \
73+
${lib.optionalString config.disableTelemetry "-disableTelemetry"} \
74+
${lib.escapeShellArgs config.extraArgs}
75+
'';
76+
};
77+
in
78+
{
79+
command = startCommand;
80+
readiness_probe = {
81+
# There is no explicit healthcheck in DynamoDB. Instead, it's a common practice to
82+
# use `list-tables` opearation as a healthcheck. Inspired by Localstack and devenv
83+
# DynamoDB service.
84+
# DynamoDB Local doesn't have any credentials, but awscli expects them.
85+
exec.command = ''
86+
AWS_ACCESS_KEY_ID='fake' \
87+
AWS_SECRET_ACCESS_KEY='fake' \
88+
AWS_DEFAULT_REGION='us-east-1' \
89+
${lib.getExe pkgs.awscli2} dynamodb list-tables \
90+
--endpoint-url http://127.0.0.1:${toString config.port}
91+
'';
92+
initial_delay_seconds = 2;
93+
period_seconds = 10;
94+
timeout_seconds = 4;
95+
success_threshold = 1;
96+
failure_threshold = 5;
97+
};
98+
# https://github.com/F1bonacc1/process-compose#-auto-restart-if-not-healthy
99+
availability = {
100+
restart = "on_failure";
101+
max_restarts = 5;
102+
};
103+
};
104+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{ pkgs
2+
, config
3+
, ...
4+
}: {
5+
services.dynamodb-local."dynamodb1" = {
6+
enable = true;
7+
inMemory = true;
8+
};
9+
10+
settings.processes.test =
11+
let
12+
cfg = config.services.dynamodb-local."dynamodb1";
13+
in
14+
{
15+
command = pkgs.writeShellApplication {
16+
name = "dynamodb-test";
17+
runtimeInputs = with pkgs; [ awscli2 jq ];
18+
runtimeEnv = {
19+
AWS_ACCESS_KEY_ID = "fake";
20+
AWS_SECRET_ACCESS_KEY = "fake";
21+
AWS_DEFAULT_REGION = "us-east-1";
22+
};
23+
text = ''
24+
aws dynamodb list-tables --endpoint-url "http://127.0.0.1:${toString cfg.port}" \
25+
| jq '.TableNames'
26+
'';
27+
};
28+
depends_on."dynamodb1".condition = "process_healthy";
29+
};
30+
}

test/flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"${inputs.services-flake}/nix/services/azurite_test.nix"
4545
"${inputs.services-flake}/nix/services/cassandra_test.nix"
4646
"${inputs.services-flake}/nix/services/clickhouse/clickhouse_test.nix"
47+
"${inputs.services-flake}/nix/services/dynamodb-local_test.nix"
4748
"${inputs.services-flake}/nix/services/grafana_test.nix"
4849
"${inputs.services-flake}/nix/services/memcached_test.nix"
4950
"${inputs.services-flake}/nix/services/minio_test.nix"

0 commit comments

Comments
 (0)