|
| 1 | +--- |
| 2 | +page_title: "Guide: Importing MongoDB Atlas Organizations" |
| 3 | +--- |
| 4 | + |
| 5 | +# Importing MongoDB Atlas Organizations |
| 6 | + |
| 7 | +**Objective**: Learn how to import existing MongoDB Atlas organizations into Terraform management, understand why certain attributes are optional for imports, and successfully manage your existing organizations using Infrastructure as Code. |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +Starting with version 1.38.0 of the MongoDB Atlas Provider, you can now import existing MongoDB Atlas organizations into your Terraform state. This feature allows you to bring organizations that were created outside of Terraform under Infrastructure as Code (IaC) management. |
| 12 | + |
| 13 | +## Why Import Organizations? |
| 14 | + |
| 15 | +Before version 1.38.0, the `mongodbatlas_organization` resource did not support the import functionality. Organizations could only be created through Terraform, which meant: |
| 16 | + |
| 17 | +- Existing organizations created through the Atlas UI or API couldn't be managed by Terraform. |
| 18 | +- Teams had to maintain organizations outside of their IaC workflows. |
| 19 | +- There was no migration path to bring existing organizations under Terraform management. |
| 20 | + |
| 21 | +With import support, you can now: |
| 22 | +- Bring existing organizations into Terraform management. |
| 23 | +- Standardize organization configuration across your infrastructure. |
| 24 | +- Apply consistent security settings and policies using IaC. |
| 25 | + |
| 26 | +## Important Changes to Required Attributes |
| 27 | + |
| 28 | +To enable import functionality, several attributes that were previously marked as required have been made optional: |
| 29 | + |
| 30 | +- `org_owner_id` |
| 31 | +- `description` |
| 32 | +- `role_names` |
| 33 | + |
| 34 | +This change was done because these attributes are only needed when creating a new organization and are not returned by the Atlas API when reading existing organizations so they will be empty if the resource is imported. |
| 35 | + |
| 36 | +While these attributes are now optional in the schema, they are still **required when creating a new organization**. The provider will validate that these attributes are present during the creation process. |
| 37 | + |
| 38 | +## How to Import an Organization |
| 39 | + |
| 40 | +### Prerequisites |
| 41 | + |
| 42 | +Before importing an organization, ensure you have: |
| 43 | +- MongoDB Atlas API credentials with Organization Owner permissions. |
| 44 | +- The Organization ID of the organization you want to import. |
| 45 | +- MongoDB Atlas Provider version 1.38.0 or later. |
| 46 | + |
| 47 | +### Finding Your Organization ID |
| 48 | + |
| 49 | +You can find your organization ID in several ways: |
| 50 | + |
| 51 | +1. **Atlas UI**: Navigate to your organization settings - the ID is visible in the URL. |
| 52 | +2. **Atlas CLI**: Use `atlas organizations list` if you have the Atlas CLI installed. |
| 53 | +3. **Atlas API**: Use the organizations endpoint to list all organizations. |
| 54 | + |
| 55 | +### Import Process |
| 56 | + |
| 57 | +#### Step 1: Create the Terraform Configuration |
| 58 | + |
| 59 | +Create a Terraform configuration file that describes the organization you want to import and the `import block`. **Do not include** the creation-only attributes: |
| 60 | + |
| 61 | +```hcl |
| 62 | +resource "mongodbatlas_organization" "imported" { |
| 63 | + name = "My Existing Organization" |
| 64 | +} |
| 65 | +
|
| 66 | +import { |
| 67 | + id = "<ORG_ID>" |
| 68 | + to = mongodbatlas_organization.imported |
| 69 | +} |
| 70 | +
|
| 71 | +``` |
| 72 | + |
| 73 | +#### Step 2: Run the Import Command |
| 74 | + |
| 75 | +Alternatively, you can use the `import command` instead of the `import block`: |
| 76 | + |
| 77 | +```bash |
| 78 | +terraform import mongodbatlas_organization.imported <ORG_ID> |
| 79 | +``` |
| 80 | + |
| 81 | +Replace `<ORG_ID>` with your actual organization ID. |
| 82 | + |
| 83 | +#### Step 3: Verify the Import |
| 84 | + |
| 85 | +Run `terraform plan` to verify that the import was successful and see if any configuration adjustments are needed: |
| 86 | + |
| 87 | +```bash |
| 88 | +terraform plan |
| 89 | +``` |
| 90 | + |
| 91 | +The plan should show minimal or no changes if your configuration matches the existing organization settings. |
| 92 | + |
| 93 | +#### Step 4: Apply Any Updates |
| 94 | + |
| 95 | +If you want to update any organization settings, modify your configuration and apply changs, e.g.: |
| 96 | + |
| 97 | +```bash |
| 98 | +resource "mongodbatlas_organization" "imported" { |
| 99 | + name = "My Existing Organization" |
| 100 | + |
| 101 | + # Add any settings that you want to modify |
| 102 | + api_access_list_required = false |
| 103 | + multi_factor_auth_required = true |
| 104 | + restrict_employee_access = true |
| 105 | + gen_ai_features_enabled = true |
| 106 | + security_contact = "[email protected]" |
| 107 | + skip_default_alerts_settings = true |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Important Considerations |
| 112 | + |
| 113 | +### API Credentials Usage |
| 114 | + |
| 115 | +- When importing an organization, the provider API credentials are used. Ensure that your API key has Organization Owner permissions for the organization being imported. |
| 116 | +- When creating an organization, the process remains unchanged: new API keys are generated as part of the resource creation, and these keys will be used for subsequent `mongodbatlas_organization` resource operations. |
| 117 | + |
| 118 | +**Important**: API credentials stored in the `mongodbatlas_organization` Terraform state will take precedence, regardless of their validity. |
| 119 | + |
| 120 | +### Creation-Only Attributes |
| 121 | + |
| 122 | +The newly-declared optional attributes **cannot be specified** when importing: |
| 123 | +- `org_owner_id` - The organization owner is already set. |
| 124 | +- `description` - API key description from organization creation. |
| 125 | +- `role_names` - API key roles from organization creation. |
| 126 | +- `federation_settings_id` - Federation settings from organization creation. |
| 127 | + |
| 128 | +If you include these in your configuration when importing, Terraform will show them as changes that cannot be applied. |
| 129 | + |
| 130 | +### State Management |
| 131 | + |
| 132 | +After a successful import: |
| 133 | +- The organization will be fully managed by Terraform. |
| 134 | +- You can update any modifiable attributes through Terraform. |
| 135 | +- The `private_key` and `public_key` attributes will be empty (as these are only generated during organization creation). |
| 136 | + |
| 137 | +## Example: Complete Import Workflow |
| 138 | + |
| 139 | +For a complete example of importing an organization, including all configuration files and detailed instructions, see the [organization-import example](https://github.com/mongodb/terraform-provider-mongodbatlas/tree/master/examples/mongodbatlas_organization/organization-import) in the provider repository. |
| 140 | + |
| 141 | +## Troubleshooting |
| 142 | + |
| 143 | +### Common Issues |
| 144 | + |
| 145 | +1. **"cannot be changed after creation" errors during import**: Ensure you haven't included creation-only attributes in your configuration |
| 146 | +2. **Permission errors**: Verify your provider API key has Organization Owner role |
| 147 | +3. **Resource not found**: Double-check the organization ID is correct |
| 148 | + |
| 149 | +## See Also |
| 150 | + |
| 151 | +- [mongodbatlas_organization Resource Documentation](../resources/organization) |
| 152 | +- [Example: Creating a New Organization](https://github.com/mongodb/terraform-provider-mongodbatlas/tree/master/examples/mongodbatlas_organization) |
| 153 | +- [MongoDB Atlas Admin API Organization](https://www.mongodb.com/docs/api/doc/atlas-admin-api-v2/group/endpoint-organizations). |
0 commit comments