Skip to content

Commit 98d031b

Browse files
committed
DOCSP-46858: Add custom AWS credential documentation
1 parent 1751141 commit 98d031b

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// start-custom-credentials
2+
const { MongoClient } = require('mongodb');
3+
const { fromNodeProviderChain } = require('@aws-sdk/credential-providers');
4+
5+
const client = new MongoClient('<cluster_url>?authMechanism=MONGODB-AWS', {
6+
authMechanismProperties: {
7+
AWS_CREDENTIAL_PROVIDER: fromNodeProviderChain()
8+
}
9+
});
10+
// end-custom-credentials
11+
12+
// start-custom-credentials-function
13+
const { MongoClient } = require('mongodb');
14+
15+
const client = new MongoClient('<cluster_url>?authMechanism=MONGODB-AWS', {
16+
authMechanismProperties: {
17+
AWS_CREDENTIAL_PROVIDER: async () => {
18+
return {
19+
accessKeyId: process.env.ACCESS_KEY_ID,
20+
secretAccessKey: process.env.SECRET_ACCESS_KEY
21+
}
22+
}
23+
}
24+
});
25+
// end-custom-credentials-function

source/security/authentication/aws-iam.txt

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,31 @@ The driver checks for your credentials in the following sources in order:
153153
.. literalinclude:: /code-snippets/authentication/aws-env-variable.js
154154
:language: javascript
155155

156-
.. important:: Retrieval of AWS Credentials
157-
158-
Starting in MongoDB version 4.11, when you install the optional
159-
``aws-sdk/credential-providers`` dependency, the driver uses the AWS SDK
160-
to retrieve credentials from the environment. As a result, if you
161-
have a shared AWS credentials file or config file, the driver will
162-
use those credentials by default.
163-
164-
You can override this behavior by performing one of the following
165-
actions:
166-
167-
- Set ``AWS_SHARED_CREDENTIALS_FILE`` variable in your shell to point
168-
to your credentials file.
169-
- Set the equivalent environment variable in your application to point
170-
to your credentials file.
171-
- Create an AWS profile for your MongoDB credentials and set the
172-
``AWS_PROFILE`` environment variable to that profile name.
156+
Retrieving AWS Credentials
157+
--------------------------
158+
159+
When you install the optional ``aws-sdk/credential-providers`` dependency, the driver
160+
uses the AWS SDK to retrieve credentials from the environment. If you have a shared AWS
161+
credentials file or config file, the driver uses those credentials by default.
162+
163+
To manually specify the AWS credentials to retrieve, you can use the ``AWS_CREDENTIAL_PROVIDER``
164+
property to specify the credential provider. The following example passes a provider chain
165+
from the AWS SDK to the AWS authentication mechanism:
166+
167+
.. literalinclude:: /code-snippets/authentication/aws-custom-credentials.js
168+
:language: javascript
169+
:start-after: // start-custom-credentials
170+
:end-before: // end-custom-credentials
171+
172+
TO use a custom provider, you can pass any async function that returns your credentials
173+
to the ``AWS_CREDENTIAL_PROVIDER`` property. The following example shows how to pass
174+
a custom provider function that fetches credentials from environment variables to the
175+
AWS authentication mechanism:
176+
177+
.. literalinclude:: /code-snippets/authentication/aws-custom-credentials.js
178+
:language: javascript
179+
:start-after: // start-custom-credentials-function
180+
:end-before: // end-custom-credentials-function
173181

174182
API Documentation
175183
-----------------

0 commit comments

Comments
 (0)