Click here to Skip to main content
15,880,967 members
Articles / Hosted Services / Serverless

Five Minute Cloud Lamdba Function

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Oct 2022Public Domain3 min read 3.1K   3  
Create an AWS Lambda in five minutes
A quick tutorial on creating an AWS Lambda.

Introduction

Serverless computing is pretty cool.

You need something done in the cloud. It might be storing a GPS location. Or pulling some from a DB based on a few query parameters.

But building a complete server instance would be overkill. You just need an API endpoint to accept a query and spit back a result.

You need a serverless function.

Building serverless function is easy. Let's create one in AWS in five minutes.

AWS calls their serverless functions Lambdas, so I'll be using those terms interchangeably throughout this tutorial.

AWS Account

First, you need an AWS account. If you don't have one, don't start your timer yet. Creating the account may be harder than creating a Lambda.

Start here and follow the instructions.

Image 1

Login to the AWS Console

If you're up and running with an AWS account, you can go right to the console.

On my console, Lambda appears near the top because I recently used it.

Image 2

But, you can click on View all services* to find it if you need to.

It'll be right near the top, under Compute.

Image 3

Click on Lambda, and let's create a service.

New Lambda

This will take you to your list of Lambdas.

Here's what mine looks like:

Image 4

I've got an old function from an Alexa skill hanging around. If you've never created a Lambda before, you'll see an empty list.

Click the Create function button.

Now, it's finally time to define a function.

Image 5

Here are your choices:

  • Select Author from scratch
  • Give your function a name.
  • Pick Python 3.9 from the Runtime drop down.

So, this will give us a single Python function. AWS will call it when a web client calls our Lambda.

But, we need to set up one more thing to make the function callable from standard web clients.

So, click Advanced settings.

Image 6

Check the box for Enable function URL. This gives you function of a web address.

Next, select NONE for Auth type.

As the warning says, this is not a secure configuration, and you shouldn't use it with functions that access secure data, or can be used as a vector for attacking other services. Since this will only be a demo app, it's fine for now.

Click Create function and we'll look at our creation and take it out for a test drive...

Here's what your function info page should look like:

Image 7

Test Drive

On the right hand side, you can see your function's public URL. Click it.

This sends a GET request to your function from the browser:

Image 8

Technically, we could stop the clock here. You've created a serverless function.

But let's take it one step further and make it process a more complicated request.

Postman

I'm going to use Postman for this next part. If you don't have an account yet, go ahead and create it. Everything we'll do here works with a free login.

Let's point Postman at the new function:

Create a new request. Paste your function URL in the URL section and click Send.

Image 9

Postman displays the result at the bottom. So, we've used Postman to make the same request as the browser.

Now let's have some fun.

Posting JSON Data to a Lambda Function

If we want to make our function do something interesting, we need to send it some data.

So, switch your request type to POST and give your request a body.

Image 10

Now, if we send this, nothing will change. We need to make some code changes.

Here's the code:

Python
import json

def lambda_handler(event, context):

    request = event['body']
    request_obj = json.loads(request)

    return {
        'statusCode': 200,
        'headers': { 'Content-Type': 'application/json' },
        'body': "{}, {} {}!".format(request_obj['Greeting'], 
         request_obj['Title'], request_obj['Name'])
    }

When you call a Lambda from a function URL, the request data is included in the event object as the body field. Since it's a raw string, we need to pull it out of the event and convert it to an object with json.loads.

Then we can access the fields and use them to build a new string.

Paste this code in and Deploy it.

Image 11

Now, send the new request.

5343508/9c473af3-3ded-42d8-8385-9a0a159af2ad.Png

It works! You built a Lambda.

So, you can see that serverless functions in AWS in five minutes are easy to build.

In the next tutorial, we'll see what else they can do.

History

  • 2nd October, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --