AWS Lambda

Tanishq Rawat
3 min readMay 22, 2024

--

AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS) that allows you to run code without managing servers. Lambda executes your code only when needed and scales automatically.

Key Features of AWS Lambda

  1. Event-Driven Execution: Lambda functions are triggered by various AWS services or HTTP requests via Amazon API Gateway. Triggers can include changes to data in an Amazon S3 bucket, updates to a DynamoDB table, new messages in an Amazon SQS queue, or custom events from a variety of sources.
  2. Support to various Programming Languages: AWS Lambda supports multiple programming languages, including Python, Node.js, Java, Ruby, C#, Go, and custom runtimes.
  3. Automatic Scaling: AWS Lambda automatically scales your application by running code in response to each trigger. Your code runs in parallel and processes each trigger individually, scaling precisely with the size of the workload.

Let us understand this with a small practical application.

I will create a Lambda function and will trigger that function via HTTP API for which I will use AWS API Gateway. API Gateway will generate a URL from which we can trigger that function. I will use Postman for testing purposes.

In the above image, it can be observed that API Gateway is the starting point and the lambda function named learningEventTriggers is attached to Gateway, which means Gateway will pass the trigger to the lambda function.

Initial state, where no trigger was added

Now I will write the lambda handler function, which is the entry point function.

import json
def lambda_handler(event, context):
# TODO implement
return {
'statusCode': 200,
'body': json.dumps("Hi!!, Thanks for reaching out to me.")
}

My function will return a JSON in response, which will be a static string.

Now let us add a trigger so that this function can be executed.

  1. Click on add trigger, and select API Gateway
  2. Click on the radio button named Create New API gateway, if there exists some API you can choose that. I will go for the existing API.
  3. Keep security as open, so that anyone can access the link.
  4. Click on add, to add the trigger and copy the URL from the triggers section.

Paste the copied URL to Postman, GET request, and hit SEND

Response to the request we sent

This is the basic example of creating a Lambda function and triggering it via an HTTP endpoint.

How to get query parameters from the request?

import json
def lambda_handler(event, context):

query_params = event.get('queryStringParameters', {})
if query_params:
return {
'statusCode': 200,
'body': json.dumps(f"Query Parameter Hello found with value : {query_params['Hello']}")
}
else:
return {
'statusCode': 200,
'body': json.dumps("No param found")
}

With NO Parameter selected
With “Hello” parameter

There are many HTTP methods and there are particular ways to handle them, which can be found in the documentation.

This is how we can create lambda functions for various works, without actually thinking about VPS, computing power, and many other things.

AWS Lambda is a great serverless computing service.

--

--

Tanishq Rawat
Tanishq Rawat

Written by Tanishq Rawat

SDE @ Mobileum | Ex-SDE Intern at Cerebry

No responses yet