Repository navigation
AWS IAM Authentication
Connect to MongoDB Atlas without a password, using an AWS IAM role (Atlas "AWS IAM" database users).
Reference Kevsoft.MigrateMongo.Aws from the migrations project:
dotnet add package Kevsoft.MigrateMongo.Aws # or: migrate-mongo init --auth awsIt pulls in MongoDB.Driver.Authentication.AWS, which must stay on the same version as your MongoDB.Driver
reference. init --auth aws pins both for you. If you add the package by hand, reference
MongoDB.Driver.Authentication.AWS at the same version as MongoDB.Driver.
environments:
prod:
mongodb:
url: mongodb+srv://cluster0.xxxxx.mongodb.net/?authSource=%24external&authMechanism=MONGODB-AWS
databaseName: myappThe runner, bundles and Hosting register the mechanism with the driver when the URL asks for it, so no code is needed.
Without the package, a MONGODB-AWS URL fails before connecting with a message saying which package to add. If you
build your own MongoClientSettings, you can call AwsAuthentication.Register() yourself (it's idempotent).
Credentials come from the standard AWS sources, so nothing secret goes in the URL:
- the
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEYandAWS_SESSION_TOKENenvironment variables; - web identity (
AWS_WEB_IDENTITY_TOKEN_FILEandAWS_ROLE_ARN); - the ECS task or EC2 instance role.
The tool passes its environment to the runner, so set these wherever you run migrate-mongo.
In Atlas, create a database user with AWS IAM authentication for the role's ARN
(arn:aws:iam::123456789012:role/deploy), not the assumed-role session ARN.
Atlas still checks its IP access list against where the connection comes from: the CI runner, or the bastion if you
use a tunnel: (mode: socks for mongodb+srv:// URLs).
Using OIDC to assume a role:
permissions: { id-token: write, contents: read }
steps:
- uses: aws-actions/configure-aws-credentials@v4
with: { role-to-assume: arn:aws:iam::123456789012:role/deploy, aws-region: eu-west-2 }
- run: dotnet tool run migrate-mongo up --env prodUsing OIDC web identity (or any script that exports the AWS_* variables):
- step:
oidc: true
script:
- export AWS_ROLE_ARN=arn:aws:iam::123456789012:role/deploy
- export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
- echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
- ./MyApp.Migrations up --env prodKevsoft.MigrateMongo ·
NuGet ·
Releases ·
Issues ·
Found a mistake? These pages live in
wiki/ on main; pull requests welcome.
Get started
Migrations
- Writing Migrations
- Transactions
- Recording Results
- Dependency Injection
- Custom Templates
- Adopting an Existing Database
Collections
- Managing Collections
- Indexes
- Search Indexes
- Collection Options
- Validators
- Views
- Undeclared Collections
Deploying
Connecting
Reference
Concepts