All About AWS Lambda Service

Ghazanfar Ali
6 min readAug 16, 2024

--

AWS lambda is a compute service and fully serverless which allow to run your code without provisioning any server, it runs on pay as you go model. We do not need to manage the scaling according to the load, lambda itself manages the load and scaling.
1-Concurrency:

There is two types of concurrency in lambda:
1- Reserved:

2- Provisioned:
In AWS lambda whenever it is invoked it runs the cold start, you can essentially tells AWS that i wanna respond immediately whenever request comes so that container will be already to run your lambda request it is called warm start:

Here 90 means 90 containers are ready to provisioned my lambda request but i will also bear cost.
Let understand the lambda cold start:

In this case a client is send the api request to api gateway then api gateway integrate with lambda that is invoked and update the dynamodb, here in this scenario lambda is not the server that is running all the time and respond immediately so it always takes the cold start means whenever it is invoked it spins up the new container and execute the lambda on it sometime it can cause delays as some request need immediate response.
Let suppose when first request is served and it comes the another request from another client in this case previous execution env will still be available so that it will skip the steps 2,3,4 in this case , it is called warm start. But without provisioned concurrency enabled if first request completed and then second comes there will be not warm start, it will again cold start so repeat all the 5 steps, other than this provisioned concurrency we can also use the aws cloud watch event role that will continuously ping the lambda at specific interval of time to keep our execution time keep active.

2- Layers:
Whenever you work on multiple projects you create a library for multiple projects same goes for lambda:

Let suppose there is some common library that are common between many functions here comes the layer, so we do not need to copy the same code in every lambda we just create a layer (library) and use that in multiple lambdas in same AWS account.
We can upload the layer in zip file in lambda section in aws account:

3- Trigger:
Let suppose you want to invoke your lambda function on some kind of interval or invoke by any other aws resources like api gateway, s3, dynamodb etc we use lambda triggers.

4- Environment Variables:
AWS Lambda environment variables are simple key-value pairs that you can use to configure your Lambda function at runtime. They let you inject configuration settings, secrets, or any other data your function needs to run without changing the code. Here’s a straightforward explanation:

What Are Environment Variables?

  • Key-Value Pairs: Environment variables are like a dictionary where each key (name) has a corresponding value. For example, API_KEY might be a key, and 12345 could be its value.
  • Configuration Settings: They help you provide different configurations or settings to your Lambda function based on the environment it’s running in (e.g., development, staging, production).

5- Step Functions with Lambda:
AWS Step Functions is a service that helps you coordinate multiple AWS services into serverless workflows, making it easier to build and manage complex applications. It allows you to define a series of steps (or states) that execute in a specific order, using a visual workflow.

Key Concepts

  1. State Machine: This is the workflow definition. It includes the sequence of steps (states) that your application will execute.
  2. States: Individual steps in the workflow. Each state can perform a task, make a choice, or pass data to the next state.
  3. Tasks: The work that each state performs. In this context, tasks often involve invoking Lambda functions.

How AWS Step Functions Work with Lambda

  1. Define a State Machine: You create a state machine that specifies the sequence of steps your application should follow. This definition is written in a JSON-based language called Amazon States Language (ASL).
  2. Invoke Lambda Functions: Each state in your state machine can invoke a Lambda function to perform a specific task. For example, one state might call a Lambda function to process data, while another state calls a different Lambda function to send a notification.
  3. Transition Between States: After a state completes its task, it transitions to the next state based on the outcome. This transition logic can include conditional branching, allowing your workflow to make decisions and follow different paths.
  4. Handle Errors and Retries: Step Functions can handle errors and retries for you, ensuring that your workflow can recover from failures and continue executing.

Example Use Case

Imagine you have an application that processes user uploads and sends a notification once processing is complete. Here’s how you could use AWS Step Functions with Lambda to achieve this:

  1. Upload State: When a user uploads a file, an initial Lambda function processes the file. This is the first state.
  2. Processing State: After the upload is processed, the state machine transitions to the next state, which calls another Lambda function to analyze the file.
  3. Notification State: Once the analysis is complete, the state machine transitions to a final state that calls a Lambda function to send a notification to the user.
  1. We can also use api gateway to invoke our step function like we do lambda integration.
    Now we will see practical demo of step function with lambda:
  1. State machine is like a work flow template, first we will create the design of out state machine, add choice and two rules in it, then paste the lambda there and update the names:
  1. Condition example:
  1. Now we will write these lambdas and select them in state machine where we define the diagram:
    Purchase handler (pyhton 3.9):
    import json

def lambda_handler(event, context):

# TODO implement

return {

“result”: “Purchased proceed success”

}

Refund Handler:
import json

def lambda_handler(event, context):

# TODO implement

return {

“result”: “Refund proceed success”

}

3- Result Handler:
import json

def lambda_handler(event, context):

step_function_result = event[‘result’]

return {

‘body’: step_function_result + “_RESULT”

}

Select the functions in state machine diagram:

Now its time to test things out!
Go to your state machine and click on start execution:

Then give below input and click test:

It executed successfully executed:

1-

2-

Thats all here.

--

--

Ghazanfar Ali
Ghazanfar Ali

Written by Ghazanfar Ali

I write technical blogs on DevOps and AWS, Azure and GCP

No responses yet