Giter VIP home page Giter VIP logo

aws-lambda's Introduction

AWS Lambda

This is the note for AWS Lambda. The content was from Udemy course AWS Lambda - A Practical Guide from Daniel who has a Youtube channel Be A Better Dev

Presentation from Daniel can be found below: https://docs.google.com/presentation/d/1UBgzd0WdBlKygAuTEEhFTMrRiwTKD5ebZvcaYzL68BE/edit#slide=id.g35f391192_04

Pricing

image

Tips to save cost:

  • Start with low memory settings
  • Once collecting enough invocation history, use the Compute Optimizer Tool to optimze memory provision and running time of function
  • 1 Million free requests per month and 400,000 GB Seconds
  • Use lambda cost calculator

Decomposing a Function Execution:

  • Steps:
    • Code download:
      • Depending on programming language and whether we run that lambda or not
    • Start execution environment
    • Execute init code: It is everything outside Lambda's handler function
      • Try to specific when import environment
    • Execute handler code

image

  • Strategies to minimize cold start
    • Minimize number of library dependencies
    • Only import what you need
    • Raise memory configuration: It is not only increasing memory but also computer nodes behind the scene: It can be an expensive option
    • Utilize provisioned concurrency: Keep Lambda always in the warm state by having some lambda containers behind the scene. It is an expensive option

Concurrency and Throttling

Lambda Concurrency:

  • Number of requests being served at a given moment
  • New containers are spawned for each concurrent request
  • Concurrency is a major scaling consideration, and can cause applications to fail due to Throttling
  • Default 1000 units of concurrency per AWS account per region.
  • Unreserved, Reserved, and Provisioned

image

image

Lambda Throttling aka RateExceeded

  • Throttling is when Lambda rejects a request
  • Occurs when in flight invocations exceeds available concurrency

Tips:

  • Alarm on throttles for early indicators of issues by Cloudwatch
  • Evaluate your concurrency needs and plan accordingly
  • Have your clients use Exponential Backoff to avoid retry storms
  • Raising Memory Limit can help, but be careful
  • Use provision concurrency to avoid cold starts, but be careful

Configuration

Version:

  • Version is optional.
  • New upload default to $LATEST version. When create a version, $LASTEST is assigned automatically to the new version if the new version does not have any specific name.

image

Aliases

image

Example of Aliases:

  • In the begining, we creat Python version 1, and assign Alias prod to that version:

image

  • Now we have version 2, $LATEST version is automatically moved from version 1 to version 2. Alias is still kept at version 1.

image

-Then we create version 3, again $LATEST version is automatically moved from version 2 to version 3. Version 3's performance is good, thus we decide to use it. So we move prod Alias to version 1 to version 3 to direct all flow to that version.

image

  • Interestingly, we can assign weight to different version/alias

Environment

image

  • AWS environment is stored. We can access it with os.environ
  • We can also add new key, value into the environment

image

  • The below code in Lambda would list all AWS environment's keys and values to dictionary.
import os

def lambda_handler(event, context):
    allAWSEnv = os.environ
    print(f"This is all environment variables: {allAWSEnv}")
    
    # I added a environment variable son_pham
    # Now I access and print that variable out
    sonPham = os.environ["son_pham"]
    print(f"This is the value of environment variable son_pham: {sonPham}")

image

Virtual Private Cloud (VPC)

  • Is isolated and private network within AWS
  • Public abd Private Subnets
  • Critical for security and apps with compliance guidelines

VPC and Lambda

  • Only necessary when the function needs to access resources not accessible over public internet such as RDS or ElasticSearch in private subnet
  • Behind the scenes, Lambda creates Elastic Network Interfaces (ENI)s for each subnet the function is deployed into. This used to be the issue with cold start latency, but not anymore.
  • VPC Endpoints can be used to communicate with some AWS services privately
  • Below is an example that Lambda cannot access directly to internet, but have to go through NAT Gateway in Public Subnet, and NAT Gateway to got Internet Gateway to go out. To connect S3, instead of going out to internet to connect (which cause latency and security issue), we can config VPC for Lambda to connect with S3.

image

Monitoring:

Logging

  • All logs are automatically saved in CloudWatch

image

image

Metric Filters

  • Allow to write patterns (similar to REGEX) to extract metrics from log times
  • Much cheaper than PutMetric
  • More suitable for high traffic application

Cloudwatch Logs Insight

  • Search for patterns across multiple functions at one
  • Pay per use
  • Query similar to SQL

image

Tips

  • Don't be overly verbose with Logging
  • Embed log lines with important ids to make tracing easier
  • Use Cloudwatch Logs Insights for fuzzy searching
  • Set up a log retention policy or archive regularly

Hands-on

Create the below function to the result when having error

def lambda_handler(event, context):
    print("UploadError 1")

Then we go to CloudWatch to setup the metric filters in Log groups after selecting the targetted function.

image

We then define the pattern to capture, and test if the pattern is working

image

Move to the next page to complete the setup for metric filter.

image

Performance Tuning and Observability

We can use an open-source step function AWS Lambda Power Tuning to optimize memory and cost

https://github.com/alexcasalboni/aws-lambda-power-tuning

https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:451282441545:applications~aws-lambda-power-tuning

We use the above tool to optimize the below function

def lambda_handler(event, context):
    fibonacci_of(100)
    
def fibonacci_of(n):
    if n in {0,1}: # Base case
        return n
    return fibonacci_of(n-1) + fibonacci_of(n-2) # Recursive case

The below input is used to run the tuner:

{
  "lambdaARN": "Lambda function arn",
  "powerValues": [
    128,
    256,
    512,
    1024
  ],
  "num": 10,
  "payload": "{}",
  "parallelInvocation": true,
  "strategy": "cost"
}

aws-lambda's People

Contributors

son-n-pham avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.