Skip to content

Commit

Permalink
feat: initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
jakowenko committed Feb 1, 2022
0 parents commit 29c53d9
Show file tree
Hide file tree
Showing 14 changed files with 13,059 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .commitlintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
[
'build',
'chore',
'ci',
'docs',
'feat',
'fix',
'perf',
'refactor',
'revert',
'style',
'test',
'wip',
],
],
'subject-case': [
2,
'always',
[
'lower-case',
'upper-case',
'camel-case',
'kebab-case',
'pascal-case',
'sentence-case',
'snake-case',
'start-case',
],
],
},
};
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!dynamodb-local/**
56 changes: 56 additions & 0 deletions .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Build

on:
release:
types: [released]
branches:
- master
workflow_dispatch:

jobs:
build:
name: Build
if: github.repository == 'twentyfourg/dynamodb-local'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Get VERSION
id: version
uses: notiz-dev/github-action-json-property@release
with:
path: ./package.json
prop_path: version
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Docker Login
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Docker Buildx (push)
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: twentyfourg/dynamodb-local:latest,twentyfourg/dynamodb-local:${{steps.version.outputs.prop}}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
- # Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release

on:
push:
branches:
- master
jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 16
- name: Install dependencies
run: npm ci
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GIT_AUTHOR_NAME: ${{ github.event.head_commit.author.name }}
GIT_AUTHOR_EMAIL: ${{ github.event.head_commit.author.email }}
GIT_COMMITTER_NAME: ${{ github.event.head_commit.author.name }}
GIT_COMMITTER_EMAIL: ${{ github.event.head_commit.author.email }}
run: npx semantic-release
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
53 changes: 53 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
FROM amazonlinux:2
COPY dynamodb-local /opt/dynamodb-local

# Determine which build platform so we know what binaries to install
ARG TARGETPLATFORM

# Fake credentials required to work with local endpoint
ENV AWS_ACCESS_KEY_ID=local
ENV AWS_SECRET_ACCESS_KEY=local

# Set default value for features
ENV DYNAMODB_ADMIN_ENABLED=true
ENV DYNAMODB_CREATE_TABLES=true
ENV DYNAMODB_TABLE_SCHEMA_PATH=./tableSchema.json

# Set environment variables for NodeJs install
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 16.13.2
ENV NVM_INSTALL_PATH $NVM_DIR/versions/node/v$NODE_VERSION

# Install required linux utilities
RUN yum update --assumeyes --quiet --errorlevel=0 && \
yum upgrade --assumeyes --quiet --errorlevel=0 && \
yum install --assumeyes --quiet --errorlevel=0 less unzip gzip tar jq && \
amazon-linux-extras install java-openjdk11 -y

# Install NodeJS and npm
RUN mkdir $NVM_DIR
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
RUN source $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
ENV NODE_PATH $NVM_INSTALL_PATH/lib/node_modules
ENV PATH $NVM_INSTALL_PATH/bin:$PATH

# Install AWS CLI
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then curl "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip" -o "/tmp/awscliv2.zip"; else curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "/tmp/awscliv2.zip"; fi
RUN unzip /tmp/awscliv2.zip -d /tmp/ && bash /tmp/aws/install

# Install dynamodb-admin GUI
RUN npm install -g dynamodb-admin

WORKDIR /opt/dynamodb-local

# Install dynamodb-local
RUN curl "https://s3.us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz" -o /opt/dynamodb-local/dynamodb_local_latest.tar.gz && \
tar -xvzf dynamodb_local_latest.tar.gz

EXPOSE 8000 8001

CMD ./entrypoint.sh
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# DynamoDB Local

Run DynamoDB locally inside a Docker container.

The container comes shipped with [DynamoDB-local](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html) and [DynamoDB Admin](https://www.npmjs.com/package/dynamodb-admin) for easy GUI access. You can also set default table(s) to be created on start up.

## Usage

```bash
docker run -d -p 8000:8000 -p 8001:8001 twentyfourg/dynamodb-local
```

## Configuration

You can enable/disable features or change configuration by using environment variables.

| Environment Variable | Description | Default |
| ---------------------------- | ------------------------------------------------------------------------------------------------------- | -------------------- |
| `DYNAMODB_ADMIN_ENABLED` | Enable or disable DynamoDB Admin | `true` |
| `DYNAMODB_CREATE_TABLES` | Whether to create default DynamoDB tables | `true` |
| `DYNAMODB_TABLE_SCHEMA_PATH` | Where the DynamoDB table schema JSON is stored. Only does something if `DYNAMODB_CREATE_TABLES` is set. | `./tableSchema.json` |

## Default Tables

Default tables are created using a [JSON schema](). You can define custom tables to create by mounting a schema file into the container and altering the `DYNAMODB_TABLE_SCHEMA_PATH` environment variable.

The schema file must be a JSON array so even if you have one table, enclose your table in `[]`.

One Table

```json
[
{
"TableName": "cache",
"KeySchema": [
{ "AttributeName": "key", "KeyType": "HASH" }
],
"AttributeDefinitions": [
{ "AttributeName": "key", "AttributeType": "S" }
],
"BillingMode": "PAY_PER_REQUEST"
}
```

Multiple Tables

```json
[
{
"TableName": "cache",
"KeySchema": [{ "AttributeName": "key", "KeyType": "HASH" }],
"AttributeDefinitions": [{ "AttributeName": "key", "AttributeType": "S" }],
"BillingMode": "PAY_PER_REQUEST"
},
{
"TableName": "rate-limit",
"KeySchema": [{ "AttributeName": "rate-limit", "KeyType": "HASH" }],
"AttributeDefinitions": [
{ "AttributeName": "rate-limit", "AttributeType": "S" }
],
"BillingMode": "PAY_PER_REQUEST"
}
]
```
19 changes: 19 additions & 0 deletions dynamodb-local/createTables.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#! /bin/bash

if [ ! -z "$DYNAMODB_CREATE_TABLES" ]
then
echo "Creating DynamoDB tables."
if [ -z "$DYNAMODB_TABLE_SCHEMA_PATH" ]
then
echo "DYNAMODB_TABLE_SCHEMA_PATH environment variable is empty. Defaulting to ./tableSchema.json."
export DYNAMODB_TABLE_SCHEMA_PATH="./tableSchema.json"
fi

jq -c '.[]' $DYNAMODB_TABLE_SCHEMA_PATH | while read i; do
TABLE_NAME=$(echo "$i" | jq '.TableName')
echo "Attempting to create table $TABLE_NAME"
aws dynamodb create-table --cli-input-json $i --endpoint-url http://localhost:8000 --region us-east-1 1> /dev/null
done
else
echo "Not creating DynamoDB tables."
fi
2 changes: 2 additions & 0 deletions dynamodb-local/dynamodb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /bin/bash
java -jar DynamoDBLocal.jar -inMemory
8 changes: 8 additions & 0 deletions dynamodb-local/dynamodbAdmin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /bin/bash
if [ ! -z "$DYNAMODB_ADMIN_ENABLED" ]
then
echo "Starting DynamoDB Admin"
dynamodb-admin
else
echo "DynamoDB Admin disabled. Not starting DynamoDB Admin"
fi
20 changes: 20 additions & 0 deletions dynamodb-local/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#! /bin/bash

# https://docs.docker.com/config/containers/multi-service_container/

# turn on bash's job control
set -m
set -e

# Start the primary dynamoDB process in the background
./dynamodb.sh &

# Try to start dynamodb-admin
./dynamodbAdmin.sh &

# Try to create tables in DynamoDB
./createTables.sh

# now we bring the primary process back into the foreground
# and leave it there
fg %1
22 changes: 22 additions & 0 deletions dynamodb-local/tableSchema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"TableName": "cache",
"KeySchema": [
{ "AttributeName": "key", "KeyType": "HASH" }
],
"AttributeDefinitions": [
{ "AttributeName": "key", "AttributeType": "S" }
],
"BillingMode": "PAY_PER_REQUEST"
},
{
"TableName": "rate-limit",
"KeySchema": [
{ "AttributeName": "rate-limit", "KeyType": "HASH" }
],
"AttributeDefinitions": [
{ "AttributeName": "rate-limit", "AttributeType": "S" }
],
"BillingMode": "PAY_PER_REQUEST"
}
]
Loading

0 comments on commit 29c53d9

Please sign in to comment.