Amazon DocumentDB が Amazon EC2 との 1 クリック EC2 接続をサポートしました

2023.12.16

こんにちは、森田です。

以下のアップデートで Amazon DocumentDB が Amazon EC2 との1クリックEC2接続をサポートするようになりました。

この機能を利用することで、セキュリティグループの設定などが自動で行われるため、サクッと Amazon DocumentDB に接続できるようになります。

以前から RDS では、サポートされていたので、今回のアップデートで DocumentDB でも使えるようになりました。

では、実際に、本ブログでは、1クリックでDocumentDBとEC2を接続してみたいと思います。

やってみた

EC2 インスタンスの起動

AWSマネジメントコンソールから、EC2インスタンスの起動を行います。

OSについては、Amazon Linux2を選択します。インスタンスタイプなどはデフォルト値のまま進めます。

セキュリティグループについては、新規で作成を行い、手元の端末から接続できるようにSSH許可を設定を行い、作成を完了させます。

作成完了後、インスタンスIDをメモしておきます。

DocumentDB クラスターの起動

同様にAWSマネジメントコンソールから、DocumentDB クラスターの起動を行います。

今回は検証のみなので、インスタンス数は1とします。

Connectivityにて、Amazon EC2 との1クリック接続を設定できます。

Connect to an EC2 compute resourceを選択し、先ほどメモしていたインスタンスIDを選択します。

以下にて認証情報を設定し、作成を完了させます。

EC2インスタンスからDocumentDBへ接続

EC2インスタンス、DocumentDBのステータスが、Availableとなっていることを確認を行います。

確認後、EC2インスタンスへSSH接続を行い、以下のコマンドで mongo shell をインストールします。

$ echo -e "[mongodb-org-5.0] \nname=MongoDB Repository\nbaseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/5.0/x86_64/\ngpgcheck=1 \nenabled=1 \ngpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc" | sudo tee /etc/yum.repos.d/mongodb-org-5.0.repo
$ sudo yum install -y mongodb-org-shell

接続のコマンドは、DocumentDBクラスターのConnectivity & securityタブに記載があるものを利用します。

以下のように、mongo shellに入ることができました!

実行結果

$ mongo --ssl --host docdb-2023-12-16-04-15-12.cluster-czdia394lloi.ap-northeast-1.docdb.amazonaws.com:27017 --sslCAFile global-bundle.pem --username sample --password ****

Warning: Non-Genuine MongoDB Detected

This server or service appears to be an emulation of MongoDB rather than an official MongoDB product.

Some documented MongoDB features may work differently, be entirely missing or incomplete, or have unexpected performance characteristics.

To learn more please visit: https://dochub.mongodb.org/core/non-genuine-mongodb-server-warning.
rs0:PRIMARY>

さいごに

1クリックセットアップを利用することで、簡単に DocumentDB に接続することができました。

検証といったサクッと接続してみたい際などには非常に便利なので、ぜひご活用ください!