DynamoDB local
AWS は、ローカル環境上で DynamoDB を使用したアプリケーションを開発・テストするための DynamoDB local
を提供している。
DynamoDB local
は以下の手順でデプロイ可能。
- DynamoDB local をローカルにインストールする
- Apache Maven リポジトリを追加して DynamoDB をデプロイする
- DynamoDB Docker イメージをインストールする
今回は Docker イメージをインストールして検証した。
Usage
Dynamo DB local
docker-compose
でコンテナをデプロイする。
docker-compose.yml
version: '3.7'
services:
dynamodb-local:
image: amazon/dynamodb-local:latest
container_name: dynamodb-local
ports:
- '8000:8000'
$ ls
docker-compose.yml
$ docker-compose up -d
これで Dynamo DB local
コンテナデプロイは完了。
ホスト OS にポート 8000
を開けているので、 http://localhost:8000/shell
をブラウザで開くと、ローカルの DynamoDB に対してコード経由で処理を実行できるエディタが開ける。
エディタは JS に対応している。
Code(JS)
AWS.config.endpoint = new AWS.Endpoint('http://localhost:8000');
let dynanodb = new AWS.DynamoDB();
let region = 'us-west-2';
AWS.config.update({
region: region,
});
let tableName = 'Articles';
let log = (err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
};
// create table
let createTable = () => {
let tableParams = {
TableName: tableName,
KeySchema: [{ AttributeName: 'ID', KeyType: 'HASH' }],
AttributeDefinitions: [{ AttributeName: 'ID', AttributeType: 'N' }],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10,
},
};
dynamodb.createTable(tableParams, log);
};
// check table existence
let checkTableParams = {
TableName: tableName,
};
dynamodb.describeTable(checkTableParams, function (err, data) {
if (err) {
createTable();
}
});
// PUT Item
let putParams = {
TableName: tableName,
Item: {
ID: { N: '1' },
Title: { S: 'title' },
Content: { S: 'content' },
},
};
dynamodb.putItem(putParams, log);
// GET Item
let getParams = {
TableName: tableName,
Key: {
ID: { N: '1' },
},
};
dynamodb.getItem(getParams, log);
=>
{}
{"Item":{"Title":{"S":"title"},"Content":{"S":"content"},"ID":{"N":"1"}}}