DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Processing Cloud Data With DuckDB And AWS S3
  • Process Mining Key Elements
  • Generic and Dynamic API: MuleSoft
  • LLMops: The Future of AI Model Management

Trending

  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • How Trustworthy Is Big Data?
  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Five Minute Cloud Lambda Function

Five Minute Cloud Lambda Function

Building a serverless function is easy. AWS calls their serverless functions Lambdas. Let’s build on a serverless function with Python on AWS.

By 
Eric Goebelbecker user avatar
Eric Goebelbecker
DZone Core CORE ·
Sep. 14, 22 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
5.5K Views

Join the DZone community and get the full member experience.

Join For Free

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 a serverless function is easy. Let’s build on a serverless function with Python on AWS.

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.

Create an AWS account (screenshot)

Log in 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.

AWS Console

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

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

Find Lambda

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:

List of Lambdas

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.

Lambda Create

Here are your choices:

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

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

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

Click Advanced settings.

Lambda Advanced

Check the box for Enable function URL. This gives you function 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:

Lambda Info

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:

Lambda Hello

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.

Postman Hello

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.

Switch your request type to POST and give your request a body.

Postman Post

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

Here’s the code:

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.

Deploy code

Now, send the new request.

Success

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 see what else they can do.

API AWS Data structure IT JSON Serverless computing Cloud Console (video game CLI) Data (computing) Event

Published at DZone with permission of Eric Goebelbecker, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Processing Cloud Data With DuckDB And AWS S3
  • Process Mining Key Elements
  • Generic and Dynamic API: MuleSoft
  • LLMops: The Future of AI Model Management

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!