Use CloudFormation to create an Amazon DocumentDB (MongoDB) database on AWS

You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose

This article helps users create a document database cluster using a single master instance using CloudFormation templates. When users want to run Mongo database workloads on AWS (Amazon Web Services), the document database is important. Amazon DocumentDB (with MongoDB Compatibility) is a scalable, fully managed, fast and highly available document database service that supports MongoDB workloads.

This hosted non-relational database service makes it easier to store, query and index JSON data. This database service is designed from the ground up to ensure the scalability, performance and availability required for large-scale operation of mission-critical MongoDB workloads.

Set up prerequisites

Users will need to have:

  • An AWS account
  • Created a user with permission to create resources on the AWS account
  • An IDE similar to visual studio code can write and edit CloudFormation templates.

CloudFormation template used

Please find below CloudFormation template. The template will create:

  • Database instance security group.
  • Database subnet group.
  • Database parameter group.
  • Document database cluster.
  • Database instance.
---
AWSTemplateFormatVersion: "2010-09-09"
Description: Template to Create a document DB parameter group, subnet group and cluster

Parameters:
  VPC:
    Type: String
    Description: The VPC to create the cluster
    Default: vpc-ID

  PrivateSubnet01:
    Type: String
    Description: The subnet for the DB cluster
    Default: subnet-ID

  PrivateSubnet02:
    Type: String
    Description: The subnet for the DB cluster
    Default: subnet-ID

  MasterUsername:
    Type: String
    Description: The username for our database.
  
  MasterUserPassword:
    Type: String
    Description: The password for the database.
    "NoEcho": true

Resources:
   DBSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: "DB instances security group"
      GroupName: "test-db-instance-SG"
      VpcId: !Ref VPC
      SecurityGroupIngress: 
        - 
          CidrIp: "*.*.*.*/32"
          FromPort: 22017
          IpProtocol: "tcp"
          ToPort: 22017
      SecurityGroupEgress: 
        - 
          CidrIp: "0.0.0.0/0"
          IpProtocol: "-1"
      
   DBSubnetGroup: 
    Type: AWS::DocDB::DBSubnetGroup
    Properties: 
      DBSubnetGroupDescription: "test document db subnet group"
      DBSubnetGroupName: "eu-central-1-test-db-subnet-group"
      SubnetIds: 
        - !Ref PrivateSubnet01
        - !Ref PrivateSubnet02
      Tags: 
        - Key: Name
          Value: eu-central-1-test-db-subnet-group
        - Key: createdBy
          Value: Maureen Barasa
        - Key: Project
          Value: test-blog
        - Key: Environment
          Value: test

   DBParameterGroup:
    Type: AWS::DocDB::DBClusterParameterGroup
    Properties:
      Description: "our test document db parameter group"
      Family: docdb3.6
      Name: test-db-parameter-group
      Parameters: 
        audit_logs: "disabled"
        tls: "enabled"
        ttl_monitor: "enabled"
      Tags: 
        - Key: Name
          Value: eu-central-1-test-db-cluster
        - Key: createdBy
          Value: Maureen Barasa
        - Key: Project
          Value: test-blog
        - Key: Environment
          Value: test

   DBCluster:
    Type: AWS::DocDB::DBCluster
    Properties: 
      BackupRetentionPeriod : 5
      DBClusterIdentifier : eu-central-1-test-db-cluster
      DBClusterParameterGroupName : !Ref DBParameterGroup
      DBSubnetGroupName : !Ref DBSubnetGroup
      MasterUsername : !Ref MasterUsername
      MasterUserPassword : !Ref MasterUserPassword
      Port : "27017"
      PreferredBackupWindow : "23:00-23:59"
      PreferredMaintenanceWindow : "sun:00:00-sun:05:00"
      VpcSecurityGroupIds:
        - !Ref DBSecurityGroup
      StorageEncrypted : true
      Tags: 
        - Key: Name
          Value: eu-central-1-test-db-cluster
        - Key: createdBy
          Value: Maureen Barasa
        - Key: Project
          Value: test-blog
        - Key: Environment
          Value: test

   DBInstance:
    Type: AWS::DocDB::DBInstance
    Properties:
      AutoMinorVersionUpgrade: true
      AvailabilityZone: "eu-west-1a"
      DBClusterIdentifier: !Ref DBCluster
      DBInstanceClass: "db.t3.medium"
      DBInstanceIdentifier: "test-cluster-instance-1"
      PreferredMaintenanceWindow: "sun:00:00-sun:05:00"
      Tags: 
        - Key: Name
          Value: eu-central-1-test-db-instance
        - Key: createdBy
          Value: Maureen Barasa
        - Key: Project
          Value: test-blog
        - Key: Environment
          Value: test

