{"id":74386,"date":"2020-10-18T22:31:38","date_gmt":"2020-10-18T19:31:38","guid":{"rendered":"https:\/\/computingforgeeks.com\/?p=74386"},"modified":"2023-11-17T03:02:38","modified_gmt":"2023-11-17T00:02:38","slug":"create-amazon-documentdb-database-on-aws","status":"publish","type":"post","link":"https:\/\/computingforgeeks.com\/create-amazon-documentdb-database-on-aws\/","title":{"rendered":"Create Amazon DocumentDB (MongoDB) Database on AWS With CloudFormation"},"content":{"rendered":"\n<p>This article helps a user create a document database cluster with a single primary instance using a CloudFormation template. A document database is important when a user wants to run Mongo database workloads on AWS (Amazon Web Services). The Amazon DocumentDB (with&nbsp;<a href=\"https:\/\/aws.amazon.com\/documentdb\/what-is-mongodb\/\" target=\"_blank\" rel=\"noreferrer noopener\">MongoDB<\/a>&nbsp;compatibility) is a scalable, fully managed, fast and highly available document database service that supports MongoDB workloads.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"946\" height=\"405\" src=\"https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/10\/AWS-Amazon-DocumentDB.png\" alt=\"\" class=\"wp-image-74511\" title=\"\" srcset=\"https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/10\/AWS-Amazon-DocumentDB.png 946w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/10\/AWS-Amazon-DocumentDB-300x128.png 300w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/10\/AWS-Amazon-DocumentDB-768x329.png 768w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/10\/AWS-Amazon-DocumentDB-696x298.png 696w\" sizes=\"auto, (max-width: 946px) 100vw, 946px\" \/><\/figure>\n\n\n\n<p>This managed non-relational database service makes it easier to store, query and index JSON data. This database service is designed from the ground-up to give guarantee scalability, performance, and availability you need when operating mission-critical MongoDB workloads at scale.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-setup-pre-requisites\">Setup Pre-requisites<\/h2>\n\n\n\n<p>The user will need to have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An AWS Account<\/li>\n\n\n\n<li>Created a user with permissions to create resources on the AWS Account<\/li>\n\n\n\n<li>An IDE like visual studio code to write and edit your CloudFormation Template.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cloudformation-template-used\">CloudFormation Template used<\/h2>\n\n\n\n<p>Kindly find below the CloudFormation Template. The template will create:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The database instance security group.<\/li>\n\n\n\n<li>Database subnet group.<\/li>\n\n\n\n<li>The database parameter group.<\/li>\n\n\n\n<li>Document database Cluster.<\/li>\n\n\n\n<li>Database instance. <\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>---\nAWSTemplateFormatVersion: \"2010-09-09\"\nDescription: Template to Create a document DB parameter group, subnet group and cluster\n\nParameters:\n  VPC:\n    Type: String\n    Description: The VPC to create the cluster\n    Default: vpc-ID\n\n  PrivateSubnet01:\n    Type: String\n    Description: The subnet for the DB cluster\n    Default: subnet-ID\n\n  PrivateSubnet02:\n    Type: String\n    Description: The subnet for the DB cluster\n    Default: subnet-ID\n\n  MasterUsername:\n    Type: String\n    Description: The username for our database.\n  \n  MasterUserPassword:\n    Type: String\n    Description: The password for the database.\n    \"NoEcho\": true\n\nResources:\n   DBSecurityGroup:\n    Type: AWS::EC2::SecurityGroup\n    Properties:\n      GroupDescription: \"DB instances security group\"\n      GroupName: \"test-db-instance-SG\"\n      VpcId: !Ref VPC\n      SecurityGroupIngress: \n        - \n          CidrIp: \"*.*.*.*\/32\"\n          FromPort: 22017\n          IpProtocol: \"tcp\"\n          ToPort: 22017\n      SecurityGroupEgress: \n        - \n          CidrIp: \"0.0.0.0\/0\"\n          IpProtocol: \"-1\"\n      \n   DBSubnetGroup: \n    Type: AWS::DocDB::DBSubnetGroup\n    Properties: \n      DBSubnetGroupDescription: \"test document db subnet group\"\n      DBSubnetGroupName: \"eu-central-1-test-db-subnet-group\"\n      SubnetIds: \n        - !Ref PrivateSubnet01\n        - !Ref PrivateSubnet02\n      Tags: \n        - Key: Name\n          Value: eu-central-1-test-db-subnet-group\n        - Key: createdBy\n          Value: Maureen Barasa\n        - Key: Project\n          Value: test-blog\n        - Key: Environment\n          Value: test\n\n   DBParameterGroup:\n    Type: AWS::DocDB::DBClusterParameterGroup\n    Properties:\n      Description: \"our test document db parameter group\"\n      Family: docdb3.6\n      Name: test-db-parameter-group\n      Parameters: \n        audit_logs: \"disabled\"\n        tls: \"enabled\"\n        ttl_monitor: \"enabled\"\n      Tags: \n        - Key: Name\n          Value: eu-central-1-test-db-cluster\n        - Key: createdBy\n          Value: Maureen Barasa\n        - Key: Project\n          Value: test-blog\n        - Key: Environment\n          Value: test\n\n   DBCluster:\n    Type: AWS::DocDB::DBCluster\n    Properties: \n      BackupRetentionPeriod : 5\n      DBClusterIdentifier : eu-central-1-test-db-cluster\n      DBClusterParameterGroupName : !Ref DBParameterGroup\n      DBSubnetGroupName : !Ref DBSubnetGroup\n      MasterUsername : !Ref MasterUsername\n      MasterUserPassword : !Ref MasterUserPassword\n      Port : \"27017\"\n      PreferredBackupWindow : \"23:00-23:59\"\n      PreferredMaintenanceWindow : \"sun:00:00-sun:05:00\"\n      VpcSecurityGroupIds:\n        - !Ref DBSecurityGroup\n      StorageEncrypted : true\n      Tags: \n        - Key: Name\n          Value: eu-central-1-test-db-cluster\n        - Key: createdBy\n          Value: Maureen Barasa\n        - Key: Project\n          Value: test-blog\n        - Key: Environment\n          Value: test\n\n   DBInstance:\n    Type: AWS::DocDB::DBInstance\n    Properties:\n      AutoMinorVersionUpgrade: true\n      AvailabilityZone: \"eu-west-1a\"\n      DBClusterIdentifier: !Ref DBCluster\n      DBInstanceClass: \"db.t3.medium\"\n      DBInstanceIdentifier: \"test-cluster-instance-1\"\n      PreferredMaintenanceWindow: \"sun:00:00-sun:05:00\"\n      Tags: \n        - Key: Name\n          Value: eu-central-1-test-db-instance\n        - Key: createdBy\n          Value: Maureen Barasa\n        - Key: Project\n          Value: test-blog\n        - Key: Environment\n          Value: test\n\nOutputs:\n   Cluster:\n    Description: The DB Cluster Name\n    Value: !Ref DBCluster\n    \n   SubnetGroup:\n    Description: The db subnet group name \n    Value: !Ref DBSubnetGroup\n\n   ParameterGroup:\n    Description: The db subnet group name \n    Value: !Ref DBParameterGroup\n<\/code><\/pre>\n\n\n\n<p>We can deploy the CloudFormation Template using a CloudFormation stack.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-the-template-explained\">The Template Explained<\/h2>\n\n\n\n<p>The template comprises 3 sections. The Parameters, Resources and Outputs sections. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-parameters\">Parameters:<\/h4>\n\n\n\n<p>In the resources section, we require the user to input the dynamic variables of their template. For our case, the user should replace the<em> VPC<\/em> and <em>subnet ID&#8217;s<\/em> with their respective VPC and subnet ID&#8217;s. Also, the user will be prompted to input their database master username and password. Kindly ensure that you do not use admin as the master username.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-resources\">Resources:<\/h4>\n\n\n\n<p>Here the user defines the AWS resources to create. For our case, we start by creating the database instance security group. The user should change the security group ingress to reflect the CIDR IP Block that they would like to permit access to the Database instances.<\/p>\n\n\n\n<p>Next, it creates the DB subnet and parameter groups. The subnet group defines the subnets where the database cluster and instances are created. The parameter group allows you to manage your database engine configurations. The user should go through the parameter group properties and change to their specific requirements. Also, the user should pay attention to the names and tags to customize as needed. <\/p>\n\n\n\n<p>Then the document database cluster is created. Just as above, the user should go through all the cluster properties and change them to match their requirements. <\/p>\n\n\n\n<p>Finally, the DB Instance is created. However, the user should go through the template and change the availability zone, the instance class, and the preferred maintenance needs to match their specific needs. Also, the DB instance identifier and tags should be customized to meet user requirements.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-outputs\">Outputs:<\/h4>\n\n\n\n<p>The outputs section of the template instructs CloudFormation to output the names of the resources created. For example, in our case, we have instructed the template to output the names of the cluster, subnet, and parameter groups. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-important-links\">Important Links<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/aws.amazon.com\/documentdb\/\" target=\"_blank\">https:\/\/aws.amazon.com\/documentdb\/<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/aws.amazon.com\/blogs\/database\/category\/database\/amazon-document-db\/\" target=\"_blank\">https:\/\/aws.amazon.com\/blogs\/database\/category\/database\/amazon-document-db\/<\/a><\/li>\n<\/ul>\n\n\n\n<p>Similar guides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/computingforgeeks.com\/setup-aws-rds-mysql-db-instance-with-cloudformation\/\" target=\"_blank\" rel=\"noreferrer noopener\">Setup AWS RDS MySQL DB Instance with CloudFormation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/computingforgeeks.com\/setup-elasticsearch-cluster-with-kibana-on-aws\/\" target=\"_blank\" rel=\"noreferrer noopener\">Setup Elasticsearch Cluster with Kibana on AWS<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/computingforgeeks.com\/stream-logs-in-aws-from-cloudwatch-to-elasticsearch\/\" target=\"_blank\" rel=\"noreferrer noopener\">How To Stream Logs in AWS from CloudWatch to ElasticSearch<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/computingforgeeks.com\/create-aws-network-architecture-with-cloudformation\/\" target=\"_blank\" rel=\"noreferrer noopener\">How To Create AWS Network Architecture With CloudFormation<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-center\"><strong>Happy Building!!!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article helps a user create a document database cluster with a single primary instance using a CloudFormation template. A document database is important when a user wants to run Mongo database workloads on AWS (Amazon Web Services). The Amazon DocumentDB (with&nbsp;MongoDB&nbsp;compatibility) is a scalable, fully managed, fast and highly available document database service that &#8230; <a title=\"Create Amazon DocumentDB (MongoDB) Database on AWS With CloudFormation\" class=\"read-more\" href=\"https:\/\/computingforgeeks.com\/create-amazon-documentdb-database-on-aws\/\" aria-label=\"Read more about Create Amazon DocumentDB (MongoDB) Database on AWS With CloudFormation\">Read more<\/a><\/p>\n","protected":false},"author":16,"featured_media":74511,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[461,511,2680,299,50],"tags":[513,30816,34701,343],"class_list":["post-74386","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-databases","category-aws","category-cloud","category-how-to","category-linux-tutorials","tag-aws","tag-cloudformation","tag-documentdb","tag-mongodb"],"_links":{"self":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/74386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/comments?post=74386"}],"version-history":[{"count":0,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/74386\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/media\/74511"}],"wp:attachment":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/media?parent=74386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/categories?post=74386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/tags?post=74386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}