SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for AWS Amazon Web Services, Redshift, AWS Lambda Functions, S3 Buckets, VPC, EC2, IAM

Find DocumentDB Instance Identifier using AWS Lambda Function Python Code

In order to scale out Amazon DocumentDB cluster by code automatically, Python Boto3 library or with other words AWS SDK for Python can be used within AWS Lambda function. Boto3 library provides methods required to manage Amazon DocumentDB cluster or database instances for Python developers. For example, in order to add an additional replica node or reader node to your MongoDB compatible DocumentDB cluster for increasing read performance, create_db_instance method can be used. Or to increase the hardware resources of your DocumentDB database instance, the related instance or node can be modified by using modify_db_instance method of the Python Boto3 library DocDB client object.

In this DocumentDB guide, I want to show AWS Lambda developers coding in Python how to find a suitable name for the new DocumentDB replica which is to be added during scale out operation. Because DocumentDB class DocDB uses create_db_instance method which requests required DBInstanceIdentifier input parameter, cloud developers should previously decide on the database instance name.

I really find it useful to decide on a standard for the DocumentDB instance naming.
For example, I try to implement following scheme on my environment where Amazon DocumentDB clusters are running.
It will be better to use a simple name for the cluster.
The database instances are automatically named by adding numbers starting from 2 and increasing to the base cluster name.

My solution to name new database instances to be added to the Amazon DocumentDB cluster is looping through all integer numbers starting from 1 to a fix number like 30 and build a string as identifier by concatenating cluster name and integer. Of course you can use a lower limit since the maximum number of nodes within a DocumentDB cluster is 16 including the write node. Then this proposed identifier can be queried within the all nodes of the DocumentDB cluster (which can be fetched by API method named describe_db_clusters) to see either it is already used or not.

Here is the Python code which I used in one of my AWS Lambda functions using Boto3 library for accessing Amazon DocumentDB cluster and database instances details.

import json
import boto3
import string

DBClusterIdentifier = "docdb-sandbox"
client = boto3.client('docdb')

def lambda_handler(event, context):

  DBClusterDetail = client.describe_db_clusters(

  print('--- Instance Count ------')

  print('--- Cluster Instances ------')

  suggestedNewInstanceIdentifier = ""
  tmpIdentifier = ""
  j = 1
  while j <= 16:
    suggestedNewInstanceIdentifier = DBClusterIdentifier + str(j)
    i = 0
    while i < currentNumberOfNodesInCluster:
        tmpIdentifier = DBClusterDetail["DBClusters"][0]["DBClusterMembers"][i]["DBInstanceIdentifier"]
        i += 1
        if suggestedNewInstanceIdentifier==tmpIdentifier:
            i=9999 # break
    if suggestedNewInstanceIdentifier != "":
      j=9999 # break
    j += 1


  return {
    'statusCode': 200,
    'body': json.dumps('Database instance identifier: ' + str(suggestedNewInstanceIdentifier))
Python code used in AWS Lambda function for DocumentDB instance identifier

Of course you can modify the above code block for more efficient execution and wrap it into a Python function so that you can consume it within a Lambda function which can be used for scaling out a DocumentDB cluster.

When I execute the AWS Lambda function for a DocumentDB cluster as seen below:

Amazon DocumentDB cluster and its nodes

I got the following output "docdb-sandbox4" to be used for the next database instance:

AWS Lambda function execution output which find instance identifier for new replica

I hope Python developers find this AWS tutorial useful. Feel free to inform me about your suggestions regarding this tutorials topic.


Copyright © 2004 - 2024 Eralper YILMAZ. All rights reserved.