AWS Lambda
HTTP request
serverless
cloud computing
API Gateway

Invoke a AWS Lambda function by a http request

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

AWS Lambda is a serverless compute service designed to execute code in response to events and automatically manage the underlying compute resources. One common use case is invoking a Lambda function via an HTTP request, which can be done through Amazon API Gateway—a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs.

This article walks you through the technical details involved in invoking a Lambda function by HTTP request, discussing common configurations and providing complete examples.

Understanding Lambda Functions and API Gateway

AWS Lambda

AWS Lambda allows you to run code without provisioning or managing servers. You only need to upload your code and Lambda takes care of everything required to run and scale it with high availability. You can set up your Lambda functions to run in response to various triggers, including HTTP requests.

Amazon API Gateway

API Gateway acts as the "front door" for applications to access data, business logic, or functionality from your back-end services, such as applications running on AWS Lambda, another AWS service, or any web application. It supports creating RESTful and WebSocket APIs that enable real-time two-way communication.

Workflow for Invoking Lambda via HTTP

The process of invoking a Lambda function via HTTP typically involves these major steps:

  1. Create a Lambda Function: Write and deploy your function code in the AWS Lambda environment.
  2. Define an API Gateway Resource: Setup an API Gateway to handle HTTP requests and route them to your Lambda function.
  3. Configure Methods and Mapping: Determine the HTTP methods (e.g., GET, POST) your API will handle and set up any necessary request/response mapping.
  4. Deploy the API: Publish the API to make it accessible via a public HTTP endpoint.
  5. Test and Monitor: Ensure the API and Lambda function work as intended and monitor their performance.

Step-by-Step Implementation

Step 1: Creating a Lambda Function

  1. Navigate to AWS Lambda in the AWS Console.
  2. Click "Create function".
  3. Enter a name for the function, select "Runtime" (e.g., Python, Node.js, Java), and choose "Create function".
  4. Author your function code: Include necessary libraries and logic for your application. For example, a simple Python Lambda handler might look like this:

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.