0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS DynamoDB/DocumentDBをローカルで確認する手順(自分用メモ)

Last updated at Posted at 2020-08-06

概要

AWS DynamoDB/DocumentDBをローカルで確認する手順

環境

Microsoft Windows [Version 10.0.19041.388]
Docker version 19.03.8, build afacb8b

SAM CLI, version 0.53.0
aws-sdk@2.727.1

DynamoDB Local

DynamoDB ローカル セットアップ

DynamoDB ローカル (ダウンロード可能バージョン) のセットアップ
https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/DynamoDBLocal.html

に手順がある

DynamoDB Docker イメージをインストールする
https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/DynamoDBLocal.Docker.html
を実施

version: '3.7'
services:
 dynamodb-local:
   image: amazon/dynamodb-local:latest
   container_name: dynamodb-local
   ports:
    - "8000:8000"

コンソール出力

>docker-compose up
Creating network "sample-sam-node_default" with the default driver Pulling dynamodb-local (amazon/dynamodb-local:latest)...
latest: Pulling from amazon/dynamodb-local
638b75f800bf: Pull complete
a455ee0c5c58: Pull complete
c7e215ba3460: Pull complete
Digest: sha256:c8aeb014edfff8b93d7f14054cb9d2a1be214d62b7c5b61eb7fb40893a8404bb
Status: Downloaded newer image for amazon/dynamodb-local:latest
Creating dynamodb-local ... done
Attaching to dynamodb-local
dynamodb-local    | Initializing DynamoDB Local with the following configuration:
dynamodb-local    | Port:       8000
dynamodb-local    | InMemory:   true
dynamodb-local    | DbPath:     null
dynamodb-local    | SharedDb:   false
dynamodb-local    | shouldDelayTransientStatuses:       false
dynamodb-local    | CorsParams: *

GUIツールからの操作

dynamodb-admin
https://github.com/aaronshaf/dynamodb-admin

npm install -g dynamodb-admin
set DYNAMO_ENDPOINT=http://localhost:8000
dynamodb-admin

コンソール

>dynamodb-admin
  database endpoint:    http://localhost:8000
  region:               ap-northeast-1
  accessKey:            xxx

  dynamodb-admin listening on http://localhost:8001 (alternatively http://0.0.0.0:8001)

以下読み替え
ハッシュキー(Hash Attribute Name) → パーティションキー
レンジキー(Range Attribute Name (Optional)) → ソートキー

VSCodeからのデバッグ

  1. VS CodeにAWS Toolkitをインストール
  2. Code Lensから「Add Debug Configuration」をクリック
"launch": {
	"configurations": [
		{
			"type": "aws-sam",
			"request": "direct-invoke",
			"name": "sample-sam-node:app.lambdaHandler (nodejs10.x)",
			"invokeTarget": {
				"target": "code",
				"projectRoot": "hello-world",
				"lambdaHandler": "app.lambdaHandler"
			},
			"lambda": {
				"runtime": "nodejs10.x",
				"payload": {},
				"environmentVariables": {}
			}
		}
	]
}

コード編集後、VSCodeのDebugボタンを押すとsam build/invokeを呼び出してくれる
以下でdynamodb localへのアクセスを確認

let response;
var dynamoOpt = {
    apiVersion: '2012-08-10',
    endpoint: "http://192.168.1.X:8000"
};
var documentClient = new AWS.DynamoDB.DocumentClient(dynamoOpt);

exports.lambdaHandler = async (event, context) => {
    try {
        scanItems = await documentClient.scan({TableName: "sample_table"}).promise();
        response = {
            "statusCode": 200,
            "body": JSON.stringify(scanItems)
        };
    } catch (err) {
        console.log(err);
        return err;
    }

    return response
};

Mongo DB (Document DB)

Document DBがMongo DB互換なのでMongo DBを設置すればよい
Node.jsからの接続はMongoDB NodeJS Driverを利用し、接続先はローカルのIPアドレス

docker pull mongo
docker run --it --name sample-mongo mongo

参考

AWS SAM CLI + DynamoDB localを使ってローカル上で完結するAPI開発

MongoDBをDockerでインストールする

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?