Skip to content

feat: Add support for MONGODB_ATLAS_PUBLIC_API_KEY and MONGODB_ATLAS_PRIVATE_API_KEY to TF #3505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/3505.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:note
New environment variables: We added support for the `MONGODB_ATLAS_PUBLIC_API_KEY` and `MONGODB_ATLAS_PRIVATE_API_KEY` environment variables which are widely used across the MongoDB ecosystem.
```
11 changes: 5 additions & 6 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ You can use any the following methods:
### Environment Variables

You can also provide your credentials via the environment variables,
`MONGODB_ATLAS_PUBLIC_KEY` and `MONGODB_ATLAS_PRIVATE_KEY`,
`MONGODB_ATLAS_PUBLIC_API_KEY` and `MONGODB_ATLAS_PRIVATE_API_KEY`,
for your public and private MongoDB Atlas programmatic API key pair respectively:

```terraform
Expand All @@ -59,14 +59,13 @@ provider "mongodbatlas" {}
Usage (prefix the export commands with a space to avoid the keys being recorded in OS history):

```shell
$ export MONGODB_ATLAS_PUBLIC_KEY="<ATLAS_PUBLIC_KEY>"
$ export MONGODB_ATLAS_PRIVATE_KEY="<ATLAS_PRIVATE_KEY>"
$ export MONGODB_ATLAS_PUBLIC_API_KEY="<ATLAS_PUBLIC_KEY>"
$ export MONGODB_ATLAS_PRIVATE_API_KEY="<ATLAS_PRIVATE_KEY>"
$ terraform plan
```

As an alternative to `MONGODB_ATLAS_PUBLIC_KEY` and `MONGODB_ATLAS_PRIVATE_KEY`
if you are using [MongoDB CLI](https://docs.mongodb.com/mongocli/stable/)
then `MCLI_PUBLIC_API_KEY` and `MCLI_PRIVATE_API_KEY` are also supported.
We recommend that you use the `MONGODB_ATLAS_PUBLIC_API_KEY` and `MONGODB_ATLAS_PRIVATE_API_KEY` environment variables because they are compatible with other MongoDB tools, such as Atlas CLI.
You can still use `MONGODB_ATLAS_PUBLIC_KEY` and `MONGODB_ATLAS_PRIVATE_KEY` as alternative keys in your local environment. However, these environment variables are not guaranteed to work across all tools in the MongoDB ecosystem.

### AWS Secrets Manager
AWS Secrets Manager (AWS SM) helps to manage, retrieve, and rotate database credentials, API keys, and other secrets throughout their lifecycles. See [product page](https://aws.amazon.com/secrets-manager/) and [documentation](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html) for more details.
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ func setDefaultValuesWithValidations(ctx context.Context, data *tfMongodbAtlasPr

if data.PublicKey.ValueString() == "" {
data.PublicKey = types.StringValue(MultiEnvDefaultFunc([]string{
"MONGODB_ATLAS_PUBLIC_API_KEY",
"MONGODB_ATLAS_PUBLIC_KEY",
"MCLI_PUBLIC_API_KEY",
}, "").(string))
Expand All @@ -362,6 +363,7 @@ func setDefaultValuesWithValidations(ctx context.Context, data *tfMongodbAtlasPr

if data.PrivateKey.ValueString() == "" {
data.PrivateKey = types.StringValue(MultiEnvDefaultFunc([]string{
"MONGODB_ATLAS_PRIVATE_API_KEY",
"MONGODB_ATLAS_PRIVATE_KEY",
"MCLI_PRIVATE_API_KEY",
}, "").(string))
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/provider_sdk2.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ func setDefaultsAndValidations(d *schema.ResourceData) diag.Diagnostics {
}

if err := setValueFromConfigOrEnv(d, "public_key", []string{
"MONGODB_ATLAS_PUBLIC_API_KEY",
"MONGODB_ATLAS_PUBLIC_KEY",
"MCLI_PUBLIC_API_KEY",
}); err != nil {
Expand All @@ -361,6 +362,7 @@ func setDefaultsAndValidations(d *schema.ResourceData) diag.Diagnostics {
}

if err := setValueFromConfigOrEnv(d, "private_key", []string{
"MONGODB_ATLAS_PRIVATE_API_KEY",
"MONGODB_ATLAS_PRIVATE_KEY",
"MCLI_PRIVATE_API_KEY",
}); err != nil {
Expand Down