Basics of HowAWS Lambda Works

Arda Baysallar
6 min readJan 7, 2023

--

Amazon Web Services (AWS) Lambda is a serverless compute service that runs our code in response to events and automatically manages the underlying compute resources for us. It is a way to run code without having to worry about the infrastructure that it runs on.

Here are the basics of how AWS Lambda works:

  1. You create a function and upload it to AWS Lambda. This function is a piece of code that is triggered by an event, such as an HTTP request or a change in a database.
    To create an AWS Lambda trigger, you can use the AWS Management Console, AWS CLI, or AWS SDKs. Here is a high-level overview of the process:
    * Sign in to the AWS Management Console and navigate to the AWS Lambda service.
    * Click the “Create function” button to create a new function or select an existing function that you want to add a trigger to.
    * On the “Configuration” page, scroll down to the “Designer” section and click the “Add trigger” button.
    * In the “Add trigger” window, select the event source that you want to use as a trigger for your function. There are many options available, such as an S3 bucket, an SNS topic, or a custom application.
    * Configure any additional settings for the trigger, such as the bucket or topic name, the event type, and any filter patterns.
    * Click the “Add” button to add the trigger to your function.
    * Click the “Save” button to save your changes.
    If you prefer to use the AWS CLI or an AWS SDK, you can use the CreateEventSourceMapping API to create a trigger for your function.
  2. You specify the event sources that trigger your function. These could be AWS services such as Amazon S3, Amazon DynamoDB, or a custom application.
    To specify event sources that trigger your AWS Lambda function, you can use the AWS Management Console, AWS CLI, or AWS SDKs. Here is a high-level overview of the process:
    * Sign in to the AWS Management Console and navigate to the AWS Lambda service.
    * Click the name of the function to that you want to add an event source.
    * On the “Configuration” page, scroll down to the “Designer” section and click the “Add trigger” button.
    * In the “Add trigger” window, select the event source that you want to use as a trigger for your function. There are many options available, such as an S3 bucket, an SNS topic, or a custom application.
    * Configure any additional settings for the trigger, such as the bucket or topic name, the event type, and any filter patterns.
    * Click the “Add” button to add the trigger to your function.
    * Click the “Save” button to save your changes.
    If you prefer to use the AWS CLI or an AWS SDK, you can use the CreateEventSourceMapping API to specify event sources for your function.
  3. When the specified event occurs, AWS Lambda executes your function and passes it the event data as input. Technically describing the following process takes place:
    * The event source (e.g., an S3 bucket, an SNS topic, or a custom application) generates an event and sends it to AWS Lambda.
    * AWS Lambda receives the event and determines which function to invoke based on the event source and any configured rules.
    * AWS Lambda prepares the function for execution by creating an execution environment and loading the function code and any dependencies.
    * AWS Lambda executes the function and passes it the event data as input in the form of a JSON object. The function processes the event data and returns a response.
    * If the function returns a response, AWS Lambda sends the response back to the event source or to any other configured destination.
    This process happens automatically and is transparent to the user. AWS Lambda scales the execution of your function based on the incoming event rate and the number of requests, so you don’t have to worry about capacity planning or resource management.
  4. Your function processes the event data and returns a response. This response could be an HTTP response for an API Gateway request, a database update, or any other action that your code performs.
  5. AWS Lambda automatically scales the execution of your function based on the incoming event rate and the number of requests. It is doing it by using a combination of container reuse, auto-scaling, and request queueing. Here’s how it works:
    * When an event occurs that is specified as a trigger for an AWS Lambda function, the function is invoked and a container is created to execute the function code.
    * If there are additional incoming events while the function is running, AWS Lambda queues the events and assigns them to available containers for execution. This helps to ensure that the function can scale to handle a high rate of incoming events.
    * If the number of incoming events exceeds the available containers and the queue size, AWS Lambda automatically scales out the number of containers to handle the increased load. This is known as auto-scaling.
    * When the load decreases and there are idle containers available, AWS Lambda scales down the number of containers to minimize costs.
    * To further optimize performance and minimize cold starts, AWS Lambda can reuse containers that have already been created to execute previous invocations of the same function. This reduces the overhead of creating new containers for each execution.
    By using these techniques, AWS Lambda is able to automatically scale the execution of your function based on the incoming event rate and the number of requests, without the need for manual capacity planning or resource management.

Some key benefits of using AWS Lambda include:

  • No need to provision or manage servers: You only need to provide your code and AWS Lambda handles the rest.
  • Low cost: You only pay for the compute time that you consume. There is no charge when your code is not running.
  • High availability: AWS Lambda automatically replicates your code across multiple availability zones and automatically scales the compute capacity to match the incoming request rate.

Some potential disadvantages of using AWS Lambda include:

  • Limited control over the underlying infrastructure: Because AWS Lambda abstracts away the infrastructure, you have less control over the underlying compute resources and their configurations. This can make it more difficult to customize the environment or troubleshoot issues.
  • Cold start issues: When an AWS Lambda function is invoked after a period of inactivity, it can take longer to start up and execute due to the overhead of creating a new container and loading the function code and dependencies. This can result in slower response times for infrequently used functions.
  • Function size and execution time limits: AWS Lambda imposes limits on the size of the deployment package and the maximum execution time for a function. These limits can be increased upon request, but it can be inconvenient to have to request increases or to work around the limits.
  • Dependency management: If your function has dependencies on external libraries or packages, you need to package them with your function code and deploy them to AWS Lambda. This can be time-consuming and error-prone, and it can also increase the size of the deployment package.
  • VPC access: If your function needs to access resources in a VPC (Virtual Private Cloud), such as a database or a messaging queue, it can be more complex to set up and can incur additional charges for data transfer and elastic network interfaces.

References :

https://aws.amazon.com/tr/

--

--