Skip to content

Commit 8e4ff82

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

File tree

1 file changed

+74
-74
lines changed

1 file changed

+74
-74
lines changed

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

Lines changed: 74 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,21 @@ 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 \
21+
--db-cluster-identifier test-docdb-cluster \
22+
--engine docdb
23+
```
24+
25+
The output will be similar to the following:
2326

24-
```yaml
27+
```bash
2528
{
2629
"DBCluster": {
2730
"DBClusterIdentifier": "test-docdb-cluster",
@@ -63,12 +66,17 @@ created.
6366
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.
6467
To create a new database, we can use the `create-db-instance` command, like in this example:
6568

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 >}}
69+
```bash
70+
awslocal docdb create-db-instance \
71+
--db-instance-identifier test-company \
72+
--db-instance-class db.r5.large \
73+
--engine docdb \
74+
--db-cluster-identifier test-docdb-cluster
75+
```
7076

71-
```yaml
77+
The output will be similar to the following:
78+
79+
```bash
7280
{
7381
"DBInstance": {
7482
"DBInstanceIdentifier": "test-docdb-instance",
@@ -114,11 +122,15 @@ Some noticeable fields:
114122
.
115123
116124
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 >}}
120125
121-
```yaml
126+
```bash
127+
awslocal docdb describe-db-clusters \
128+
--db-cluster-identifier test-docdb-cluster
129+
```
130+
131+
The output will be similar to the following:
132+
133+
```bash
122134
{
123135
"DBClusters": [
124136
{
@@ -158,50 +170,35 @@ Interacting with the databases is done using `mongosh`, which is an official com
158170
It is designed to provide a modern and enhanced user experience for interacting with MongoDB
159171
databases.
160172
161-
{{< command >}}
173+
```bash
174+
mongosh mongodb://localhost:39045
175+
```
162176
163-
$ mongosh mongodb://localhost:39045
177+
The output will be similar to the following:
178+
179+
```bash
164180
Current Mongosh Log ID: 64a70b795697bcd4865e1b9a
165181
Connecting to: mongodb://localhost:
166182
39045/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.1
167183
Using MongoDB: 6.0.7
168184
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 >}}
185+
```
177186
178187
This command will default to accessing the `test` database that was created with the cluster.
179188
Notice the port, `39045`,
180189
which is the cluster port that appears in the aforementioned description.
181190
182191
To work with a specific database, the command is:
183192
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 >}}
193+
```bash
194+
mongosh mongodb://localhost:39045/test-company
195+
```
198196
199197
From here on we can manipulate collections
200198
using [the JavaScript methods provided](https://www.mongodb.com/docs/manual/reference/method/)
201199
by `mongosh`:
202200
203-
{{< command >}}
204-
201+
```bash
205202
test-company> db.createCollection("employees")
206203
{ ok: 1 }
207204
test-company> db.createCollection("customers")
@@ -210,20 +207,19 @@ test-company> show collections
210207
customers
211208
employees
212209
test-company> exit
213-
214-
{{< /command >}}
210+
```
215211
216212
For more information on how to use MongoDB with `mongosh` please refer to
217213
the [MongoDB documentation](https://www.mongodb.com/docs/).
218214
219215
### Connect to DocumentDB using Node.js Lambda
220216
221-
{{< callout >}}
217+
:::note
222218
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.
223219
224220
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 >}}
221+
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.
222+
:::
227223
228224
In this sample we will use a Node.js lambda function to connect to a DocumentDB.
229225
For the mongo-db connection we will use the `mongodb` lib.
@@ -235,29 +231,31 @@ We included a snippet at the very end.
235231
#### Create the DocDB Cluster with a username and password
236232
237233
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 \
234+
235+
```bash
236+
awslocal docdb create-db-cluster \
237+
--db-cluster-identifier test-docdb \
240238
--engine docdb \
241239
--master-user-password S3cretPwd! \
242240
--master-username someuser
243-
{{< /command >}}
241+
```
244242
245243
#### Prepare the lambda function
246244
247245
First, we create the zip required for the lambda function with the mongodb dependency.
248246
You will need [`npm`](https://docs.npmjs.com/) in order to install the dependencies.
249247
In your terminal run:
250248
251-
{{< command >}}
252-
$ mkdir resources
253-
$ cd resources
254-
$ mkdir node_modules
255-
$ npm install [email protected]
256-
{{< /command >}}
249+
```bash
250+
mkdir resources
251+
cd resources
252+
mkdir node_modules
253+
npm install [email protected]
254+
```
257255
258256
Next, copy the following code into a new file named `index.js` in the `resources` folder:
259257
260-
{{< command >}}
258+
```javascript
261259
const AWS = require('aws-sdk');
262260
const RDS = AWS.RDS;
263261
const { MongoClient } = require('mongodb');
@@ -305,35 +303,40 @@ exports.handler = async (event) => {
305303
};
306304
}
307305
};
308-
{{< /command >}}
306+
```
309307
310308
Now, you can zip the entire.
311309
Make sure you are inside `resources` directory and run:
312-
{{< command >}}
313-
$ zip -r function.zip .
314-
{{< /command >}}
310+
311+
```bash
312+
zip -r function.zip .
313+
```
315314
316315
Finally, we can create the `lambda` function using `awslocal`:
317-
{{< command >}}
318-
$ awslocal lambda create-function \
316+
317+
```bash
318+
awslocal lambda create-function \
319319
--function-name MyNodeLambda \
320320
--runtime nodejs16.x \
321321
--role arn:aws:iam::000000000000:role/lambda-role \
322322
--handler index.handler \
323323
--zip-file fileb://function.zip \
324324
--environment Variables="{DOCDB_CLUSTER_ID=test-docdb,DOCDB_SECRET=S3cretPwd!}"
325-
{{< /command >}}
325+
```
326326
327327
You can invoke the lambda by calling:
328-
{{< command >}}
329-
$ awslocal lambda invoke --function-name MyNodeLambda outfile
330-
{{< /command >}}
328+
329+
```bash
330+
awslocal lambda invoke \
331+
--function-name MyNodeLambda \
332+
outfile
333+
```
331334
332335
The `outfile` contains the returned value, e.g.:
333336
334-
```yaml
337+
```json
335338
{"statusCode":200,"body":"{\"_id\":\"6560a21ca7771a02ef128c72\",\"key\":\"value\"}"}
336-
````
339+
```
337340
338341
#### Use Secret To Connect to DocDB
339342
@@ -343,7 +346,7 @@ Secrets follow a [well-defined pattern](https://docs.aws.amazon.com/secretsmanag
343346
For the lambda function, you can pass the secret arn as `SECRET_NAME`.
344347
In the lambda, you can then retrieve the secret details like this:
345348
346-
{{< command >}}
349+
```javascript
347350
const AWS = require('aws-sdk');
348351
const { MongoClient } = require('mongodb');
349352
@@ -390,17 +393,14 @@ exports.handler = async (event) => {
390393
};
391394
}
392395
};
393-
394-
{{< /command >}}
396+
```
395397
396398
## Resource Browser
397399
398400
The LocalStack Web Application provides a Resource Browser for managing DocumentDB instances and clusters.
399401
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.
400402
401-
<img src="docdb-resource-browser.png" alt="DocumentDB Resource Browser" title="DocumentDB Resource Browser" width="900" />
402-
<br>
403-
<br>
403+
![DocumentDB Resource Browser](/images/aws/docdb-resource-browser.png)
404404
405405
The Resource Browser allows you to perform the following actions:
406406

0 commit comments

Comments
 (0)