AWS Lambda

Deep Dive

Documentation

Hi there, I’m Sanjay 👋!

LinkedIn Badge Website Badge Twitter Badge Github Badge Stack Overflow

About me

  • 🔭 Working: Lead Software Engineer - working on Spring Boot, Reactive Programming, Microservices, Kafka, Cassandra, Kubernetes, AWS.
  • 🖥️ Interests: I love building cool Software & Systems, Self-Hosting, Gaming
  • 🌱 Learning: Go | Rust | Scala | Design Patterns
  • 💬 Ask me about: Java | Reactive Spring | Containers | AWS
  • 🧑‍🤝‍🧑 Collaboration: Looking to collaborate on several projects over here, check out my GitHub

Languages, Frameworks and Platforms

Java Spring Kotlin Project Reactor Kafka Cassandra AWS Kubernetes

What is AWS Lambda Function

  • A Compute Service to run code without Provisioning or Managing Servers
  • Uses Containerization tech called Firecracker
  • Handles Server & OS Maintenance, Provisioning, Auto-Scaling, Logging
  • Code in one of the Languages supported by Lambda Runtimes

Lambda Concepts

  • Function: a resource invoked to run your code
  • Trigger: a resource/config that invokes a Lambda Function
  • Event: a JSON-formatted data for a Lambda Function to Process
  • Event Source Mapping: a resource that reads items from a Stream/Queue & invokes a function
  • Execution Env: provides a secure and isolated Runtime Environment for your Lambda function
  • Runtime: provides a lang-specific Environment that runs in an Execution Environment

What is Lambda Runtimes

  • Runtime provides a lang-specific env that runs in an execution env
  • It relays invocation Events, Context, & Responses between Lambda & the Function
  • Use AWS provided Lambda Runtimes or Build your own by implementing Runtime API

Execution Environment & Runtime

Lambda Execution Environment Lambda Execution Environment

Supported Lambda Runtimes

Node.js v18, v16, v14 & v12*
Python v3.9, v3.8, v3.7
Java v8, v11
C# .NET v5, v6 & .NET Core v3.1
Go v1.x
Ruby v2.7*
Custom Runtime - Based on Amazon Linux

Micronaut Custom Runtime Implementation Example

Lambda Function Java

public class MsgHandler implements RequestHandler<String, String> {
    public String handleRequest(String input, Context ctx) {
        context.getLogger().log("Input: " + input);
        return "Hello World - " + input;
    }
}
public class StreamHandler implements RequestStreamHandler {
    public void handleRequest(InputStream is, OutputStream os, Context ctx) {
        String input = IOUtils.toString(is, "UTF-8");
        os.write(("Hello World - " + input).getBytes());
    }
}
public class NoInterfaceHandler {
    public String customHandle(String input, Context context) {
        context.getLogger().log("Input: " + input);
        return "Hello World - " + input;
    }
}

Features

  • Automatically manages the underlying Compute Resources
  • Concurrency and Scaling controls
  • Pay Per Request Execution is millis
  • Multiple Event Sources: API-GW, SQS, S3, DynamoDB, etc.
  • Functions defined as Container Images
  • Lambda Extensions
  • Execute inside Private Subnets inside a VPC
  • File Systems Access + EFS

Limitations

  • Memory: 128 MB to 10,240 MB
  • No Direct Way to assign CPU: 1,769 MB memory is equivalent of one vCPU
  • Function Timeouts: 15 minutes
  • Invocation Payload: 6 MB (sync) & 256 KB (async)
  • File descriptors: 1024
  • Execution processes/threads: 1024
  • No Async Processing Allowed: Lambda Instance Freezes once invocation is completed
  • 1 Lambda Instance serves only 1 request at time

Other Compute Options

Services Comparison with Lambda
AWS EC2 Not Serverless, More RAM/CPU/Storage, Always ON
AWS EKS Managed Container Platform on EC2
Fargate Serverless Container Platform on EC2
Beanstalk AWS Container based PaaS
GCP Cloud RUN Based on OSS Knative - Charged for Runtime only

Questions?