One of my 2021 goals was to learn more about AWS, and what better place to start than with Serverless! In this blog post, we are going to create an AWS Lambda function, add some JavaScript to return a few random dad jokes, and then test our function to make sure it works correctly.
Let’s get started!
Prerequisites
- To get started you just need an AWS account. You can sign up here.
Create your Lambda Function
- Login to the AWS console
- Browse to the Services page. Search for and select Lambda
- Click the Create Function button
- AWS offers several options to deploy a function. For this blog post, we are going to use the Author from scratch option.
Click the Author from scratch option and fill out the following information in the Basic information section:
Enter your Function name
Runtime – For this example I am using Node.js 12.x
Click the Create function button - Your function is now ready! Notice the index.js file is already created as well as a “Hello from Lambda!” example.
Before we make any changes in the next section, let’s take a minute to look at the example code to better understand what it’s doing.
exports.handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
The exports.handler is the method in your function that processes events. When an event is passed into your function, AWS Lambda runs the handler method. When the handler exits or returns a response, it becomes available to process another event. Events can be generated by other AWS services, HTTP requests, etc.
If you’re interested in learning more about how Lambda works, check out the AWS documentation.
Add the Code
- Copy and paste the following code into the index.js file in Function code section.
var jokes = ["Dad, did you get a haircut?, No, I got them all cut!", "Why don't eggs tell jokes? They'd crack each other up.", "I don't trust stairs. They're always up to something.", "Why couldn't the bicycle stand up by itself? It was two tired.", "What do you call cheese that isn't yours? Nacho cheese.", "Did you hear the rumor about butter? Well, I'm not going to spread it!", "Did you hear about the restaurant on the moon? Great food, but no atmosphere!", "How many apples grow on a tree? All of them!", "What concert costs just 45 cents? 50 Cent featuring Nickelback!", "What rock group has four men that don't sing? Mount Rushmore." ]; exports.handler = async (event) => { // calculates a random integer from 0 to 9 and returns the position from the array let joke = jokes[Math.floor(Math.random()*10)]; return joke; };
This code is also available on my GitHub page.
Test your Function
- After you’ve added the code above, click the Deploy button to save your changes.
- Click the Test button
- In this step, we’ll configure a test event.
Verify the hello-world Event template is selected
Enter a Event name
Click the Create button
When the test event is successfully created, you will see the following notification - Verify the test event you created in step three is defaulted in the test event dropdown and click the Test button
- Click the carrot icon next to Details to expand the Execution results sections and click the Test button to execute your function. Each time you click the test button, a different dad joke will be returned by the function.
Summary
In this blog post we created a lambda function in AWS, modified the function and tested it.
Thank you for reading!