AWS Lambda Introduction

Nathan Luong | May 19, 2024 5

What is AWS Lambda

  • AWS Lambda is a Serverless Function as a Service (FAAS) offering made by AWS
  • Launched in November 13 2014, Lambda is a mature serverless service that support countless use-cases, workflows, and AWS native integrations.
  • Formally AWS Lambda allows user to:
    • Run code without provisioning or managing servers, maintaining event integrations, or managing runtimes
    • Run code for any type of application or backend service.
    • Write custom logic on most popular programming language, and use both serverless and container tools (Docker, AWS SAM)

Why using AWS Lambda

Leveraging Serverless Technology

Depending on the computation demands, it doesn’t always make sense to spin up a long-living server to handle some short-living logic.

Easy to manage short-living logic

ECR Integration
  • One good example is to automatically trigger a Lambda after a new Docker image is pushed to AWS Elastic Container Registry (ECR).
  • Simply put, having this logic inside of a long-living computing solution (such as Fargate, or EC2) is unnecessary and decrease development efficiency, when these simple logic can be implemented in a few files.

Achieving extreme granular access control

Micro-service Example
  • In this sample e-commerce micro-service application, there are 3 Lambda functions which access 3 different DynamoDB tables.
  • With 3 separate Lambdas, we can assign custom IAM role to them, which only grant them access to the corresponding DynamoDB Table (ex: Ordering Lambda with access to write to Order table, but read access to other tables)
  • With traditional monolith application, we would need to grant the entire server, with read-write access to the entire database. Which can be insecure if an attacker get a hold of that one server.

Other Benefits

  1. Pay-as-you go: Since Lambda only got triggered when it needs to, teams will only have to pay for exactly those computation resources, no more, and no less.
  2. Quick Deployment and Updates: With amazing first-party and third-party tooling (such as , , ), testing, deploying and monitoring Lambda becomes a thousand time simpler.
  3. Leveraging edge computing: Because Lambda is not hosted on an origin server, its code can be run from anywhere. Therefore Lambda can be run on servers that are closer to the user, which significantly reduce cross-region latency. This can be achieve via Lambda@Edge, utilizing AWS Edge network, and can be configured via Amazon CloudFront.

Native integrations with other AWS services

Invocation Examples
  • AWS Lambda can be triggered by other popular AWS services, which significantly extends the use-cases and functionality of these services.

How to use AWS Lamba

Lambda Invocation via REST API calls, (API Gateway Integration with Lambda Proxy)

API-GW
// Lambda function to return `Hello, ${greeter}!$`
// When user hit endpoint curl -X GET '<API-Gateway-URL>/helloworld?greeter=John'
const handler = async (event, context, callback) => {
    const res ={
        "statusCode": 200,
        "headers": {
            "Content-Type": "*/*"
        }
    };
    const greeter = event.queryStringParameters?.greeter 
	    ? event.queryStringParameters?.greeter
	    : 'World';
    res.body = `Hello, ${greeter}!$`
    return res
};

export { handler }

Periodic Lambda Invocation with EventBridge

API-GW
const handler = async (event, context, callback) => { 
	console.log('LogScheduledEvent'); 
	console.log('Received event:', JSON.stringify(event, null, 2)); 
	callback(null, 'Finished'); 
};

export { handler }