@@ -36,12 +36,14 @@ Code Placeholders
36
36
The code examples on this page use the following placeholders:
37
37
38
38
- ``<hostname>``: The network address of your MongoDB Atlas deployment
39
- - ``<awsKeyId >``: Your AWS access key ID
40
- - ``<awsSecretKey >``: Your AWS secret access key
41
- - ``<awsSessionToken >``: Your AWS session token
39
+ - ``<AWS IAM access key ID >``: Your AWS access key ID
40
+ - ``<AWS IAM secret access key >``: Your AWS secret access key
41
+ - ``<AWS session token >``: Your AWS session token
42
42
43
43
To use the code examples on this page, replace these placeholders with your own values.
44
44
45
+ .. include:: /includes/authentication/percent-encoding.rst
46
+
45
47
Using AWS IAM Authentication in Your Application
46
48
------------------------------------------------
47
49
@@ -56,7 +58,7 @@ To use AWS IAM authentication, you must install {+driver-short+} with the
56
58
tries to retrieve AWS credentials from the following sources, in the order listed:
57
59
58
60
1. Named arguments passed to the ``MongoClient`` constructor or parameters in the
59
- connection URI
61
+ connection string
60
62
#. Environment variables
61
63
#. Shared credentials file
62
64
#. AWS config file
@@ -74,13 +76,13 @@ these sources and use them to authenticate your application.
74
76
75
77
First, {+driver-short+} checks whether you passed AWS credentials
76
78
to the ``MongoClient`` constructor, either as a named argument or as part of the
77
- connection URI . To pass your credentials to ``MongoClient``,
79
+ connection string . To pass your credentials to ``MongoClient``,
78
80
set the following connection options:
79
81
80
82
- ``username``: The AWS IAM access key ID to authenticate. Percent-encode this value
81
- before including it in a connection URI .
83
+ before including it in a connection string .
82
84
- ``password``: The AWS IAM secret access key. Percent-encode this value before including
83
- it in a connection URI .
85
+ it in a connection string .
84
86
- ``authMechanism``: Set to ``"MONGODB-AWS"``.
85
87
86
88
You can set these options in two ways: by passing arguments to the
@@ -93,7 +95,7 @@ You can set these options in two ways: by passing arguments to the
93
95
94
96
.. code-block:: python
95
97
96
- client = pymongo.MongoClient("mongodb://@ <hostname>:<port >",
98
+ client = pymongo.MongoClient("mongodb+srv ://<hostname>",
97
99
username="<AWS IAM access key ID>",
98
100
password="<AWS IAM secret access key>",
99
101
authMechanism="MONGODB-AWS")
@@ -103,9 +105,9 @@ You can set these options in two ways: by passing arguments to the
103
105
104
106
.. code-block:: python
105
107
106
- uri = ("mongodb://<percent-encoded AWS IAM access key ID>:"
108
+ uri = ("mongodb+srv ://<percent-encoded AWS IAM access key ID>:"
107
109
"<percent-encoded AWS IAM secret access key>"
108
- "@<hostname>:<port> /?"
110
+ "@<hostname>/?"
109
111
"&authMechanism=MONGODB-AWS")
110
112
client = pymongo.MongoClient(uri)
111
113
@@ -148,15 +150,15 @@ You can set this option in two ways: by passing an argument to the
148
150
149
151
.. code-block:: python
150
152
151
- client = pymongo.MongoClient("mongodb://<hostname>:<port >",
153
+ client = pymongo.MongoClient("mongodb+srv ://<hostname>",
152
154
authMechanism="MONGODB-AWS")
153
155
154
156
.. tab:: Connection String
155
157
:tabid: connectionstring
156
158
157
159
.. code-block:: python
158
160
159
- uri = "mongodb://<hostname>:<port >/?&authMechanism=MONGODB-AWS"
161
+ uri = "mongodb+srv ://<hostname>/?&authMechanism=MONGODB-AWS"
160
162
client = pymongo.MongoClient(uri)
161
163
162
164
.. tip:: AWS Lambda
@@ -194,15 +196,15 @@ You can set this option in two ways: by passing an argument to the
194
196
195
197
.. code-block:: python
196
198
197
- client = pymongo.MongoClient("mongodb://<hostname>:<port >",
199
+ client = pymongo.MongoClient("mongodb+srv ://<hostname>",
198
200
authMechanism="MONGODB-AWS")
199
201
200
202
.. tab:: Connection String
201
203
:tabid: connectionstring
202
204
203
205
.. code-block:: python
204
206
205
- uri = "mongodb://<hostname>:<port >/?&authMechanism=MONGODB-AWS"
207
+ uri = "mongodb+srv ://<hostname>/?&authMechanism=MONGODB-AWS"
206
208
client = pymongo.MongoClient(uri)
207
209
208
210
.. tip::
@@ -244,15 +246,15 @@ You can set this option in two ways: by passing an argument to the
244
246
245
247
.. code-block:: python
246
248
247
- client = pymongo.MongoClient("mongodb://<hostname>:<port >",
249
+ client = pymongo.MongoClient("mongodb+srv ://<hostname>",
248
250
authMechanism="MONGODB-AWS")
249
251
250
252
.. tab:: Connection String
251
253
:tabid: connectionstring
252
254
253
255
.. code-block:: python
254
256
255
- uri = "mongodb://<hostname>:<port >/?&authMechanism=MONGODB-AWS"
257
+ uri = "mongodb+srv ://<hostname>/?&authMechanism=MONGODB-AWS"
256
258
client = pymongo.MongoClient(uri)
257
259
258
260
.. _pymongo-mongodb-aws-assume-role:
@@ -275,11 +277,11 @@ in the AWS documentation.
275
277
After you create the config file, set the following connection options:
276
278
277
279
- ``username``: The AWS IAM access key ID to authenticate returned by the ``AssumeRole``
278
- request. Percent-encode this value before including it in a connection URI .
280
+ request. Percent-encode this value before including it in a connection string .
279
281
- ``password``: The AWS IAM secret access key returned by the ``AssumeRole`` request.
280
- Percent-encode this value before including it in a connection URI. .
282
+ Percent-encode this value before including it in a connection string .
281
283
- ``authMechanismProperties``: Set to ``AWS_SESSION_TOKEN:`` and the
282
- AWS session token returned by the ``AssumeRole`` request.
284
+ AWS session token returned by the ``AssumeRole`` request.
283
285
- ``authMechanism``: Set to ``"MONGODB-AWS"``.
284
286
285
287
You can set these options in two ways: by passing arguments to the
@@ -294,7 +296,7 @@ You can set these options in two ways: by passing arguments to the
294
296
295
297
.. code-block:: python
296
298
297
- client = pymongo.MongoClient("mongodb://@<hostname>:<port >",
299
+ client = pymongo.MongoClient("mongodb+srv ://@<hostname>",
298
300
username="<AWS IAM access key ID>",
299
301
password="<AWS IAM secret access key>",
300
302
authMechanismProperties="AWS_SESSION_TOKEN:<AWS session token>",
@@ -305,9 +307,9 @@ You can set these options in two ways: by passing arguments to the
305
307
306
308
.. code-block:: python
307
309
308
- uri = ("mongodb://<percent-encoded AWS IAM access key ID>:"
310
+ uri = ("mongodb+srv ://<percent-encoded AWS IAM access key ID>:"
309
311
"<percent-encoded AWS IAM secret access key>"
310
- "@<hostname>:<port> /?"
312
+ "@<hostname>/?"
311
313
"authMechanismProperties=AWS_SESSION_TOKEN:<AWS session token>"
312
314
"&authMechanism=MONGODB-AWS")
313
315
client = pymongo.MongoClient(uri)
@@ -353,15 +355,15 @@ You can set this option in two ways: by passing an argument to the
353
355
354
356
.. code-block:: python
355
357
356
- client = pymongo.MongoClient("mongodb://<hostname>:<port >",
358
+ client = pymongo.MongoClient("mongodb+srv ://<hostname>",
357
359
authMechanism="MONGODB-AWS")
358
360
359
361
.. tab:: Connection String
360
362
:tabid: connectionstring
361
363
362
364
.. code-block:: python
363
365
364
- uri = "mongodb://<hostname>:<port >/?&authMechanism=MONGODB-AWS"
366
+ uri = "mongodb+srv ://<hostname>/?&authMechanism=MONGODB-AWS"
365
367
client = pymongo.MongoClient(uri)
366
368
367
369
For more information about using an ``AssumeRoleWithWebIdentity`` request to
@@ -391,15 +393,15 @@ You can set this option in two ways: by passing an argument to the
391
393
392
394
.. code-block:: python
393
395
394
- client = pymongo.MongoClient("mongodb://<hostname>:<port >",
396
+ client = pymongo.MongoClient("mongodb+srv ://<hostname>",
395
397
authMechanism="MONGODB-AWS")
396
398
397
399
.. tab:: Connection String
398
400
:tabid: connectionstring
399
401
400
402
.. code-block:: python
401
403
402
- uri = "mongodb://<hostname>:<port >/?&authMechanism=MONGODB-AWS"
404
+ uri = "mongodb+srv ://<hostname>/?&authMechanism=MONGODB-AWS"
403
405
client = pymongo.MongoClient(uri)
404
406
405
407
API Documentation
0 commit comments