AWS Lambda Java: How to create your first AWS Lambda Function in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everyone my name is Dan Vega and today you're going to learn how to build your first AWS Lambda function using Java so I recently put together a guide called serverless Spring I'll leave a link to that in the description below also a video version of that is coming soon but the idea was how can you build serverless functions using spring Boot and spring Cloud function and so along the way I did a bunch of research I picked a platform in this case AWS and I learned how to build serverless functions from scratch so this video and the ones that are going to follow are what I've learned and I want to teach that back to you so today we're building our first function using Java and really just plain old Java we'll talk about how to go ahead and create the project from scratch how to upload it to the AWS console and how to go ahead and test it out this is where we need to start and progress and see some of the pain points and building serverless functions to see where something like spring Cloud function can really help us out so I really hope you enjoy this video what are we waiting for let's create our very first aw aw Lambda function from scratch using Java let's jump into it alright so I'm going to get started here by using IntelliJ I'm using the ultimate edition you can do this in the community Edition again if you're creating a Java project you can use whatever ID or text editor you like we're just creating a simple Maven project here so it should work with whatever tool you're used to using so I'm going to get started here and create a new project so I'm using a maven archetype here so I'm going to come into a location I'm going to call this AWS playground and the name of this project is going to be hello Lambda this is our first Lambda project in Java so we're going to call this like a hello world so here's where we're storing it here's where it will be created Dev AWS playground hello Lambda uh we do want to go ahead and create a git repository for it the jdk that we're using is coretto 11 this is from Amazon and then we got to pick an archetype so when I'm using Maven here I'm going to use uh just the quick start so the quick start will give us just kind of a basic shell of an application it will give us enough to go ahead and get started from the advanced settings I'm just going to change this group to dev.dan Vega hello Lambda is fine and 1.0 snapshot version that is fine as well so I'm going to go ahead and click create and we'll see this go ahead and get set up for us everything looks good I'm going to go ahead and delete this we need to set some other properties here I'm going to say that the maven compiler dot source is going to be 11. oops and then the maven.compiler.target is also going to be 11 and that should be enough there as far as our dependencies go I'm just going to get rid of this and say let's use junit Jupiter from org.j unit Jupiter and the current version is 5.8.2 so that gives us enough to go ahead and get started so the first thing we want to do is we want to create a class and we want to go ahead and create a class that will hold our function so I'm going to go into Source Main and actually because we are getting rid of the old version of g in it I'm going to get rid of that test I'm going to go ahead and get rid of this class too that gets created for me I don't need that so I'm going to create a new class in here we're going to say new Java class and we'll call this hello Lambda and this is just a normal Java class right and we're just going to create a method in here and this method is going to be the functions so we're going to say public this is going to return a string you can call this whatever you want I'm going to call it handle request and again it's just going to return a string so we'll return hail hello AWS Lambda and that's it that is the entirety of our simple function now I know this is very simple but you got to start somewhere right what I also want to do though is I want to go ahead and generate a test it's good to get in the business of testing our functions so let's go ahead and create a test we'll call it hello Lambda test we're going to go ahead and test that and I'm going to call this should return Hello message so this is going to be a test oops we didn't pull in the new version of Genie unit at this point this is no good so we want to go ahead and use a test and what we are testing here so the system under test is what I'm calling this set is the new uh that hello Lambda class that we created so what we want to do is use the assertions dot assert equal and what we want to test is the expected output should be hello AWS Lambda and what we get back is the system under test and we want to call handle request so this assertions we can import as a static import and then we should be able to clean that up a little and let's go ahead and run this test and our test passes of course but again this is a good practice to get into early on as we start to develop more complex functions it's good to start to test these so we have a test we have a function we need to go ahead and package this this function up so that we can go ahead and upload this to Amazon so how do we do that we need a What's called the maven shade plug-in so I'm going to go ahead and add a little bit of code here Maven shade plugin I have a little live template for this that just adds this in you can grab this from the GitHub repository uh for this particular tutorial you can find a link for that in the description below but this is just a build plug-in this is the um Maven shade plugin so if you look at that you should be able to see what this does this repackages the project classes together with their dependencies into a single Uber jar so we get everything in one jar and that's really what Amazon is looking for when we upload this uh this jar file with our function in it so with all of that in place we should be ready to go we can go ahead and create a new jaw for this the way that we're going to do this is we're going to open up the terminal so again I'm using the integrated terminal here in IntelliJ you could pop over to a shell if you want whatever your favorite one is I'm going to go ahead and just use this right here and all we're going to say is we're going to say Maven so you should have Maven installed if you're going to run this so make sure you have Maven installed so we're going to say Maven you're going to say clean and then you're going to say package so it's going to run through and you see that that was very fast build success we have um our hello Lambda shaded jar so it moved it to this hello Lambda 1.0 snapshot jar so this jar right here in the Target directory is what we want to use to go ahead and upload to Amazon so with that let's head over to the browser and log into the AWS Council alright so here we are in AWS Lambda under functions you see I have a few functions already I'm going to go ahead and create a new function we're going to author this from scratch we want to give it a function name you can see the recommendation here is to go ahead and name your function like this so I'm going to do that I'm going to start mine with YouTube so we know that this is coming from YouTube and I'm going to say hello Lambda next we gotta choose a run time in our case where we are using Java 11 coretto so we're going to pick that so now that we have that um we want to go ahead and pick our architecture so you can choose whatever architecture you want um you actually can save a little bit money if you're using arm 64 so I'm going to choose that and then we're not going to get into anything else here we can talk about some of the advanced settings later on in this series as we start to develop some other serverless functions so let's go ahead and create our function all right and once that function is created we need to do a couple of things first is the code source so we need to upload this so this is from a zip or a jar so you can also pick an S3 bucket if this is a very large function later on you can upload this to S3 and then pick it from an S3 bucket right now we're just going to upload it from our local machine so I'm in Dev I'm going to go to AWS playground under hello Lambda under Target and then we're going to pick that snapshot jar and we're going to go ahead and Save so once that's been uploaded we need to do one more thing here in the runtime settings we're going to click edit so we need to to define the Handler so this is a full path to a Handler so the package name the class and then the function uh that we're going to go go ahead and call so if we go back to IntelliJ here we can see that our package is dev.dan Vega the class name is hello Lambda and this function name is hello handle request so I'm going to do that same thing here so I'm going to say dev.dan Vega the class is hello lamb oops hello lamb and then the function name is handle request so this should go ahead and work again if you get this wrong and you try to execute your function it'll tell you some you know can't find that particular method and that you can always come back here and correct that if you need to so I'm going to click save and at this point we are ready to test our function now because this is a very simple test we can test it right here in the console and as we're as we're in the console I should mention something else this is just one way to go ahead and create this function you can use the AWS CLI to create the function you have to really you have to get that installed and understand how to use the CLI you can also programmatically create your functions using infrastructure as a code with something called cdk so you can write code to basically push your code up so we'll get into that as we start to go throughout this series but right now this is kind of the hello world let's just use the console to kind of push this up and test this up so when you're testing it you could test the events so you could have events trigger a Lambda function like hey something got dropped into an S3 bucket go ahead and call this Lambda function if you were doing that you can use this event here and create an event to kind of trigger this we're just simply asking this particular function to return a string so I'm going to come down here in the event Json and I'm just going to have an empty string you need to have something here so we're going to have an empty string and we should be able to go ahead and test this and you can see the execution result was successful if you go under details you can see that we are getting that string back that we asked for which is hello AWS Lambda there's some information about the request so we have a request ID the duration and the initiation so these will start to become important as we start to like build real applications here right how long did this function take to execute how long did this function take to come up so what is the cold start on this because this was the first time we ran it this is a cold start so you see the cold start is 381 milliseconds the duration that it took was 6.42 milliseconds here's the resources we had configured for that again this is all stuff that you can configure here's what you were billed for and then here's some log output so if we go ahead and run this again so let's go ahead and test this again remember this was 381 milliseconds and 6.4 if we do that again you see the duration that cold start went way down because it didn't need to call you know it didn't need to like fire up the infrastructure to make that call so the second time in every subsequent request uh not everyone but right now that's going to be slower much slower and then you could see the build duration was two milliseconds so as we keep running these it gets faster this is where we'll start to talk later when we're dealing with Java about growl VM which is ahead of time compilation which is going to increase that Colts which is going to decrease that cold start and the amount of memory that we're using so if you don't care about grow VM right now don't worry about it we'll get in we'll talk about that as we go but as you can see the second time we did this this was faster again here are your logs if you go over to monitor you can get a look at all the different logs here sometimes this takes a little while to come up to load these logs so nobody here you can always come in here and keep refreshing see if they showed up if they don't don't worry about it you can come into configuration there's a whole bunch of things you can configure we'll get into this as we keep going again this is kind of the hello world version of this so I think that's all I want to talk about with this one let's take a look at a second example all right so here I am in a project called hello name and I just wanted to kind of show off that hey we can also go ahead and accept input into our normal our plain old Java function here right so similar structure we have a test if we look in the Palm file we're using Java 11 we're using junit Jupiter we have this Maven shade plugin right but the only difference with this test is now we accept an argument of type string called name and what we'll do in is say hello comma and then the name with the exclamation point after this so again I've already built this once we can go ahead and upload that but I'll go ahead I'll show you that I've already uploaded this and we can take a look at how this works so back in my functions I have a Hello name so if we go into hello name the difference here again you're going to come in you're going to upload this you're going to edit the configuration to say dev.dan Vega hello name handle request but when we go ahead and test this now it needs to accept some input it's not accepting Json this is just a simple string that it's going to take so I'm going to pass my name in and what it should do is then return hello comma Dan what's the exclamation point so let's go ahead and test that out we can see that I succeeded and there is the string that it return so if we want to just change that so it takes my name like that let's go ahead and test it and there's hello Dan and again we can see the duration the build duration max memory use resources config configured so at if this was like some very basic functions that we wrote but what I really want to press here is that we didn't involve AWS libraries at all so in the next tutorial we'll bring in kind of the AWS core libraries and see we see how we can build functions that way and then even add events on top of that but this was just plain Java we didn't need to bring in any dependencies to make this work which I thought it was really cool but again we'll start to build on this as we go so I think that's it uh for this first video in this series I really hope you found some value in this if you did please let me know in the comments below if these are types of videos that you'd like to see I would love to keep making these for you again I am here to make content for you let me know what you're interested in seeing but if hey if you did found value in this please go ahead and hit that thumbs up subscribe to the channel and as always happy coding here we go [Music]
Info
Channel: Dan Vega
Views: 37,217
Rating: undefined out of 5
Keywords: dan vega, aws lambda java, aws lambda java tutorial, aws lambda and serverless java, java lambda aws, aws lambda local development java, aws lambda java project, aws lambda serverless java, aws java lambda, aws lambda tutorial java, aws, amazon web services, aws lambda, java, serverless, lambda, aws lambda tutorial, lambda function, cloud computing, lambda functions, aws lambda using java, aws lambda function, programming, aws tutorial
Id: MaHxZEBRcT4
Channel Id: undefined
Length: 16min 44sec (1004 seconds)
Published: Wed Nov 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.