AWS Lambda Getting Started: First Steps

published on 01 March 2024

Getting started with AWS Lambda is a straightforward process ideal for beginners looking to run code without managing servers. This guide covers the essentials:

  • Creating an AWS account and understanding AWS IAM basics for proper permissions.
  • Making a simple 'Hello World' Lambda function using the AWS Management Console.
  • Customizing your function to calculate the area of a rectangle.
  • Testing your Lambda function with sample data to ensure it works as expected.
  • Viewing test results and logs in CloudWatch for performance insights.
  • Cleaning up resources post-experimentation to avoid unnecessary charges.

By the end of this guide, you'll have gained the knowledge to create, customize, test, and manage basic AWS Lambda functions, setting a solid foundation for more complex projects.

Sign up for an AWS account

If you don't have an AWS account yet, you'll need to create one. Just go to sign up. New accounts can use some AWS services for free, up to certain limits.

Learn the basics of AWS IAM

AWS IAM is a tool that lets you control who can do what with your AWS resources. For making Lambda functions, your user account needs the right permissions. You can read up on IAM basics in the IAM documentation.

Get administrative console access

To follow this guide, you'll need the ability to create Lambda functions and roles in the AWS Management Console. An admin can give you these permissions through IAM policies.

Create a basic Lambda function

Let's walk through making a simple "Hello World" Lambda function step by step. This is a basic example to help you get started with AWS Lambda.

1. Go to the Lambda service

  • First, in the AWS Management Console, find and click on Lambda. This takes you to the Lambda section.

2. Click "Create function"

  • Once you're on the Lambda Functions page, look for a button that says Create function and click it. This is how you begin making a new function.

3. Configure function settings

  • You'll see a spot to type in a Function name. Put something simple like my-hello-function there.
  • For Runtime, pick Node.js 16.x.
  • Choose x86_64 for the Architecture.
  • Then, hit Create function.

4. Review default code

  • Lambda starts you off with some basic code that says "Hello from Lambda!".
  • There's a Handler field that tells you which file and function Lambda will call, like index.handler.

5. Replace code

  • Erase the code that's there and put in this instead:
exports.handler = async () => {
  return "Hello World!"  
};
  • This new code will say "Hello World!" when it runs.

6. Deploy the code

  • Click Deploy to send your code to Lambda. Now it's ready to run.

And that's it! You've just made a simple Lambda function. You can now test it out and add more to it if you want.

Customize the function code

Now that you've got your basic "Hello World" Lambda function up and running, let's make it do something a bit more useful. We're going to change the function so it can figure out the area of a rectangle using the length and width you give it.

Define the handler method

First, let's swap out the current handler code for this new one:

exports.handler = async (event) => {

  // Get length and width from what you input
  const length = event.length;
  const width = event.width;
  
  // Use length and width to find the area
  const area = length * width;

  // Give back the area value
  return {
    area: area
  };

};

Here's what this new code does:

  • It looks for length and width in the input (event) you give it
  • It multiplies length and width to find the area
  • It sends back the area as a result

Handle input parameters

When you run this function, Lambda sends it an event, which is just a fancy way of saying a bunch of information. This code grabs the length and width info from that event.

For instance, if the event looks like this:

{
  "length": 5,
  "width": 2 
}

Then, inside the handler, length will be 5 and width will be 2.

The code uses a cool shortcut from ES6 (a version of JavaScript) to grab these values easily.

Return area result

To send something back from a Lambda handler, you just use return with what you want to send. Here, we're sending back a simple object that says how much area we've calculated.

Testing the function will show you how Lambda uses what you return to display your function's result.

And that's pretty much it for making a Lambda function do something more specific! This is a basic way to start making functions that do all sorts of useful things with the info they get.

Test the Lambda function

To see if your Lambda function works, you'll need to set up a simple test. This involves giving it some example data to work with. It's like a practice run to make sure everything goes smoothly.

Create test event

Here's a sample set of data you can use to test your function. It has measurements for length and width:

{
  "length": 6,
  "width": 7
}

Feel free to change these numbers to try out different sizes.

Test function

To run the test:

  • Head over to the AWS Lambda section in the AWS Management Console.
  • Find your function and click Test. Pick the test event you just set up.
  • Click Test one more time.

This tells your function to run using the test data. Shortly after, you'll see the result.

