For AI agents: a documentation index is available at https://www.mongodb.com/pt-br/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

AWS IAM Authentication Mechanism

Important

The MONGODB-AWS authentication mechanism is available only in MongoDB version 4.4 and later.

The MONGODB-AWS authentication mechanism uses Amazon Web Services Identity and Access Management (AWS IAM) credentials to authenticate a user to MongoDB. You can use this mechanism only when authenticating to MongoDB Atlas.

Tip

Configure Atlas for AWS IAM Authentication

To learn more about configuring MongoDB Atlas for AWS IAM authentication, see Set Up Authentication with AWS IAM in the Atlas documentation.

To connect to a MongoDB instance with MONGODB-AWS authentication enabled, specify the MONGODB-AWS authentication mechanism.

The driver checks for your credentials in the following sources in the order listed:

  1. Connection string.

  2. Custom credential provider. To learn more, see Specify a Custom AWS Credential Provider.

  3. Environment variables.

  4. Web identity token file.

  5. AWS ECS endpoint specified in the AWS_CONTAINER_CREDENTIALS_RELATIVE_URI environment variable.

  6. AWS EC2 endpoint. To learn more, see IAM Roles for Tasks in the AWS documentation.

Important

The driver obtains the credentials only from the first source in which they are found. For example, if you specify your AWS credentials in the connection string, the driver ignores any credentials that you specify in environment variables.

Tip

The following examples set the appropriate credentials by using the SetAuth() method. You can also specify these credentials by using the ApplyURI() method. If you use the ApplyURI() method you must URL encode the username and password to ensure they are correctly parsed.

To connect to your MongoDB instance using your AWS IAM credentials, perform the following steps:

  • Assign the AuthMechanism option the value MONGODB-AWS

  • Assign the Username option the value of your accessKeyID

  • Assign the Password option the value of your secretAccessKey

var accessKeyID, secretAccessKey string
awsCredential := options.Credential{
AuthMechanism: "MONGODB-AWS",
AuthSource: "<authenticationDb>",
Username: "<accessKeyID>",
Password: "<secretAccessKey>",
}
awsIAMClient, err := mongo.Connect(options.Client().SetAuth(awsCredential))
if err != nil {
panic(err)
}
_ = awsIAMClient

If you must specify an AWS session token, use the temporary credentials returned from an assume role request.

To use temporary credentials, assign the value of your sessionToken to the AuthMechanismProperties option:

var accessKeyID, secretAccessKey, sessionToken string
assumeRoleCredential := options.Credential{
AuthMechanism: "MONGODB-AWS",
AuthSource: "<authenticationDb>",
Username: "<accessKeyID>",
Password: "<secretAccessKey>",
AuthMechanismProperties: map[string]string{
"AWS_SESSION_TOKEN": "<sessionToken>",
},
}
assumeRoleClient, err := mongo.Connect(options.Client().SetAuth(assumeRoleCredential))

You can supply your own AWS credential provider to the driver, which enables you to use the full credential chain of the AWS SDK for Go v2. If you authenticate by using an AWS mechanism that the driver's built-in credential chain doesn't support, such as Amazon EKS Pod Identity, use this method.

Before you begin, ensure that you have the following:

  • Go 1.26 or later

  • Go driver v2.9 or later

Note

Experimental Feature

The ext/awsauth module is pre-release. Its API is unstable and might change in backward-incompatible ways.

To supply a credential provider from the AWS SDK, install the ext/awsauth module and the AWS SDK for Go v2:

go get go.mongodb.org/mongo-driver/ext/awsauth github.com/aws/aws-sdk-go-v2/config

The following example passes a provider chain from the AWS SDK to the AWS authentication mechanism:

awsConfig, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
panic(err)
}
awsSdkCredential := options.Credential{
AuthMechanism: "MONGODB-AWS",
AWSCredentialsProvider: awsauth.NewCredentialsProvider(awsConfig.Credentials),
}
awsSdkClient, err := mongo.Connect(options.Client().SetAuth(awsSdkCredential))
if err != nil {
panic(err)
}
_ = awsSdkClient

To learn more about any of the methods or types discussed on this page, see the following API documentation: