{DocumentDB}監査の有効化


-- 1. クラスターパラメータグループの作成

aws docdb create-db-cluster-parameter-group \
--db-cluster-parameter-group-name cpg01 \
--db-parameter-group-family docdb4.0 \
--description "cpg01"

aws docdb describe-db-cluster-parameters \
--db-cluster-parameter-group-name cpg01


aws docdb modify-db-cluster-parameter-group \
--db-cluster-parameter-group-name cpg01 \
--parameters "ParameterName"=audit_logs,"ParameterValue"=enabled,"ApplyMethod"=immediate

 

-- 2. Amazon DocumentDB クラスターの作成

aws docdb create-db-cluster \
--db-cluster-identifier cluster01 \
--engine docdb \
--engine-version 4.0.0 \
--port 27017 \
--master-username test \
--master-user-password 'password' \
--no-storage-encrypted \
--no-deletion-protection \
--db-cluster-parameter-group-name cpg01 \
--enable-cloudwatch-logs-exports audit


aws docdb create-db-instance \
--db-instance-identifier instance01 \
--db-instance-class db.t3.medium \
--engine docdb \
--no-auto-minor-version-upgrade \
--db-cluster-identifier cluster01

 

aws docdb describe-db-clusters \
--filter Name=engine,Values=docdb \
--db-cluster-identifier cluster01


aws docdb describe-db-instances \
--filter Name=engine,Values=docdb \
--db-instance-identifier instance01


-- 3. 動作確認

wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
mongo --ssl --host cluster01.cluster-xxxxxxxxxxxx.ap-northeast-1.docdb.amazonaws.com:27017 --sslCAFile rds-combined-ca-bundle.pem --username test

--コレクション作成とデータ追加
db.tab1.insert({_id:1,col2:"val12",col3:13});
db.tab1.insert({_id:2,col2:"val22",col3:23});

db.tab2.insert({_id:"val11",col2: 12});

--データ追加せずにコレクション作成
db.createCollection('tab3');


--コレクション確認(データベースはコレクション作成時に作成される)
show dbs
show collections

--データ取得
db.tab1.find()
db.tab1.find({_id:2});

--データ更新
db.tab1.update({_id:2 }, { $set : { col2: "newval22" } });
db.tab1.find({_id:2});


--データ削除
db.tab1.remove({_id:1 })
db.tab1.find({_id:1});

--コレクション削除
db.tab1.drop()
show dbs
show collections


--データベース削除
db.dropDatabase();
show dbs
show collections

 


CloudWatchの「/aws/docdb/cluster01/audit」にログが出力される


-- 4. クリーンアップ

-- Amazon DocumentDBインスタンス削除

aws docdb delete-db-instance \
--db-instance-identifier instance01

-- Amazon DocumentDBクラスター削除

aws docdb delete-db-cluster \
--db-cluster-identifier cluster01 \
--skip-final-snapshot


-- クラスタパラメータグループ削除
aws docdb delete-db-cluster-parameter-group \
--db-cluster-parameter-group-name cpg01


-- クラスタパラメータグループ一覧
aws docdb describe-db-cluster-parameter-groups | grep DBClusterParameterGroupName