2022-08-02

AWS Lambda Overview

What is AWS Lambda

AWS Lambda is a serverless compute service offered by Amazon Web Services (AWS) that enables you to run your code without the need to provision or manage servers. Lambda automatically scales your applications based on the number of incoming events, making it an ideal solution for event-driven architectures and applications with unpredictable workloads.

Lambda supports a variety of runtime environments, including Node.js, Python, Java, Go, and .NET, allowing developers to write functions using their preferred programming language. You can create Lambda functions to handle events from various AWS services, such as Amazon S3, Amazon DynamoDB, Amazon API Gateway, and many more.

By using AWS Lambda, you can focus on writing application logic while AWS takes care of the operational aspects, such as scaling, patching, and monitoring. Additionally, Lambda's pricing model means that you only pay for the compute time your code uses, making it a cost-effective solution for many use cases.

What Lambda Can Do

AWS Lambda offers a wide range of capabilities to help you build and scale applications. Some key features and use cases include:

  • Event-driven processing
    Lambda can automatically execute your code in response to events from various AWS services, such as changes to data in an Amazon S3 bucket, updates to a DynamoDB table, or messages in an Amazon SQS queue.

  • Scalability
    Lambda automatically scales your applications by managing the number of instances required to handle incoming events. This means your applications can efficiently handle spikes in traffic without manual intervention.

  • Microservices and APIs
    You can build microservices using Lambda functions, and then expose them as APIs using Amazon API Gateway. This enables you to create scalable and cost-effective RESTful APIs that automatically scale with the number of requests.

  • Data processing
    Lambda functions can be used for real-time and batch data processing. For example, you can use Lambda to process and analyze log files, transform data, or perform real-time data processing for streaming data sources like Amazon Kinesis or Apache Kafka.

  • Integration with AWS services
    Lambda integrates seamlessly with other AWS services, making it easier to build end-to-end applications without managing servers. You can use Lambda to trigger code execution based on events from services like Amazon S3, Amazon DynamoDB, Amazon API Gateway, and more.

  • Custom runtime environments
    AWS Lambda supports a variety of runtime environments, including Node.js, Python, Java, Go, and .NET. Additionally, you can create custom runtimes to use other languages or specific versions of your preferred language.

  • Cost optimization
    With AWS Lambda, you only pay for the compute time used to execute your code. This means you're not paying for idle server resources, which can lead to cost savings for many applications.

By leveraging AWS Lambda, developers can focus on writing and deploying application code while AWS handles operational aspects such as scaling, patching, and monitoring, simplifying the development process and reducing time-to-market.

Constraints of Lambda

While AWS Lambda offers several benefits, it also has some limitations and constraints that you need to consider when designing and implementing your serverless applications:

  • Execution timeout
    The maximum execution time for a Lambda function is 15 minutes. If your function requires longer processing times, you may need to break it into smaller tasks or consider other compute options like Amazon EC2 or AWS Fargate.

  • Function memory and CPU power
    Lambda functions can be allocated between 128 MB and 10,240 MB of memory. The CPU power, network bandwidth, and disk I/O are proportionally allocated based on the memory configuration. If your application requires more resources, you may need to consider other AWS compute services.

  • Package size limits
    The deployment package size for a Lambda function has a limit of 50 MB (zipped) and 250 MB (unzipped). If your function has large dependencies or libraries, you may need to use a container image or optimize your dependencies.

  • Concurrent executions
    AWS Lambda has a default soft limit of 1,000 concurrent executions per region. This limit can be increased by requesting a limit increase from AWS Support. However, it is important to consider the impact of increased concurrency on the downstream services your Lambda function interacts with, as they may have their own limitations.

  • Cold start latency
    When a Lambda function is invoked for the first time or after a period of inactivity, there may be an additional latency called a "cold start." This occurs because AWS has to provision a new container to execute the function. To minimize cold start latency, you can use provisioned concurrency or keep the function "warm" by periodically invoking it.

  • Stateless execution
    Lambda functions are stateless, meaning they do not maintain their state between invocations. If your application requires state management, you may need to store state data in an external data store, such as Amazon DynamoDB or Amazon S3.

  • Limited GPU support
    Lambda functions do not support GPU-based processing. If your use case requires GPU resources, consider using Amazon EC2 instances with GPU support or Amazon SageMaker for machine learning workloads.

