Skip to content

Commit 8f54a19

Browse files
committed
revamp docdb
1 parent 66e9f91 commit 8f54a19

File tree

1 file changed

+72
-74
lines changed

1 file changed

+72
-74
lines changed

src/content/docs/aws/services/docdb.md

Lines changed: 72 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
title: "DocumentDB (DocDB)"
3-
linkTitle: "DocumentDB (DocDB)"
43
tags: ["Ultimate"]
54
description: Get started with AWS DocumentDB on LocalStack
65
---
@@ -11,17 +10,19 @@ DocumentDB is a fully managed, non-relational database service that supports Mon
1110
DocumentDB is compatible with MongoDB, meaning you can use the same MongoDB drivers, applications, and tools to run, manage, and scale workloads on DocumentDB without having to worry about managing the underlying infrastructure.
1211

1312
LocalStack allows you to use the DocumentDB APIs to create and manage DocumentDB clusters and instances.
14-
The supported APIs are available on our [API coverage page]({{< ref "coverage_docdb" >}}), which provides information on the extent of DocumentDB's integration with LocalStack.
13+
The supported APIs are available on our [API coverage page](), which provides information on the extent of DocumentDB's integration with LocalStack.
1514

1615
## Getting started
1716

1817
To create a new DocumentDB cluster we use the `create-db-cluster` command as follows:
1918

20-
{{< command >}}
21-
$ awslocal docdb create-db-cluster --db-cluster-identifier test-docdb-cluster --engine docdb
22-
{{< /command >}}
19+
```bash
20+
awslocal docdb create-db-cluster --db-cluster-identifier test-docdb-cluster --engine docdb
21+
```
22+
23+
The output will be similar to the following:
2324

