|
| 1 | +.. _node-authentication-aws: |
| 2 | + |
| 3 | +================================ |
| 4 | +AWS IAM Authentication Mechanism |
| 5 | +================================ |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 3 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: atlas, amazon web services, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +The ``MONGODB-AWS`` authentication mechanism uses Amazon Web Services |
| 24 | +Identity and Access Management (AWS IAM) credentials to authenticate a user to MongoDB. |
| 25 | +You can use this mechanism only when authenticating to MongoDB Atlas. |
| 26 | + |
| 27 | +.. tip:: Configure Atlas for AWS IAM Authentication |
| 28 | + |
| 29 | + To learn more about configuring MongoDB Atlas for AWS IAM authentication, see |
| 30 | + :atlas:`Set Up Authentication with AWS IAM </security/aws-iam-authentication/>` in |
| 31 | + the Atlas documentation. |
| 32 | + |
| 33 | +Specify MONGODB-AWS Authentication |
| 34 | +---------------------------------- |
| 35 | + |
| 36 | +The ``MONGODB-AWS`` authentication mechanism uses your Amazon Web Services |
| 37 | +Identity and Access Management (AWS IAM) credentials to authenticate your |
| 38 | +user. If you do not already have the `AWS signature library |
| 39 | +<https://www.npmjs.com/package/aws4>`__, use the following |
| 40 | +``npm`` command to install it: |
| 41 | + |
| 42 | +.. code-block:: bash |
| 43 | + |
| 44 | + npm install aws4 |
| 45 | + |
| 46 | +To connect to a MongoDB instance with ``MONGODB-AWS`` authentication |
| 47 | +enabled, specify the ``MONGODB-AWS`` authentication mechanism. |
| 48 | + |
| 49 | +The driver checks for your credentials in the following sources in order: |
| 50 | + |
| 51 | +1. Connection string |
| 52 | +#. Environment variables |
| 53 | +#. Web identity token file |
| 54 | +#. AWS ECS endpoint specified in ``AWS_CONTAINER_CREDENTIALS_RELATIVE_URI`` |
| 55 | +#. AWS EC2 endpoint. For more information, see `IAM Roles for Tasks |
| 56 | + <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html>`_. |
| 57 | + |
| 58 | +.. important:: |
| 59 | + |
| 60 | + The driver only reads the credentials from the first method that it detects |
| 61 | + in the order as given by the preceding list. For example, if you specify |
| 62 | + your AWS credentials in the connection string, the driver ignores any |
| 63 | + credentials that you specified in environment variables. |
| 64 | + |
| 65 | +.. tabs:: |
| 66 | + |
| 67 | + .. tab:: Connection String |
| 68 | + :tabid: connection string |
| 69 | + |
| 70 | + To connect to your MongoDB instance with a connection string, pass |
| 71 | + your ``AWS_ACCESS_KEY_ID`` and ``AWS_SECRET_ACCESS_KEY`` |
| 72 | + credentials to the driver when you attempt to connect. If your AWS |
| 73 | + login requires a session token, include your ``AWS_SESSION_TOKEN`` as well. |
| 74 | + |
| 75 | + The following code shows an example of specifying the ``MONGODB-AWS`` |
| 76 | + authentication mechanism and credentials with a connection string: |
| 77 | + |
| 78 | + .. important:: |
| 79 | + |
| 80 | + Always **URI encode** the username and certificate file path using the |
| 81 | + ``encodeURIComponent`` method to ensure they are correctly parsed. |
| 82 | + |
| 83 | + .. literalinclude:: /code-snippets/authentication/aws.js |
| 84 | + :language: javascript |
| 85 | + |
| 86 | + .. tab:: Environment Variables |
| 87 | + :tabid: environment variables |
| 88 | + |
| 89 | + To authenticate to your MongoDB instance using AWS credentials stored in |
| 90 | + environment variables, set the following variables by using |
| 91 | + a shell: |
| 92 | + |
| 93 | + .. code-block:: bash |
| 94 | + |
| 95 | + export AWS_ACCESS_KEY_ID=<awsKeyId> |
| 96 | + export AWS_SECRET_ACCESS_KEY=<awsSecretKey> |
| 97 | + export AWS_SESSION_TOKEN=<awsSessionToken> |
| 98 | + |
| 99 | + .. note:: |
| 100 | + |
| 101 | + Omit the line containing ``AWS_SESSION_TOKEN`` if you don't need an AWS |
| 102 | + session token for that role. |
| 103 | + |
| 104 | + After you've set the preceding environment variables, specify the ``MONGODB-AWS`` |
| 105 | + authentication mechanism in your connection string as shown in the following example: |
| 106 | + |
| 107 | + .. literalinclude:: /code-snippets/authentication/aws-env-variable.js |
| 108 | + :language: javascript |
| 109 | + |
| 110 | + .. tab:: Web Identity Token File |
| 111 | + :tabid: web-identity-token-file |
| 112 | + |
| 113 | + You can use the OpenID Connect (OIDC) token obtained from a web identity |
| 114 | + provider to authenticate to Amazon Elastic Kubernetes Service (EKS) or |
| 115 | + other services. |
| 116 | + |
| 117 | + To authenticate with your OIDC token you must first install |
| 118 | + `@aws-sdk/credential-providers |
| 119 | + <https://www.npmjs.com/package/@aws-sdk/credential-providers>`__. You can |
| 120 | + install this dependency using the following ``npm`` command: |
| 121 | + |
| 122 | + .. code-block:: bash |
| 123 | + |
| 124 | + npm install @aws-sdk/credential-providers |
| 125 | + |
| 126 | + Next, create a file that contains your OIDC token. Then |
| 127 | + set the absolute path to this file in an environment variable by using |
| 128 | + a shell as shown in the following example: |
| 129 | + |
| 130 | + .. code-block:: bash |
| 131 | + |
| 132 | + export AWS_WEB_IDENTITY_TOKEN_FILE=<absolute path to file containing your OIDC token> |
| 133 | + |
| 134 | + AWS recommends using regional AWS STS endpoints instead of global |
| 135 | + endpoints to reduce latency, build-in redundancy, and increase session token validity. |
| 136 | + To set the AWS region, set `AWS_REGION <https://docs.aws.amazon.com/sdkref/latest/guide/feature-region.html>`__ |
| 137 | + and `AWS_STS_REGIONAL_ENDPOINTS <https://docs.aws.amazon.com/sdkref/latest/guide/feature-sts-regionalized-endpoints.html>`__ |
| 138 | + as environment variables, as shown in the following example: |
| 139 | + |
| 140 | + .. code-block:: bash |
| 141 | + |
| 142 | + export AWS_STS_REGIONAL_ENDPOINTS=regional // Enables regional endpoints |
| 143 | + export AWS_REGION=us-east-1 // Sets your AWS region |
| 144 | + |
| 145 | + If both these environment variables aren't set, the default region is |
| 146 | + ``us-east-1``. For a list of available AWS regions, see the |
| 147 | + `Regional Endpoints <https://docs.aws.amazon.com/general/latest/gr/rande.html#regional-endpoints>`__ |
| 148 | + section of the AWS Service Endpoints reference in the AWS documentation. |
| 149 | + |
| 150 | + .. warning:: Consult your SDK's Documentation for Setting an AWS Region |
| 151 | + |
| 152 | + You cannot set your AWS region with environment variables for all SDKs, |
| 153 | + as in the above example. See your SDK's specific documentation for |
| 154 | + configuring an AWS region. |
| 155 | + |
| 156 | + After you've set the preceding environment variables, specify the ``MONGODB-AWS`` |
| 157 | + authentication mechanism in your connection string as shown in the following example: |
| 158 | + |
| 159 | + .. literalinclude:: /code-snippets/authentication/aws-env-variable.js |
| 160 | + :language: javascript |
| 161 | + |
| 162 | +.. important:: Retrieval of AWS Credentials |
| 163 | + |
| 164 | + Starting in MongoDB version 4.11, when you install the optional |
| 165 | + ``aws-sdk/credential-providers`` dependency, the driver uses the AWS SDK |
| 166 | + to retrieve credentials from the environment. As a result, if you |
| 167 | + have a shared AWS credentials file or config file, the driver will |
| 168 | + use those credentials by default. |
| 169 | + |
| 170 | + You can override this behavior by performing one of the following |
| 171 | + actions: |
| 172 | + |
| 173 | + - Set ``AWS_SHARED_CREDENTIALS_FILE`` variable in your shell to point |
| 174 | + to your credentials file. |
| 175 | + - Set the equivalent environment variable in your application to point |
| 176 | + to your credentials file. |
| 177 | + - Create an AWS profile for your MongoDB credentials and set the |
| 178 | + ``AWS_PROFILE`` environment variable to that profile name. |
| 179 | + |
| 180 | +API Documentation |
| 181 | +----------------- |
| 182 | + |
| 183 | +To learn more about any of the methods or types discussed on this |
| 184 | +page, see the following API documentation: |
| 185 | + |
| 186 | +- `MongoClient <{+api+}/classes/MongoClient.html>`__ |
0 commit comments