Understanding the constraints of AWS Lambda is essential for building serverless applications that perform well and scale efficiently. By being aware of these limitations, you can design your applications to work within these constraints or choose alternative AWS services when Lambda is not the best fit for your specific use case.

AWS Lambda pricing

AWS Lambda pricing is based on the number of requests and the duration of compute time your functions consume. This pay-as-you-go model allows you to optimize costs by paying only for the resources used by your Lambda functions.

  • Requests
    You are billed for the total number of requests made to your Lambda functions. The price is $0.20 per 1 million requests after the free tier.

  • Duration
    You are charged for the compute time your Lambda function takes to execute, measured in increments of 100 milliseconds. The cost depends on the amount of memory allocated to your function. As an example, the price for 128 MB of memory is $0.0000002083 per 100 ms.

AWS also offers a generous free tier for Lambda users, which includes:

  • 1 million free requests per month
  • 400,000 GB-seconds of compute time per month

It's important to note that data transfer costs and any additional charges associated with other AWS services used in conjunction with Lambda, such as Amazon S3 or Amazon DynamoDB, are billed separately.

For a more detailed breakdown of AWS Lambda pricing and to calculate costs based on your specific requirements, visit the AWS Lambda Pricing page.

https://aws.amazon.com/lambda/pricing/

How to Create Lambda Function

You can create AWS Lambda functions using two methods: packaging your code and dependencies in a ZIP file, or using a container image. Here's a step-by-step guide for both approaches:

ZIP file

  1. Prepare your code

Write your Lambda function using a supported runtime, such as Node.js, Python, Java, Go, or .NET. Make sure to include any required dependencies in the same directory as your code.

  1. Package your code and dependencies

Create a ZIP archive containing your code and dependencies. Ensure that your function's entry point (e.g., handler for Node.js or lambda_function for Python) is at the root level of the archive.

  1. Upload the ZIP file to AWS Lambda
  • Using AWS Management Console: Navigate to the Lambda service in the AWS Management Console, click Create function, choose Author from scratch, and provide the necessary details such as function name, runtime, and execution role. In the Function code section, choose Upload a .zip file and upload your ZIP archive.

  • Using AWS CLI: You can also use the AWS CLI to create a Lambda function by running the following command:

bash
$ aws lambda create-function \
   --function-name YourFunctionName \
   --runtime YourRuntime \
   --role YourExecutionRoleARN \
   --handler YourEntryPoint \
   --zip-file fileb://your-zipped-code.zip
  1. Configure triggers (optional)

Set up event sources to trigger your Lambda function, such as Amazon S3, Amazon DynamoDB, or Amazon API Gateway.

  1. Test your Lambda function

Use the AWS Management Console, AWS CLI, or SDKs to invoke and test your Lambda function.

Container Image

  1. Prepare your code

Write your Lambda function using a supported runtime or custom runtime. Include any required dependencies in the same directory as your code.

  1. Create a Dockerfile

Create a Dockerfile in your project directory to define your container image. Use a base image provided by AWS or create your own base image. Ensure that your function's entry point is specified correctly in the Dockerfile.

  1. Build the container image

Run the following command in your project directory to build the container image:

bash
$ docker build -t your-image-name .
  1. Push the image to Amazon Elastic Container Registry (ECR)

Create a new repository in Amazon ECR and authenticate your Docker client to your ECR registry. Push your container image to the ECR registry with the following command:

bash
$ docker push your-ecr-repository:your-image-tag
  1. Create a Lambda function with the container image
  • Using AWS Management Console: Navigate to the Lambda service in the AWS Management Console, click Create function, choose Container image, and provide the necessary details such as function name and execution role. In the Container image URI field, enter the ECR image URI.

  • Using AWS CLI: You can also use the AWS CLI to create a Lambda function with a container image by running the following command:

bash
$ aws lambda create-function \
   --function-name YourFunctionName \
   --role YourExecutionRoleARN \
   --package-type Image \
   --code ImageUri=your-ecr-repository:your-image-tag
  1. Configure triggers (optional)

Set up event sources to trigger your Lambda function, such as Amazon S3, Amazon DynamoDB, or Amazon API Gateway.

  1. Test your Lambda function

Use the AWS Management Console, AWS CLI, or SDKs to invoke and test your Lambda function.

References

https://aws.amazon.com/lambda/

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!