1. Scheduled Scaling Operations during MongoDB Atlas Maintenance

    Python

    Scheduled scaling operations during MongoDB Atlas maintenance typically involve adjusting the resources (CPU, memory, storage, etc.) allocated to your MongoDB Atlas clusters based on a schedule. This can be useful to either scale up resources in anticipation of a heavy load or to reduce resources during times of low usage to save costs. Maintenance windows are periods of time when MongoDB Atlas performs routine updates and maintenance on the underlying infrastructure without impacting the cluster's availability.

    In this use case, you might define a Pulumi program to handle such operations automatically. For instance, you might have a Pulumi program that specifies a Maintenance Window resource to define when maintenance occurs, and a Cluster resource that includes auto-scaling policies to adjust the resources during these periods.

    Pulumi enables this by providing a declarative infrastructure as code approach, where you define the desired state of your infrastructure using code. Pulumi then makes the necessary API calls to MongoDB Atlas to enforce the state described by your code.

    Let's create a simple Pulumi program using Python to specify both a maintenance window and scheduled scaling operations for a MongoDB Atlas project.

    import pulumi import pulumi_mongodbatlas as mongodbatlas # Configure MongoDB Atlas settings. project_id = "YOUR_PROJECT_ID_HERE" # Replace with your MongoDB Atlas Project ID cluster_name = "YOUR_CLUSTER_NAME_HERE" # Replace with your MongoDB Atlas Cluster Name maintenance_day_of_week = 3 # Day of week as an integer (0-6), where 0 is Sunday. maintenance_hour_of_day = 5 # Hour of the day in UTC (0-23). # Define the maintenance window for the MongoDB Atlas Cluster. maintenance_window = mongodbatlas.MaintenanceWindow( "maintenance_window", day_of_week=maintenance_day_of_week, hour_of_day=maintenance_hour_of_day, project_id=project_id, defer=False, # Set to True if you want to defer maintenance to the next available window. ) # Define an auto-scaling policy for the Cluster. # This policy will scale up the cluster during maintenance and scale down after. auto_scaling_policy = mongodbatlas.Cluster( "cluster_scaling", project_id=project_id, name=cluster_name, auto_scaling_disk_gb_enabled=True, auto_scaling_compute_enabled=True, auto_scaling_compute_scale_down_enabled=True, disk_size_gb=10, # Set up desired disk size in GB. provider_instance_size_name="M10", # Set the instance size. replication_specs=[mongodbatlas.ClusterReplicationSpecArgs( num_shards=1, region_configs=[ mongodbatlas.ClusterRegionConfigArgs( region_name="US_EAST_1", # Set the desired provider region here. auto_scaling_compute_min_instance_size="M10", # Minimum instance size during scale down. auto_scaling_compute_max_instance_size="M30", # Maximum instance size during scale up. ), ], )], ) # Exporting the cluster connection string for external use primary_endpoint = pulumi.Output.all(maintenance_window, auto_scaling_policy).apply(lambda args: args[1].connection_strings["standard"]) pulumi.export("primary_connection_string", primary_endpoint)

    This program does the following:

    1. It imports the necessary Pulumi modules.
    2. It sets configuration values such as the project ID and cluster name.
    3. It defines a maintenance window resource (maintenance_window) for the MongoDB Atlas cluster specifying the day and time for the maintenance operations.
    4. It defines a cluster auto-scaling policy (auto_scaling_policy) that enables disk auto-scaling, CPU & memory (compute) auto-scaling, and allows for scaling down. Scale-down operations are often used outside of heavy load periods to save on costs.
    5. Finally, it exports the cluster's primary connection string for use elsewhere, such as in application code or other infrastructure components.

    Remember to replace "YOUR_PROJECT_ID_HERE" and "YOUR_CLUSTER_NAME_HERE" with your actual MongoDB Atlas Project ID and Cluster Name.

    Once you have this Pulumi program set up, you can run it using Pulumi CLI commands to deploy your infrastructure:

    1. Initialize a new Pulumi project with pulumi new.
    2. Place this program in your __main__.py file.
    3. Run pulumi up to create or update your resources.

    Please ensure you have the Pulumi CLI installed and configured with the necessary cloud provider credentials before running the above commands.