If you used the sample data above, your function should tell you the area is:

{
  "area": 42 
}

Check logs

To look at the details of your test:

  • Visit the CloudWatch Logs console.
  • Click on the log group that matches your Lambda function's name (like /aws/lambda/myLambdaFunction).
  • Open the most recent log to see detailed info about your test run.

These logs are handy for understanding more about how your function behaves and troubleshooting if needed.

sbb-itb-6210c22

Clean up resources

After you're done playing around with your example Lambda function, it's a good idea to delete everything you set up. This way, you won't keep getting charged for stuff you're not using. Here's how to get rid of the function, its logs, and the role that let it do its thing:

Delete the Lambda function

  • Head to the Functions page on the Lambda console.
  • Click on the function you made.
  • Hit Actions, and then Delete.
  • A box will pop up asking if you're sure. Type delete and click Delete.

Delete the log group

  • Jump over to the Log groups page in the CloudWatch console.
  • Find the log group for your function (it'll look something like /aws/lambda/my-function).
  • Click Actions, then Delete log group(s).
  • Say yes to the delete confirmation.

Delete the execution role

  • Go to the Roles page in the IAM console.
  • Find the role you made for your function.
  • Press Delete.
  • You'll need to type in the role name to confirm you really want to get rid of it.

If you're just trying things out, deleting these by hand is totally fine. But remember, if you start using Lambda a lot, you might want to learn how to clean up automatically with tools like AWS CloudFormation or the AWS CLI. Either way, make sure to delete what you don't need to keep your AWS bill in check.

Conclusion

Great job on setting up your first AWS Lambda function! This guide aimed to make it easy for beginners to get started with AWS Lambda, and by now, you should know how to:

  • Make a simple Lambda function using the AWS console
  • Change the function's code to do what you need
  • Use test data to check if your function works
  • Look at the results and logs to see how your function performed

Now that you've got the basics down, you're ready to tackle more complicated projects with Lambda. Here are a few ideas for what to do next:

  • Figure out how to include extra code or libraries with your Lambda function
  • Set up your functions to run automatically at certain times with CloudWatch Events
  • Try building a website without servers using Lambda and other AWS tools
  • Dive into more detailed Lambda guides and tutorials to learn even more

Remember to clean up any Lambda functions and resources you're not using to keep your AWS costs down. If you ever get stuck or need ideas, the AWS documentation and community are great resources. And most importantly, enjoy creating and experimenting with your new serverless skills!

How do you start a Lambda?

Lambda

To get going with AWS Lambda, here's a simple guide:

  • Sign into your AWS account and head over to the Lambda section.
  • Look for and click on "Create function".
  • Name your function and pick the type of code it will run (like Node.js or Python).
  • You can either write your code right there or upload a file with your code.
  • Set up a test to see if it works and decide what will make your function run.
  • Hit "Save" to make your function live.

Basically, you create a Lambda function by setting it up in the AWS console, adding your code, and letting AWS take care of running it when needed.

How do I launch AWS Lambda?

You can start an AWS Lambda function in several ways:

  • Directly through the AWS Lambda console
  • By sending a request to the function's web address
  • Using the AWS command-line tool
  • From within an app using AWS's software for developers
  • By setting up other AWS services to make the function run automatically

For instance, an app on your phone could use AWS's tools for developers to run a function, or you could have a function start whenever you add files to a storage service on AWS.

How do I write my first AWS Lambda function?

Here's how to make a simple Lambda function that says "Hello World":

  • Go to the Lambda part of the AWS console and click Create function.
  • Choose Author from scratch.
  • Name your function something like myFirstFunction.
  • Pick a programming language version, like Node.js 14.x.
  • Use this code:
exports.handler = async () => {
  return "Hello World!";
};
  • Click Save.

Now you have a Lambda function that will say "Hello World!" when you run it. You can test it and then add more to it as you learn.

How do you start a step function from Lambda?

To make a Lambda function start a workflow with AWS Step Functions:

  1. Make sure the Lambda function has the right setup to call the workflow.
  2. The function needs permission to start workflows, so set that up.
  3. In your function's code, use the command to start a workflow.
  4. You can send data to the workflow if it needs it.
  5. Your function will get a response back when it starts the workflow, but it doesn't have to wait around for the workflow to finish.

This way, you can kick off a complex set of tasks with just a Lambda function.

Related posts

Read more