Lambda

Serverless

  • Serverless is a new paradigm in which developers dont have to manage servers anymore. Developers need not provision them, AWS will take care of it behind the scene.

  • They just deploy code and functions.

  • Initially they were called Function As A Service (FaaS).

  • Serverless was pioneered by AWS Lambda. However, it now includes database, messaging, storage etc.

  • Serverless services include,

    • Lambda

    • DynamoDB

    • Cognito

    • API Gateway

    • S3

    • SNS and SQS

    • Kinesis Data Firehose

    • Aurora

    • Step Functions

    • Fargate

Icon

Lambda Icon

About

  • AWS Lambda are virtual functions (i.e no servers to manage) whose executions are limited by the time (i.e., short executions upto 15 minutes).

  • They run on-demand.

  • Scaling is automated.

  • Integrated with so many services and easy monitoring with AWS Cloud Watch.

  • Can get upto 10 GB of RAM per functions.

  • Supports many languages such as,

    • Node.js

    • Java

    • Javascript

    • Python

    • C#

    • Powershell

  • Custom Runtime API for Golang and Rust.

  • Supports Container through Lambda Container Image, where container must implement Lamda Runtime API.

Event Object

  • Event Object represents event that triggered invocation of lambda.

  • This object will have details about,

    • Source of event

    • Region from which event was send

    • Time of event

    • Account details

    • Resources ARN and so on

  • Its in json form.

  • Its passed in runtime to lambda function.

Context Object

  • This involves details about runtime, lambda function's meta-data.

  • Will have details such as,

    • Function name

    • Memory limit

    • client context

    • function verson and so on

  • Its in json form.

  • This is passed to lambda function at runtime.

Lambda execution context

  • Its an execution context, which is a temporary runtime environment that initializes any external dependencies of your lambda code.

  • Useful for establishing Database connection, HTTP calls, SDK client.

  • The execution context once created will be maintained for some time in anticipation of another lambda function execution i.e., next function invocation can reuse the context and save time in initializing connection objects.

  • One can use /tmp directory (ephemeral storage) which will retain files across execution. Upto 10 GB of disk space is available. Though for permanent storage such as S3/EFS is recommended for permanent data.

  • Also to encrypt data in /tmp files, generate KMS Data keys and use them to encrypt them.

  • So these execution context which need to reused should be written outside of lambda handler code.

Examples

  • One can use CloudWatch Events EventBridge can be used to trigger AWS Lambda every 1 hour, creating serverless cron jobs.

Pricing

  • Pay per calls

    • First 1 million requests are free.

    • Thereafter $0.20 per million requests.

  • Pay per duration

    • 400,000 GB/s of compute time per month for free i.e., if function is having 1GB of RAM then one will get 400,000 seconds.

    • Thereafter it costs $1.00 for every 600,000 GB-seconds.

Lambda Invocations

  • Lambda invocations can be done synchronously or asynchronously.

  • CLI, SDK, API Gateway, ALB, CloudFront, S3 Batch and et. al invoke Lambda synchronously and expect result.

  • For such invocations, retries must happen at client side.

  • Asynchronous invocations can be done using service such as S3, Event-Bridge and so on.

Lambda Limits

Execution

  • Memory allocation: 128 MB - 10 GB (1 MB increments).

  • Maximum exeuction times: 900 seconds.

  • Environment variables: 4KB

  • Disk capacity in the function container (/tmp): 512 MB to 10 GB.

  • Concurrency executions: 1000 (can be increased)

Deployment

  • Lambda function deployment size (compressed .zip): 50 MB

  • Size of uncompressed deployment (code + dependencies): 250 MB

  • Can use /tmp directory to load other files at startup.

  • Size of environment variables: 4 KB.

Last updated