Build an AWS Lambda Serverless function with Kotlin and MongoDB
Rate this video
00:00:00Introduction
The presenter introduces himself and the tutorial's objective: creating an AWS Lambda function using Kotlin and MongoDB. He explains that the tutorial will leverage the Kotlin driver that MongoDB offers.00:03:45Setting Up the Project
The presenter demonstrates how to set up the project using IntelliJ. He creates a new project, names it, and saves it to his desktop. He also explains the use of Gradle for dependencies.00:07:30Creating and Editing Files
The presenter shows how to create a new project file and edit the Gradle file. He also explains the importance of creating a 'fat jar' for AWS and how to add dependencies to the project.00:11:15Establishing MongoDB Connection
The presenter demonstrates how to establish a connection to MongoDB and get a reference to the database and collection. He also explains how to create a filter for the find operation.00:14:59Uploading to AWS Lambda
The presenter guides viewers on how to upload the project to AWS Lambda. He explains how to add the jar file, edit the handler information, and add environment variables.00:18:44Testing the Function
The presenter demonstrates how to test the function on AWS Lambda. He creates a basic test and runs it, showing the results. He also explains how to modify the test to search for a specific title.00:22:25Conclusion
The presenter concludes the tutorial by summarizing the steps taken and encouraging viewers to like the video, subscribe to the channel, and leave comments.The primary focus of the video is to guide viewers on creating an AWS Lambda function using Kotlin and MongoDB, and how to upload and test the function on AWS Lambda.
🔑 Key Points
- The tutorial leverages the Kotlin driver that MongoDB offers.
- The presenter uses IntelliJ for the tutorial, but any IDE can be used.
- The project involves creating a serverless function that will create a jar file to upload to AWS and use their Java runtime.
- The presenter demonstrates how to establish a connection to MongoDB and get a reference to the database and collection.
- The tutorial also covers how to upload the project to AWS Lambda and test the function.
🔗 Related Links
Full Video Transcript
hey everyone I'm Nick raboy from mongodb and in this tutorial we're going to see how to create an AWS Lambda function so a serverless function using kotlin and mongodb so this particular tutorial is going to Leverage The fairly new kotlin driver that mongodb offers and we're going to see that it's pretty easy to create these serverless functions because in the end kotlin will create a jar file that will allow us to upload to AWS and use their Java runtime so even though we're using kotlin it still will work on the Java runtime all right so up on my screen you'll notice that I do have IntelliJ this is what I'm going to be using for this tutorial honestly you can use whatever you want it doesn't really matter this is just a convenience thing for me but what I'm going to do is I'm going to create a new project and this new project I'm going to go ahead and call it Lambda kotlin and I'm going to save this to my desktop this is going to Leverage The kotlin Language I'm going to be using Gradle from my dependencies so my dependencies like mongodb and that Gradle file in in the spirit of kotlin we're going to create it as kotlin rather than groovy and I'm just going to go ahead and create this project now you'll notice that the project is created we're going to be editing the Gradle file we're also going to be creating a new project file of our own to store all of our code we're going to focus on getting things created and up to speed first and then we're going to worry about actually dabbling with mongodb or dabbling with AWS Lambda so all of that's going to come later so the first thing that I want to do is I want to open up my build.gradle.kts file we're going to add a few dependencies here to make our project successful the first thing that we want to do is we want to add a package that will allow us to create a fat jar so AWS expects us to either upload a jar with all of the dependencies and stuff built into it or a zip with a jar in it we we have to do a fat jar because if it's missing all of our dependencies we're probably going to get errors related to either the Mong and B driver or the Lambda dependencies or something else so the easiest way to do that with kotlin is to use a package called Shadow and we can create what's called a shadow jar so to do that what we can do is we can say ID and we can say com dot GitHub dot John ringelman dot shadow and we're going to provide a diversion and for this particular tutorial we're going to say 7.1.2 and we can go ahead and save it we can refresh our dependency list if we want and it'll go ahead and get that dependency for us so next we want to Define our dependencies so our dependencies will include the driver for kotlin it will also include the dependency for Lambda so inside of the dependency section what we can do right below the test implementation let's go ahead and add a few dependencies so what I'm going to do is I'm actually going to paste them in and I'll explain what they mean so the first two are related to AWS Lambda and it will allow us to create a class and it will basically work with AWS Lambda how it how it expects next up we do have the mongodb bson package that way we can work with bson data and as well as the mongodb driver for kotlin and this is just going to be a basic example we're not going to do anything extravagant here but it will set the foundation for for more complex projects uh the next thing that we want to do is we want to Define what our main class is because our our shadow jar dependency depends on what our main class will be and that's going to be where we add all of our code so what we want to do is we want to Define application and we want to say mainclass dot set and this is where we would Define where our Handler is going to be so our AWS Lambda Handler and the naming convention is not too important as long as we're consistent throughout but we're going to say examples it's going to be part of the example package and it's going to be in a Handler class now you'll notice that it probably is throwing some kind of error right here instead of our plugins we probably also want to add the application plugin and it should clear things up and it did it just took us it just took a second to reset so I'm going to go ahead and import all these Gradle dependencies and at this point in time we should be ready to start coding if we end up getting any kind of funky errors in the future as we progress we'll we'll revisit this Gradle file but ultimately we really just wanted to get the shadow dependency for our jar and we wanted to add our Lambda and mongodb dependencies so now what we want to do is we want to actually create our class Handler and we want to create it in the example package so we're going to go to main we're going to go to kotlin and we're going to say create a new package and this package is going to be called example and inside the example package we're going to create a new kotlin class and this kotlin class is going to be called Handler so it should look something like this and remember we're using kotlin not Java and the and it's not a strict requirement that we have to use IntelliJ you would just reproduce these steps however you see fit for whatever IDE or even a shell tool that you're you're planning on using all right so we have that uh done so I'm going to go ahead and paste in the import lines this is going to be relevant to mongodb for kotlin and it's going to be relevant to Lambda and everything that we plan to do is going to be inside of this Handler class and we're going to make some modifications so that way it's up to spec when it comes to Lambda because we have to do things the Lambda way so what we're going to do is first of all we're going to extend the Handler so it's going to say it's we're going to extend it to request Handler and this is going to be a type map so we're going to have a map of a string value and a string key and for now what we're going to do is we're going to say void so basically what we've said by extending the request Handler is that we're going to be able to accept a map of strings and the response for now is going to be basically null or void and that's going to change we're going to be returning actual data from mongodb but for now it's easiest just to leave it at that so that way we avoid any kind of errors now what we want to do inside this Handler class is we want to create an override function so we're going to say override and we're going to say fun and we're going to say handle request and we're going to accept an input so this input because we've defined it in the request Handler as a map of string it also needs to be a map of string so we're going to say map string we're gonna we're gonna include the context and we're going to return void because that's what we're expecting to return as of now so we're going to say return null now let's start worrying about the mongodb side of things so what we want to do is we want to establish a connection to mongodui and we want to go ahead and get a reference to our database and a reference to our collection and this is we're going to be switching between the mongodb atlas dashboard and I'm going to be switching between the code but let's go ahead and get the code in place first and I'm going to walk you through what that looks like inside of mongodb itself so outside of the handle request function this is where we want to Define our mongodb client so I'm going to say private value and this is going to be client which is our name and we're going to say client equals so client is the type we're going to say Mungo client dot create and I could just hard code a connection string in here it's not it's not advisable it's definitely best practice to use some kind of environment variable or configuration file so that we don't accidentally commit this information to say GitHub or or somewhere else because your mongodb connection information has your username and password in it so it's a good idea to I make this excluded from your code so in our example what we're going to do is we're going to say system dot get EnV and we're going to provide a environment variable for it so we're going to say mongodb Atlas URI so that's just the name of our future mongodb environment variable so this environment variable we're going to be adding it to the AWS Lambda function in the cloud so we won't ever need to worry about actually hard coding this in our code so we're going to assume this connection will be valid as of right now inside of the handle request this is where we want to get a reference to our database and our collection so I'm going to say value database this is going to be of type database and I'm going to say client dot get database and for this example if you've ever worked with mongodb and I'm going to show you this in a moment if you ever worked with mongodb there are sample data sets available I'm going to be using the sample empflix data set and you can load this directly in mongodb I'll show you in just a moment but I'm going to say sample mflix is my database name and I'm going to say value and I'm going to say collection and I'm going to say collection this is going to be of type document and it's going to be database dot get collection and for this collection I'm going to say movies so sample influx is kind of like IMDb and it just has information about movies so I'm going to pull from the movies collection that includes things like the title the plot things like that anything related to the movie itself now if I go into my web browser and I go to mongod to be this so this is the mongodb dashboard that I was talking about so I have a examples instance created if I wanted to load a sample database I could say load sample data set and that would include sample inflix and that would include the movies collection that I'm working with but what I'm going to do is I'm going to say browse collections and it actually looks something like this sample inflix movies and then we have quite a quite a few movie documents and the data for this example doesn't really matter because it's such a simplistic example that we're going for when it comes to mongodb and AWS Lambda but in case you're curious now what is important to us is if we go to the database tab what we want to do is we want to click connect we can scroll down we can go to drivers and we can choose the driver that we're working with in this case kotlin and this would be the connection string that I want to use now I'm not going to add this to my code I'm not going to add this to my local environment variables this will be used when we go to the AWS Lambda dashboard but this is what we would copy and and what we want to do is we would want to create a username and we'd want to create a password so if you close this if you don't already have a username and password you can go to the database access rules and you could add a new database user and then you would fill that in when you when you paste in your connection URI string you'd also need to set up some network access rules so that way Lambda can actually reach mongodb but that's out of the scope for this tutorial we're just going to open up the access to all IP addresses we're going to give a pretty generic user and it should be enough to to get us going so going back into our code now we can actually focus on communicating with mongodb so in theory we have a connection established so we would create this client we would get reference to our database and collection we're not actually querying mongodb we're not inserting any data we're not we're not doing anything particularly interesting at all for this example we're just going to query mongodb and the best way to handle that for this example is if we go into the handle request override function let's go ahead and create a find operation so we can say Val results and the result type is going to be list and it's going to be a type document inside of that list and we're going to say collection dot find so this is a find operation it's going to find one or more documents provided that they exist and we're going to pass in a filter in just a moment but we're also going to limit the result set so because we're using the movies collection the movies collection has tens of thousands of documents we don't want to return all of them and for this example we're not going to be working with cursors so what we want to do is we want to just maybe limit that result set to five documents and we also want to convert it to a list because that's what we're expecting as the end result so we're going to say to list we don't have a filter in here yet so we're basically going to say we're going to return all documents from this collection limiting it to five but let's go ahead and improve upon that we're actually going to create a filter here so I'm going to say VAR I'm going to say filter and this is going to be of type bson our filter and we're going to say bson document and we're going to go ahead and add that to this particular line on on line 24. so we have a filter it's empty it's still going to get all documents here so let's go ahead and prove upon this let's go ahead and say that if the user provides us a title let's go ahead and search based on that title and we're not actually doing a search we're doing a a just a filter so a match a match query searching if you want to do an aggregation pipeline in mongodb you could do Atlas search which is full text search but that is out of the scope for this particular example so what we're going to say is we're going to say if we're going to say input so input being the user input for this particular function when they call it we're going to say contains key so if it contains a key called title remember we're working with a map and it's of type string for the key and string for the value if we include a title so it contains that key and we're also going to say if the input is not basically we want to make sure that it's not null or empty so we're gonna we're gonna get the input we're going to say title so the the title key exists now we're just checking to see what that value is so we're going to make sure it's not is null or empty and as long as it passes that criteria so it exists and it contains data we're going to set the filter to that particular value in the in the title so we're going to say filter equals filters dot EQ we're going to say title and we're going to say input dot get and we're going to say title so the First Title Here is the actual key inside of mongodb so our field and this would be our value so that's what our filter is going to be so as long as the the field in mongodb the title field matches whatever this value is then return those documents so they don't need to match it's just it's just coincidence that they they both match in this particular case so if if if a title exists we're going to use that as our filter criteria and we're still only going to limit our results to five records maximum it may it may never reach that much depends on the movie that we call now we're still returning null here we need to make an adjustment so instead of void what we want to say is we want to say the list and we want to say document so that's the type of data that we want to return here we're also going to need to make that adjustment on line 17. so we're going to say list document as our return and now we don't have to worry about null instead what we can do is we can return results because we're working with a list here and the type is list of document so returning the results now in theory this should be enough to upload to AWS Lambda it should work and we're once again we're going to get that connection string when the time comes and add it to Lambda but the code itself should be good so what we want to do is we want to build our jar so go ahead and open up whatever menu you're using this could be command line and you could be using Gradle directly from the candleman line I'm using IntelliJ so what I'm going to do is I'm going to say Gradle and I'm going to say shadow jar because I'm using the shadow jar plugin I'm going to hit enter so it says that it finished successfully so if I go to my finder I can actually find it inside of my project so I'm in Lambda kotlin I can go to build and I can go to Libs and it should be there it says Lambda kotlin and then it's got all this other stuff attached to it I can upload this jar AS is or I can add it to a zip file it really depends on what you're up to and what your project calls for but this is the file ultimately that I would want to add to AWS now let's go back to my web browser so I'm going to go to my Lambda dashboard here and I'm going to create a new function this function I'm going to author from scratch I'm going to call it maybe mongodb example and the runtime it's not going to be node it's actually going to be Java so after I've chosen my Java runtime I'm going to make sure that I choose the architecture that I want and I'm going to go ahead and create the function all right so inside of the function landing page there's going to be a few steps that we want to do first step what I want to do is I want to upload that jar file so like I said we're going to go to the libs directory and I'm going to upload that jar file directly and I'm going to save it all right so it's uploaded we're not quite done yet though so if we look at our Handler information for the runtime settings this is not correct it could be if we've decided to do it this way but based on our project that we've just done that's not correct so we're going to edit it so our package is called example our actual class is called Handler and our actual function is called handle request so just make sure that you modify it to to match whatever you're set up with so I'm going to click save all right so that's done next thing I want to do is I want to go to configuration and I want to go to the environment variables this is where we add our environment variable strength so I'm going to say add environment variable we called it mongodb Atlas URI and now we actually want to add that value in and you could do different encryption options for these environment variables I'm just going to paste it right in so I'm going to go back to my mongodb Atlas dashboard here I'm going to go to data services I'm going to click connect drivers kotlin I'm going to copy this I'm going to go back into Lambda I'm going to paste it and I will have to update the username and password so what I'm going to do is I'm just going to populate this and click save the next thing that we want to do is we want to test it out we've uploaded everything we've we've added our connection information to Lambda itself in theory we should be good now so I can go to the test tab and if I scroll down I can just create a very basic test here I don't I don't really need to name anything if I don't want to I don't need to save it the event Json we're going to leave it like default for now because we don't have anything looking for key one key two key three we're looking for title so it should should ignore any extras so what I want to do is I want to click test so the test finished let's go and check out the details so looking at the details here it looks like it found data now remember we're limiting our results to five so I do trust that there are a maximum of five movies here uh remember this collection has uh many of thousands of of movies so uh five will definitely be the maximum here it shouldn't be less now let's go ahead and make a change let's go ahead and go down we're going to test again but this time around we're going to say title we're going to say this one's going to be the Terminator we don't have to remove these others because we're not we're not doing any kind of checks but I'm going to remove them anyways just keep it nice and clean here and I'm going to run the test again in theory we should have uh hopefully just one movie come back so if I go up uh we have a human looking cyborg so this looks good this is probably the Arnold Schwarzenegger movie that I wanted and the result set is just one here it's very short um so it did work out successfully um so just a just a recap on on everything that we did real quick um so inside of our build.gradle file this is a kotlin version of it we did include a plug-in so that way we can create a fat jar so a jar bundle of of our of our project and dependencies uh we added we defined it the main class here for our project so this would work with our shadow dependency so we added the Lambda dependencies we also added the mongodb dependencies inside of our code we're accepting a map of strings so like you saw in the test I was able to add a title in that map so in that Json object we are establishing a client it uses an environment variable we're getting our our database and collection reference is and we are doing a simple find operation on our data so nothing too extravagant but you can definitely take this and you can build pretty powerful things in a serverless manner so in my example I was using a free tier of manga to be Atlas I was using a free cluster size if you wanted to go full serverless with this example you could create a serverless instance of mongodb so that way not only does your application scale for demand so your your Lambda function will scale but your database will also scale with demand so it's not always on it's only you're only being built per usage and it it could be a very convenient and efficient way to do business with your applications that use mongodb and kotlin if you like this video please take a moment to hit that like button on the video subscribe to the YouTube channel drop me a comment if if something didn't make sense let me know about it if you like this video if you want to see more videos like this also drop me a comment I'd love to produce more on kotlin or even AWS until next time have a great rest of your day