Keyboard shortcuts

Press โ† or โ†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

๐Ÿ  Back to Blog

Lambda

Introduction

  • Serverless, virtual functions
  • Short executions up to 15 minutes
  • Run on-demand
  • Pay for number of invocations and compute time
  • Works with many programming languages
    • Node.js, python, Java, c#, Go, Powershell, Ruby, and Custom Runtime API (which can run practically any language)
  • You can provision up to 10GB of RAM per function

Lambda Integrations

  • API Gateway
  • Kinesis
  • DynamoDB
  • S3
  • CloudFront
  • CloudWatch Events / EventBridge
  • CloudWatch Logs
  • SNS
  • SQS
  • Cognito

Pricing

  • Pay per call:
    • First 1,000,000 requests are free
    • .20 per 1 million requests after the first million
  • Pay per duration
    • 400,000 GB-seconds of compute time per month for free

Synchronous Invocation

  • When invoking the function from the CLI, SDK, API Gateway, or ALB, the call is synchronous, meaning the result is returned right away
  • Error handling must happen on the client side (retires, exponential backoff, etc.)

Asynchronous Invocation

  • S3, SNS, CloudWatch Events are all processed asynchronously
  • The events are placed in an internal event queue
  • The lambda function will read from the event queue and attempt to process the events
  • Lambda will attempt to retry failures up to 3 times
    • This means that event may be processed multiple times, so make sure the lambda function is idempotent
    • If the function is retried, you will see duplicate entries in CloudWatch Logs
    • You can define a DLQ (dead-letter queue) (SNS or SQS) for failed processing
  • Async invocations allow you to speed up the processing if you donโ€™t need to wait for the result

S3 Event Notifications

  • Run a Lambda function when a event in S3 is detected

Lambda Event Source Mapping

  • Lambda will poll from the sources and be invoked synchronously
    • Kinesis Data Streams
    • SQS or SQS FIFO
    • DynamoDB Streams
  • Two categories of Event Source Mapping:
    • Streams
      • Kinesis or DynamoDB Streams
      • One Lambda invokation per stream shard
      • If you use parallelization, up to 10 batches processed per shard simultaneously
    • Queues
      • Poll SQS using Long Polling

Lambda in VPC

  • By default, Lambda functions are launched outside of your VPC. Therefore, it cannot access resources in your VPC.
  • Lambda can create an Elastic Network Interface inside your VPC
    • You must define the VPC ID, subnets, and security groups
    • Lambda requires the AWSLambdaVPCAccessExecutionRole
  • By default, a Lambda function in your VPC does not have internet access
    • Deploying a Lambda function in a public subnet does not give it internet access
    • Instead, you can deploy the Lambda function in a private subnet and give it internet access via a NAT Gateway / NAT Instance

Lambda Concurrency

  • Concurrency limit up to 1000 concurrent executions
  • each invocation over the concurrency limit will respond with a HTTP 429
  • Cold starts and provisioned concurrency
    • If the init is large, cold start could take a long time. This may cause the first request to have high latency than the rest
    • To resolve the cold start issue, you can use Provisioned concurrency
      • With Provisioned Concurrency, concurrency is allocated before the function is invoked

Lambda Containers

  • Deploy Lambda functions as container images up to 10GB from ECR