Outputs:
   Cluster:
    Description: The DB Cluster Name
    Value: !Ref DBCluster
    
   SubnetGroup:
    Description: The db subnet group name 
    Value: !Ref DBSubnetGroup

   ParameterGroup:
    Description: The db subnet group name 
    Value: !Ref DBParameterGroup

We can deploy CloudFormation templates using the CloudFormation stack.

Template description

The template consists of 3 parts. Parameters, resources and output parts.

parameter:

In the resource section, we ask users to enter the dynamic variables of their template. In our case, users should replace the VPC and subnet ID with their respective VPC and subnet ID. In addition, users will be prompted to enter their database master username and password. Please make sure you do not use admin as the main username.

Resources:

The user defines the AWS resources to be created here. For our case, we first create a database instance security group. Users should change the security group entry to reflect the CIDR IP block they wish to allow access to the database instance.

Next, it creates the database subnet and parameter group. The subnet group defines the subnet in which the database cluster and instance are created. Through the parameter group, you can manage the database engine configuration. The user should browse the attributes of the parameter group and change it to its specific requirements. In addition, users should pay attention to the name and label to customize as needed.

Then create a document database cluster. Just like above, the user should go through all cluster properties and change them to meet their requirements.

Finally, create a database instance. However, users should browse the template and change the availability zone, instance class, and preferred maintenance requirements to match their specific needs. In addition, the database instance identifier and label should be customized to meet user requirements.

Output:

The output part of the template indicates that CloudFormation outputs the name of the resource created. For example, in this example, we have instructed the template to output the names of clusters, subnets and parameter groups.

Similar guides:

  • Use CloudFormation to set up an AWS RDS MySQL database instance
  • Set up Elasticsearch Cluster with Kibana on AWS
  • How to stream logs in AWS from CloudWatch to ElasticSearch
  • How to create AWS Network Architecture using CloudFormation

Happy Building! ! !

You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose

Use CloudFormation to create an Amazon DocumentDB (MongoDB) database on AWS

You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose

This article helps users create a document database cluster using a single master instance using CloudFormation templates. When users want to run Mongo database workloads on AWS (Amazon Web Services), the document database is important. Amazon DocumentDB (with MongoDB Compatibility) is a scalable, fully managed, fast and highly available document database service that supports MongoDB workloads.

This hosted non-relational database service makes it easier to store, query and index JSON data. This database service is designed from the ground up to ensure the scalability, performance and availability required for large-scale operation of mission-critical MongoDB workloads.

Set up prerequisites

Users will need to have:

  • An AWS account
  • Created a user with permission to create resources on the AWS account
  • An IDE similar to visual studio code can write and edit CloudFormation templates.

CloudFormation template used

Please find below CloudFormation template. The template will create:

  • Database instance security group.
  • Database subnet group.
  • Database parameter group.
  • Document database cluster.
  • Database instance.
---
AWSTemplateFormatVersion: "2010-09-09"
Description: Template to Create a document DB parameter group, subnet group and cluster

Parameters:
  VPC:
    Type: String
    Description: The VPC to create the cluster
    Default: vpc-ID

  PrivateSubnet01:
    Type: String
    Description: The subnet for the DB cluster
    Default: subnet-ID

  PrivateSubnet02:
    Type: String
    Description: The subnet for the DB cluster
    Default: subnet-ID

  MasterUsername:
    Type: String
    Description: The username for our database.
  
  MasterUserPassword:
    Type: String
    Description: The password for the database.
    "NoEcho": true

