Is AI harder than we think?

Even with all the seemingly AI breakthroughs, long-promised technologies such as robots, chatbots, self-driving cars turned out to be much harder and postponed to the future. Most of us are confused…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Using Mongo DB Atlas with AWS ElasticBeanstalk

So recently I started working with AWS for my deployments and also realized that mLab (where I hosted all of my staging/prod databases) was now part of MongoDB and the alternative for new users is to create a Mongo DB Atlas now.

I made some researching and found how to wire up Atlas with an AWS backend (mostly Lambda tutorials, which are pretty cool, I like a lot the serverless trend). However, I did not find anything specific for AWS beanstalk, which as you might know, scales and not necessarily might have a static public IP (Atlas clusters can only be accessed from within whitelisted IPs, or at least that is what us beginners understand). So I decided to make this short tutorial on how to deploy an Elasticbeanstalk environment which will interact directly with a MongoDB Atlas Cluster that is also hosted within the AWS ecosystem, all happening via the very common use of VPCs.

If you do not know about VPCs, be sure to take a look at the AWS documentation or some very useful tutorials on this very site to get familiar (I am by no means an expert on the subject).

Now let’s get to it…

Let’s imagine you have a Node JS application which has been deployed to AWS using the ElasticBeanStalk service. Firstly, it is super important to verify that you declare your environment as being part of a VPC (you can very well use the default one AWS gives you)

Create an environment using the Nodejs platform

Be sure to click the Configure more options button, since once an environment has been created, you CANNOT attach it to a VPC or change the VPC for it.

Attach the elastic beanstalk environment to a VPC

I am using the default VPC for this exercise, and assigning some availability zones based on the subnets that are part of the VPC.

That’s it for the initial environment config. Be sure to check out the URL on the EBS console and confirm that the nodejs app is running properly.

Now, we need to configure SSH for our instance(s) associated to the EBS environment. We could do this by using the eb cli on our application’s directory, by prompting the following command:

eb ssh — setup<env-name>

For this example we would use “test-env” as env-name.

Or we could directly go to the EC2 dashboard on the AWS console, and configure a new inbound rule for the running instance. For this, we locate our instance, and observe the security group to which it is attached.

We can here observe that the SG associated to the instance is the “default” one. So that we can edit the inbound rules for that particular SG.

Adding SSH rule for my computer’s public IP

This config was done to be able to access the instance and easily test the connectivity to our Atlas Cluster.

We then proceed to creating and configuring the MongoDB Atlas’ side.

Please create an Atlas account if you do not already have one and notice that this procedure will only work for clusters that are outside of the sandbox environment (i.e. you must pay for using these clusters, unfortunately, but hey! it is still MUCH CHEAPER than Amazon Document DB).

For my example I will be using an instance size of M10, which is actually the cheapest one that qualifies for being used with VPC Peering.

After creating the cluster, you must basically follow along with the instructions presented by the people on MongoDB team itself. I add here the blog post which I found the most useful for doing this process.

The tutorial is pretty well explained, you simply create a new peering connection by going to the security tab for your cluster and add the private IP that corresponds to your AWS account’s VPC.

We can now test that our EC2 instance can properly interact with the Atlas cluster, the best part about this is that everything is done within AWS VPCs! Meaning that no traffic from the database is exposed through the internet. We are just enabling the SSH connection for the instance, but after testing everything is OK this can as very well be removed and everything will remain within the AWS private (and secured) network infrastructure.

Finally, after connecting to our EBS instance using ebs ssh <env-name> (test-env for our example) from within the eb cli. Once accessed, we can download the mongo shell for our instance. We could use the instructions provided from AWS documentation on the following article:

Only pay attention to Step 1 of this article, since the remainder is only intended for when using AWS Document DB (So freakin expensive!)

We can then obtain the connection string for the mongo shell directly on atlas by clicking on the “Connect” button for our cluster.

Atlas Cluster Overview. Click on Connect

We select “connect via mongo shell” and copy the connection string outputted from our cluster.

Connection string for the cluster (mongo shell)

We enter this command on our EBS SSH session (EC2 instance) using a proper username. Mongo shell will then ask us for the password and if everything goes smoothly we should see something like this.

output when connecting to cluster from ec2 instance

Omit all of the uncomfortable intermediate logging, at the very end we see the “Mongo DB Enterprise” text, which means we are now connected to our Atlas Cluster (on a specific “test” database on this case).

That’s it! We have proved that our EC2 instance can access the Atlas Cluster, and since the EBS enviroment is part of a VPC, we guarantee that any instances deployed by the scaling policies will be able to interact with our cluster , and hence its databases.

This is just a basic setup, of course you can configure many additional features for all this process, but for me this was very explanatory and helped me prove that my backend can interact with my DB, and hopefully it was also useful for you!

Add a comment

Related posts:

Custom Cutie Cloned?

As a part of our collab with TRON, we made a custom cutie for TRON’s CEO Justin Sun. It turned out accurate and awesome in every way. We already mentioned that the TRON presale went out of control…

How to Plan a Trip to Yala

The Yala National Park is a wildlife paradise that has lots to offer. Here are of the most popular activities you can engage in when you visit the national park and don’t forget to include them in…

State of the World 2011

Originally published at books.google.com.. “State of the World 2011” is published by Fidelis Zvomuya.