MongoDB Atlas Terraform Provider

repository·master·Indexed 43 days ago

https://github.com/mongodb/terraform-provider-mongodbatlas

Allows users to manage MongoDB Atlas Database as a Service using Terraform for Infrastructure as Code (IaC). Includes guides for migrating from deprecated Atlas User data sources to modern replacements and transitioning from mongodbatlas_cluster to mongodbatlas_advanced_cluster resources using Terraform moved blocks.

Tokens
398.6K
Snippets
896
Records
1.4K
Agent score
53%

What's inside MongoDB Atlas Terraform Provider

  1. New features in MongoDB Atlas Provider 1.15.0

    master

    Version 1.15.0 introduced support for managing and retrieving MongoDB Atlas OIDC Workforce identity providers.

    New Capabilities:

    • Resource Management: Use the mongodbatlas_federated_settings_identity_provider resource to manage OIDC Workforce identity providers.
    • Data Retrieval: Use the mongodbatlas_federated_settings_identity_provider and mongodbatlas_federated_settings_identity_providers data sources to programmatically retrieve identity provider information.
  2. Manage MongoDB Atlas Flex Clusters with `mongodbatlas_flex_cluster`

    master

    The mongodbatlas_flex_cluster resource allows you to create, update, delete, and import Flex Clusters in MongoDB Atlas.

    Recommendation: It is highly recommended to use the mongodbatlas_advanced_cluster resource instead. The mongodbatlas_advanced_cluster resource supports Flex clusters as well as tenant and dedicated clusters, which facilitates easier migration between different cluster types.

    resource "mongodbatlas_flex_cluster" "example-cluster" {
      project_id = var.project_id
      name       = var.cluster_name
      provider_settings = {
        backing_provider_name = "AWS"
        region_name           = "US_EAST_1"
      }
      termination_protection_enabled = true
    }
  3. New features in MongoDB Atlas Provider v1.12.0

    master

    Version 1.12.0 adds the following capabilities:

    Atlas Resource Tags

    You can now manage Atlas Resource Tags within these resources:

    • mongodbatlas_cluster
    • mongodbatlas_advanced_cluster
    • mongodbatlas_serverless_instance

    Atlas User Data Sources

    You can programmatically retrieve Atlas Organizational User details using these data sources:

    • mongodbatlas_atlas_user
    • mongodbatlas_atlas_users
  4. Available Log Integration Examples

    master

    The following integration patterns are available in the examples directory:

    DestinationExample PathDescription
    AWS S3./s3bucket/Basic export to a single S3 bucket. Creates S3 bucket, IAM role/policy, Cloud Provider Access, and Log Integration.
    AWS S3 (MRAP)./s3bucket_mrap/Advanced export to an S3 Multi-Region Access Point for high availability. Creates multi-region buckets, MRAP, IAM permissions, Cloud Provider Access, and Log Integration.
    Google Cloud (GCS)./gcp/Exports logs to a GCS bucket. Creates GCS bucket and Log Integration.
    Azure Blob Storage./azure/Exports logs to an Azure container. Creates Resource Group, Storage Account, Container, and Log Integration.
    Datadog./datadog/Exports logs directly to Datadog.
    Splunk./splunk/Exports logs to a Splunk HTTP Event Collector (HEC) endpoint.
    OpenTelemetry./otel/Exports logs to an OpenTelemetry (OTel) collector.
  5. Manage Advanced Clusters with mongodbatlas_advanced_cluster

    master

    The mongodbatlas_advanced_cluster resource allows you to create, edit, and delete advanced MongoDB Atlas clusters.

    Best Practices:

    • It is highly recommended to use the Official MongoDB Atlas Cluster Module instead of managing the raw resource, as the module implements best practices by default.
    • If migrating from provider version v1.x.x to v2.0.0 or later, you must follow the migration guide to update your configuration.

    Key Capabilities:

    • Supports creating Flex clusters.
    • Supports upgrading M0 clusters to Flex.
    • Supports upgrading Flex clusters to Dedicated clusters.
    • Creates a network container for every provider/region combination specified. You can access these via the computed replication_specs[#].container_id attribute.
  6. Manage Atlas Federated Settings Identity Providers

    master

    The mongodbatlas_federated_settings_identity_provider resource allows you to manage a subset of settings for an existing Atlas federated authentication configuration. This resource supports both SAML and OIDC protocols.

    CRITICAL: If you are using a SAML Identity Provider, you MUST import the resource before you can manage it with this provider.

    resource "mongodbatlas_federated_settings_identity_provider" "identity_provider" {
      federation_settings_id     = "627a9687f7f7f7f774de306f14"
      name = "mongodb_federation_test"
      associated_domains           = ["yourdomain.com"]
      sso_debug_enabled = true
      status = "ACTIVE"
      sso_url = "https://mysso.oktapreview.com/app/mysso_terraformtestsso/exk17q7f7f7f7f50h8/sso/saml"
      issuer_uri = "http://www.okta.com/exk17q7f7f7f7fp50h8"
      request_binding = "HTTP-POST"
      response_signature_algorithm = "SHA-256"
    }
  7. New features in MongoDB Atlas Provider 1.17.0

    master

    Version 1.17.0 introduced the following capabilities:

    • OIDC Workforce Identity Providers: Create and delete providers using the mongodbatlas_federated_settings_identity_provider resource.
    • OIDC Workload Identity Providers: Supported via the mongodbatlas_federated_settings_identity_provider resource and the mongodbatlas_federated_settings_identity_provider and mongodbatlas_federated_settings_identity_providers data sources.
    • Control Plane IP Addresses: Retrieve MongoDB Atlas control plane IP addresses using the mongodbatlas_control_plane_ip_addresses data source.
  8. Manage MongoDB Atlas Event Triggers with `mongodbatlas_event_trigger`

    master

    The mongodbatlas_event_trigger resource allows you to automate actions in MongoDB Atlas App Services by responding to specific events. You can create three types of triggers:

    1. DATABASE: Responds to changes in MongoDB collections (e.g., INSERT, UPDATE).
    2. AUTHENTICATION: Responds to user authentication events (e.g., LOGIN, CREATE, DELETE).
    3. SCHEDULED: Runs on a specific schedule defined by a cron expression.

    Important Note: Changing the app_id will force a replacement of the resource, deleting the trigger from the old Atlas App Services app and creating it in the new one.

    Note on Functions: While you can create database triggers via Terraform, you cannot create the Atlas Functions themselves using this provider. You must define the functions in Atlas separately.

    resource "mongodbatlas_event_trigger" "test" {
      project_id = "PROJECT ID"
      app_id = "APPLICATION ID"
      name = "NAME OF THE TRIGGER"
      type = "DATABASE"
      # ... other configuration ...
    }
  9. Overview of configuration changes in `mongodbatlas_advanced_cluster` v2.0.0

    master

    When upgrading mongodbatlas_advanced_cluster from v1.x to v2.0.0, several breaking changes occur in the configuration schema:

    Removed Attributes

    • id (Use cluster_id instead)
    • disk_size_gb
    • replication_specs.#.num_shards
    • replication_specs.#.id
    • advanced_configuration.default_read_concern
    • advanced_configuration.fail_index_key_too_long

    Structural Changes

    • Sharding: Supports only the new sharding configuration for independent shard scaling.
    • List Attributes: replication_specs and region_configs have changed from blocks to list attributes (arrays of objects).
    • Single Attributes: connection_strings, timeouts, advanced_configuration, bi_connector_config, pinned_fcv, electable_specs, read_only_specs, analytics_specs, auto_scaling, and analytics_auto_scaling are now single attributes (objects) instead of blocks. Remove indices like [0] or .0 when referencing them.
    • Maps: tags and labels are now maps instead of blocks.
    # Old tags block syntax
    tags {
      key   = "env"
      value = "dev"
    }
    
    # New tags map syntax
    tags = {
      env           = "dev"         # key strings without blanks can be enclosed in quotes but not required
      "tag 2"       = "val"         # enclose key strings with blanks in quotes
      (var.tag_key) = "another_val" # enclose key expressions in brackets to evaluate
    }
  10. What is use_effective_fields and when to use it

    master

    The use_effective_fields attribute is used with the mongodbatlas_advanced_cluster resource and data sources to manage configuration drift caused by MongoDB Atlas auto-scaling.

    When auto-scaling is enabled, Atlas automatically adjusts instance_size, disk_size_gb, and disk_iops. Without use_effective_fields, Terraform perceives these automatic changes as drift from the desired state.

    Key Benefits:

    • Eliminates lifecycle.ignore_changes: You no longer need to manually list dozens of auto-scalable fields in a lifecycle block.
    • Reduces Complexity: Avoids the need to maintain separate modules for auto-scaling vs. non-auto-scaling clusters.
    • Provides Visibility: Data sources provide effective_*_specs attributes to show the actual provisioned values versus the configured intent.
  11. How Private Endpoint Services work in MongoDB Atlas

    master

    The mongodbatlas_privatelink_endpoint resource creates a Private Endpoint Service within your MongoDB Atlas project.

    Crucial Distinction: This resource does not create the endpoint in your cloud environment. Instead, it creates the service in Atlas and returns the necessary information (like service names or resource IDs) that you must use to create the actual endpoint in your cloud provider (AWS, Azure, or GCP).

    To complete the connection, you must use a complementary resource, mongodbatlas_privatelink_endpoint_service, to link your cloud provider's endpoint to the Atlas service created by this resource.

  12. Manage Auto-Scaling with `use_effective_fields`

    master

    The recommended way to manage auto-scaling without encountering Terraform plan drift is to set use_effective_fields = true in the mongodbatlas_advanced_cluster resource.

    This approach allows you to define an initial instance_size in your configuration, while Terraform uses the 'effective' values (the actual sizes Atlas has scaled to) to prevent unnecessary updates. You can use the mongodbatlas_advanced_cluster data source with use_effective_fields = true to read these actual values back into your configuration.

    Module Authors: This is highly recommended for reusable modules to support both auto-scaling and non-auto-scaling clusters without requiring lifecycle blocks.

    resource "mongodbatlas_advanced_cluster" "test" {
      project_id            = var.project_id
      name                  = "AutoScalingCluster"
      cluster_type          = "SHARDED"
      use_effective_fields  = true
    
      replication_specs = [{
        region_configs = [{
          electable_specs = {
            instance_size = "M40"
            node_count    = 3
          }
          auto_scaling = {
            compute_enabled = true
            compute_max_instance_size = "M60"
          }
          # ...
        }]
        zone_name = "Zone 1"
      }]
    }
    
    # Read effective values to see what Atlas has scaled to
    data "mongodbatlas_advanced_cluster" "test" {
      project_id           = mongodbatlas_advanced_cluster.test.project_id
      name                 = mongodbatlas_advanced_cluster.test.name
      use_effective_fields = true
      depends_on           = [mongodbatlas_advanced_cluster.test]
    }
    
    output "shard_sizes" {
      value = [
        for idx, spec in data.mongodbatlas_advanced_cluster.test.replication_specs : {
          shard_index           = idx
          configured_size       = spec.region_configs[0].electable_specs.instance_size
          actual_electable_size = spec.region_configs[0].effective_electable_specs.instance_size
          actual_analytics_size = spec.region_configs[0].effective_analytics_specs.instance_size
        }
      ]
    }