Resources:
   DBSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: "DB instances security group"
      GroupName: "test-db-instance-SG"
      VpcId: !Ref VPC
      SecurityGroupIngress: 
        - 
          CidrIp: "*.*.*.*/32"
          FromPort: 22017
          IpProtocol: "tcp"
          ToPort: 22017
      SecurityGroupEgress: 
        - 
          CidrIp: "0.0.0.0/0"
          IpProtocol: "-1"
      
   DBSubnetGroup: 
    Type: AWS::DocDB::DBSubnetGroup
    Properties: 
      DBSubnetGroupDescription: "test document db subnet group"
      DBSubnetGroupName: "eu-central-1-test-db-subnet-group"
      SubnetIds: 
        - !Ref PrivateSubnet01
        - !Ref PrivateSubnet02
      Tags: 
        - Key: Name
          Value: eu-central-1-test-db-subnet-group
        - Key: createdBy
          Value: Maureen Barasa
        - Key: Project
          Value: test-blog
        - Key: Environment
          Value: test

   DBParameterGroup:
    Type: AWS::DocDB::DBClusterParameterGroup
    Properties:
      Description: "our test document db parameter group"
      Family: docdb3.6
      Name: test-db-parameter-group
      Parameters: 
        audit_logs: "disabled"
        tls: "enabled"
        ttl_monitor: "enabled"
      Tags: 
        - Key: Name
          Value: eu-central-1-test-db-cluster
        - Key: createdBy
          Value: Maureen Barasa
        - Key: Project
          Value: test-blog
        - Key: Environment
          Value: test

   DBCluster:
    Type: AWS::DocDB::DBCluster
    Properties: 
      BackupRetentionPeriod : 5
      DBClusterIdentifier : eu-central-1-test-db-cluster
      DBClusterParameterGroupName : !Ref DBParameterGroup
      DBSubnetGroupName : !Ref DBSubnetGroup
      MasterUsername : !Ref MasterUsername
      MasterUserPassword : !Ref MasterUserPassword
      Port : "27017"
      PreferredBackupWindow : "23:00-23:59"
      PreferredMaintenanceWindow : "sun:00:00-sun:05:00"
      VpcSecurityGroupIds:
        - !Ref DBSecurityGroup
      StorageEncrypted : true
      Tags: 
        - Key: Name
          Value: eu-central-1-test-db-cluster
        - Key: createdBy
          Value: Maureen Barasa
        - Key: Project
          Value: test-blog
        - Key: Environment
          Value: test

   DBInstance:
    Type: AWS::DocDB::DBInstance
    Properties:
      AutoMinorVersionUpgrade: true
      AvailabilityZone: "eu-west-1a"
      DBClusterIdentifier: !Ref DBCluster
      DBInstanceClass: "db.t3.medium"
      DBInstanceIdentifier: "test-cluster-instance-1"
      PreferredMaintenanceWindow: "sun:00:00-sun:05:00"
      Tags: 
        - Key: Name
          Value: eu-central-1-test-db-instance
        - Key: createdBy
          Value: Maureen Barasa
        - Key: Project
          Value: test-blog
        - Key: Environment
          Value: test

Outputs:
   Cluster:
    Description: The DB Cluster Name
    Value: !Ref DBCluster
    
   SubnetGroup:
    Description: The db subnet group name 
    Value: !Ref DBSubnetGroup

   ParameterGroup:
    Description: The db subnet group name 
    Value: !Ref DBParameterGroup

We can deploy CloudFormation templates using the CloudFormation stack.

Template description

The template consists of 3 parts. Parameters, resources and output parts.

parameter:

In the resource section, we ask users to enter the dynamic variables of their template. In our case, users should replace the VPC and subnet ID with their respective VPC and subnet ID. In addition, users will be prompted to enter their database master username and password. Please make sure you do not use admin as the main username.

Resources:

The user defines the AWS resources to be created here. For our case, we first create a database instance security group. Users should change the security group entry to reflect the CIDR IP block they wish to allow access to the database instance.

Next, it creates the database subnet and parameter group. The subnet group defines the subnet in which the database cluster and instance are created. Through the parameter group, you can manage the database engine configuration. The user should browse the attributes of the parameter group and change it to its specific requirements. In addition, users should pay attention to the name and label to customize as needed.

Then create a document database cluster. Just like above, the user should go through all cluster properties and change them to meet their requirements.

Finally, create a database instance. However, users should browse the template and change the availability zone, instance class, and preferred maintenance requirements to match their specific needs. In addition, the database instance identifier and label should be customized to meet user requirements.

Output:

The output part of the template indicates that CloudFormation outputs the name of the resource created. For example, in this example, we have instructed the template to output the names of clusters, subnets and parameter groups.

Similar guides:

  • Use CloudFormation to set up an AWS RDS MySQL database instance
  • Set up Elasticsearch Cluster with Kibana on AWS
  • How to stream logs in AWS from CloudWatch to ElasticSearch
  • How to create AWS Network Architecture using CloudFormation

Happy Building! ! !

You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose

Sidebar