AWS Lambda Introduction

AWS Lambda Introduction

Introduction to AWS Lambda

Overview of AWS Lambda

  • Shanti introduces the topic of AWS Lambda, explaining that the session will cover its definition and related terminology.
  • The traditional method of running code involves setting up an EC2 instance, where a server runs continuously regardless of traffic, leading to constant costs.

How AWS Lambda Works

  • In contrast to EC2, AWS Lambda allows users to upload their code without managing servers; it automatically finds a machine to run the code when triggered by an event.
  • Users only pay for the execution time of their code rather than for idle server time.

Serverless Concept Explained

  • The term "serverless" can be misleading; it does not mean there are no servers involved but indicates that users do not manage them.
  • A service is considered serverless if it scales automatically and users only pay for actual usage.

Differences Between Traditional Hosting and AWS Lambda

Application Execution

  • Traditional applications on EC2 run continuously, while Lambda functions execute only in response to specific events.
  • Each event triggers a separate execution environment in parallel, allowing multiple instances without manual scaling.

Limitations of AWS Lambda

  • There is a maximum execution time limit of 15 minutes per function call in AWS Lambda.
  • State management is unreliable since each invocation may occur on different machines with no shared memory between executions.

Understanding Execution Environments

Warm vs. Cold Starts

  • Two types of starts exist: cold starts (creating a new environment for each event with potential latency), and warm starts (reusing existing environments).

Event Sources and Invocation Flow

  • Various sources can trigger events for Lambda functions, such as S3 uploads or Spring Boot applications invoking the service.

Lifecycle of an Execution Environment

Phases Explained

  • The execution environment acts as an isolated runtime space managed by AWS that includes necessary components like JVM and application dependencies.

Initialization Phase Details

  • During initialization, static fields are set once per environment creation; object constructors are also invoked at this stage.

Handling Multiple Events

Business Logic Execution

  • The handle request method containing business logic executes every time an event occurs while static fields remain initialized throughout the lifecycle.

Instance Variables Caution

  • Developers must be cautious with instance variables since they persist across invocations within the same execution environment.

Concurrency Management in AWS Lambda

Concurrency Limits

  • Default concurrency limits allow up to 1000 concurrent executions per region; exceeding this results in throttling responses (HTTP status 429).

Asynchronous vs. Synchronous Invocations

Invocation Types

  • Synchronous calls require waiting for completion before proceeding, while asynchronous calls do not wait for responses from Lambdas.

Internal Queue Mechanism

Asynchronous requests are queued internally by the lambda service which retries failed executions up to six hours before discarding or moving them to a dead letter queue.

Event Source Mapping with SQS

Pull Model Explanation

  • Involves polling from sources like SQS queues where messages are processed based on availability rather than being pushed directly.

Understanding Event Source Mapping in AWS Lambda

Introduction to Event Source Mapping

  • Event source mapping is a mechanism used in AWS Lambda to connect services like SQS with Lambda functions, allowing for automatic event processing.
  • The event source mapping continuously polls the SQS queue for messages, passing them to the Lambda service for execution. Successful executions result in message deletion from the queue.

Polling Mechanism and Message Handling

  • The Lambda service utilizes event source mapping to poll continuously, ensuring that events are processed efficiently without loss.
  • If there are more messages than can be processed at once, they remain in the SQS queue until they can be handled by the Lambda function.

Concurrency Management

  • Concurrency limits in AWS Lambda dictate how many instances of a function can run simultaneously; exceeding this limit causes other functions to be throttled.
  • When multiple events require different functions but one function reaches its concurrency limit, other functions may not execute until capacity is available.

Reserved Concurrency Concept

  • Reserved concurrency allows you to allocate a specific number of concurrent executions for each function, preventing resource contention among multiple functions.
  • For example, if Function A has a reserved concurrency of 50, it will only process up to 50 requests even if more are queued.

Unreserved Concurrency and Function Invocation

  • Any unreserved concurrency (e.g., 100 remaining from a total of 1000) can be utilized by new or different functions when their reserved limits have been reached.
  • Functions without reserved concurrency must rely on unreserved capacity when invoked; otherwise, they cannot execute until resources become available.

Provisioned Concurrency Explained

  • Provisioned concurrency pre-warms execution environments to reduce latency during initialization phases when handling requests.
  • This setup incurs costs regardless of usage since you're paying for provisioned environments whether they're actively processing requests or not.

Event Source Identification and Function Invocation

  • AWS Lambda determines which function to invoke based on metadata included with each event rather than guessing based on content alone.
  • For instance, an S3 bucket notification specifies which function should handle an uploaded file event directly within its configuration.

Summary of Key Concepts

  • Important concepts discussed include cold starts vs. warm starts, synchronous vs. asynchronous invocation methods, and various event sources such as S3 and SNS.
  • Additional topics like lambda permissions and versions will be covered during practical setups for better understanding.

Turn any video into a summary like this

YouTube links, meetings, lectures. With transcripts, search, and chat.

Video description

Join this channel to get access to perks: https://www.youtube.com/channel/UCDJ2HAZ_hW-DMJj_U0zN38w/join Chapters: 00:00 - What is AWS Lambda 09:23 - High level flow of AWS Lambda 14:57 - What is Execution Environment and its lifecycle 27:31 - Scenario 1, event comes 1 after another 28:45 - Scenario 2, multiple events in parallel 29:26 - Concurrency, Synchronous, Asynchronous, Push, Poll 39:50 - Reserved Concurrency 44:30 - Provisioned Concurrency 46:41 - Scenario 3, how Lambda determines which function to invoke