24-
```yaml
25+
```bash
2526
{
2627
"DBCluster": {
2728
"DBClusterIdentifier": "test-docdb-cluster",
@@ -63,12 +64,17 @@ created.
6364
As we did not specify a `MasterUsername` or `MasterUserPassword` for the creation of the database, the mongo-db will not set any credentials when starting the docker container.
6465
To create a new database, we can use the `create-db-instance` command, like in this example:
6566

66-
{{< command >}}
67-
$ awslocal docdb create-db-instance --db-instance-identifier test-company \
68-
--db-instance-class db.r5.large --engine docdb --db-cluster-identifier test-docdb-cluster
69-
{{< /command >}}
67+
```bash
68+
awslocal docdb create-db-instance \
69+
--db-instance-identifier test-company \
70+
--db-instance-class db.r5.large \
71+
--engine docdb \
72+
--db-cluster-identifier test-docdb-cluster
73+
```
7074

71-
```yaml
75+
The output will be similar to the following:
76+
77+
```bash
7278
{
7379
"DBInstance": {
7480
"DBInstanceIdentifier": "test-docdb-instance",
@@ -114,11 +120,15 @@ Some noticeable fields:
114120
.
115121
116122
To obtain detailed information about the cluster, we use the `describe-db-cluster` command:
117-
{{< command >}}
118-
$ awslocal docdb describe-db-clusters --db-cluster-identifier test-docdb-cluster
119-
{{< /command >}}
120123
121-
```yaml
124+
```bash
125+
awslocal docdb describe-db-clusters \
126+
--db-cluster-identifier test-docdb-cluster
127+
```
128+
129+
The output will be similar to the following:
130+
131+
```bash
122132
{
123133
"DBClusters": [
124134
{
@@ -158,50 +168,35 @@ Interacting with the databases is done using `mongosh`, which is an official com
158168
It is designed to provide a modern and enhanced user experience for interacting with MongoDB
159169
databases.
160170
161-
{{< command >}}
171+
```bash
172+
mongosh mongodb://localhost:39045
173+
```
162174
163-
$ mongosh mongodb://localhost:39045
175+
The output will be similar to the following:
176+
177+
```bash
164178
Current Mongosh Log ID: 64a70b795697bcd4865e1b9a
165179
Connecting to: mongodb://localhost:
166180
39045/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.1
167181
Using MongoDB: 6.0.7
168182
Using Mongosh: 1.10.1
169-
170-
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
171-
172-
------
173-
174-
test>
175-
176-
{{< /command >}}
183+
```
177184
178185
This command will default to accessing the `test` database that was created with the cluster.
179186
Notice the port, `39045`,
180187
which is the cluster port that appears in the aforementioned description.
181188
182189
To work with a specific database, the command is:
183190
184-
{{< command >}}
185-
$ mongosh mongodb://localhost:39045/test-company
186-
Current Mongosh Log ID: 64a71916fae7fdeeb8b43a73
187-
Connecting to: mongodb://localhost:
188-
39045/test-company?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.1
189-
Using MongoDB: 6.0.7
190-
Using Mongosh: 1.10.1
191-
192-
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
193-
194-
------
195-
test-company>
196-
197-
{{< /command >}}
191+
```bash
192+
mongosh mongodb://localhost:39045/test-company
193+
```
198194
199195
From here on we can manipulate collections
200196
using [the JavaScript methods provided](https://www.mongodb.com/docs/manual/reference/method/)
201197
by `mongosh`:
202198
203-
{{< command >}}
204-
199+
```bash
205200
test-company> db.createCollection("employees")
206201
{ ok: 1 }
207202
test-company> db.createCollection("customers")
@@ -210,20 +205,19 @@ test-company> show collections
210205
customers
211206
employees
212207
test-company> exit
213-
214-
{{< /command >}}
208+
```
215209
216210
For more information on how to use MongoDB with `mongosh` please refer to
217211
the [MongoDB documentation](https://www.mongodb.com/docs/).
218212
219213
### Connect to DocumentDB using Node.js Lambda
220214
221-
{{< callout >}}
215+
:::note
222216
You need to set `DOCDB_PROXY_CONTAINER=1` when starting LocalStack to be able to use the returned `Endpoint`, which will be correctly resolved automatically.
223217
224218
The flag `DOCDB_PROXY_CONTAINER=1` changes the default behavior and the container will be started as proxied container.
225-
Meaning a port from the [pre-defined port]({{< ref "/references/external-ports" >}}) range will be chosen, and when using lambda, you can use `localhost.localstack.cloud` to connect to the instance.
226-
{{< /callout >}}
219+
Meaning a port from the [pre-defined port]() range will be chosen, and when using lambda, you can use `localhost.localstack.cloud` to connect to the instance.
220+
:::
227221
228222
In this sample we will use a Node.js lambda function to connect to a DocumentDB.
229223
For the mongo-db connection we will use the `mongodb` lib.
@@ -235,29 +229,31 @@ We included a snippet at the very end.
235229
#### Create the DocDB Cluster with a username and password
236230
237231
We assume you have a `MasterUsername` and `MasterUserPassword` set for DocDB e.g:
238-
{{< command >}}
239-
$ awslocal docdb create-db-cluster --db-cluster-identifier test-docdb \
232+
233+
```bash
234+
awslocal docdb create-db-cluster \
235+
--db-cluster-identifier test-docdb \
240236
--engine docdb \
241237
--master-user-password S3cretPwd! \
242238
--master-username someuser
243-
{{< /command >}}
239+
```
244240
245241
#### Prepare the lambda function
246242
247243
First, we create the zip required for the lambda function with the mongodb dependency.
248244
You will need [`npm`](https://docs.npmjs.com/) in order to install the dependencies.
249245
In your terminal run:
250246
251-
{{< command >}}
252-
$ mkdir resources
253-
$ cd resources
254-
$ mkdir node_modules
255-
$ npm install [email protected]
256-
{{< /command >}}
247+
```bash
248+
mkdir resources
249+
cd resources
250+
mkdir node_modules
251+
npm install [email protected]
252+
```
257253
258254
Next, copy the following code into a new file named `index.js` in the `resources` folder:
259255
260-
{{< command >}}
256+
```javascript
261257
const AWS = require('aws-sdk');
262258
const RDS = AWS.RDS;
263259
const { MongoClient } = require('mongodb');
@@ -305,35 +301,40 @@ exports.handler = async (event) => {
305301
};
306302
}
307303
};
308-
{{< /command >}}
304+
```
309305
310306
Now, you can zip the entire.
311307
Make sure you are inside `resources` directory and run:
312-
{{< command >}}
313-
$ zip -r function.zip .
314-
{{< /command >}}
308+
309+
```bash
310+
zip -r function.zip .
311+
```
315312
316313
Finally, we can create the `lambda` function using `awslocal`:
317-
{{< command >}}
318-
$ awslocal lambda create-function \
314+
315+
```bash
316+
awslocal lambda create-function \
319317
--function-name MyNodeLambda \
320318
--runtime nodejs16.x \
321319
--role arn:aws:iam::000000000000:role/lambda-role \
322320
--handler index.handler \
323321
--zip-file fileb://function.zip \
324322
--environment Variables="{DOCDB_CLUSTER_ID=test-docdb,DOCDB_SECRET=S3cretPwd!}"
325-
{{< /command >}}
323+
```
326324
327325
You can invoke the lambda by calling:
328-
{{< command >}}
329-
$ awslocal lambda invoke --function-name MyNodeLambda outfile
330-
{{< /command >}}
326+
327+
```bash
328+
awslocal lambda invoke \
329+
--function-name MyNodeLambda \
330+
outfile
331+
```
331332
332333
The `outfile` contains the returned value, e.g.:
333334
334-
```yaml
335+
```json
335336
{"statusCode":200,"body":"{\"_id\":\"6560a21ca7771a02ef128c72\",\"key\":\"value\"}"}
336-
````
337+
```
337338
338339
#### Use Secret To Connect to DocDB
339340
@@ -343,7 +344,7 @@ Secrets follow a [well-defined pattern](https://docs.aws.amazon.com/secretsmanag
343344
For the lambda function, you can pass the secret arn as `SECRET_NAME`.
344345
In the lambda, you can then retrieve the secret details like this:
345346
346-
{{< command >}}
347+
```javascript
347348
const AWS = require('aws-sdk');
348349
const { MongoClient } = require('mongodb');
349350
@@ -390,17 +391,14 @@ exports.handler = async (event) => {
390391
};
391392
}
392393
};
393-
394-
{{< /command >}}
394+
```
395395
396396
## Resource Browser
397397
398398
The LocalStack Web Application provides a Resource Browser for managing DocumentDB instances and clusters.
399399
You can access the Resource Browser by opening the LocalStack Web Application in your browser, navigating to the **Resources** section, and then clicking on **DocumentDB** under the **Database** section.
400400
401-
<img src="docdb-resource-browser.png" alt="DocumentDB Resource Browser" title="DocumentDB Resource Browser" width="900" />
402-
<br>
403-
<br>
401+
![DocumentDB Resource Browser](/images/aws/docdb-resource-browser.png)
404402
405403
The Resource Browser allows you to perform the following actions:
406404

0 commit comments

Comments
 (0)