Table of Contents
- Introduction
- Lambda Function Code
- Lambda Configuration
- Lambda Event Source
- Quick Notes
- How to create a Lambda function?
- How to test Lambda function locally?
- How to manage access?
Introduction
Serveless applications are ones that don't require you to provision or manage any servers. They provide the following benefits:
- No server management
- Flexible scaling
- High availability
- No idle capacity
A typical AWS serverless architecture might have the following components:
- Compute - AWS Lambda
- APIs - Amazon API Gateway
- Storage - S3
- Database - DynamoDB
- Interprocess messaging - SNS and SQS
- Orchestration - AWS Step Functions and Amazon CloudWatch Events
- Analytics - Amazon Kinesis
As we can see in the above list, the computing component in a typical serverless architecture is AWS Lambda Service. It follows the Function-as-a-Service (FaaS) approach where the unit of execution and execution is functions. Lambda provides the ultimate abstraction because everything except the business logic is handled by AWS automatically. Lambda functions scale automatically and precisely with the size of the workload which makes it very handy when we build event-driven computing systems. For each event, a separate Lambda instance is triggered, therefore the execution is completely independent.
In this pose, we will cover the following aspect of Lambda function:
- Function Code
- Configuration
- Event Source
Lambda Function Code
Ultimately, a Lambda function is just a piece of code. To execute codes, we need to provide
- An execution environment (for example, operating system, JPM, etc.)
- Dependencies. This include code dependencies (e.g. third-party libraries) and resource dependencies (e.g. data or configuration files)
To execute a function, we need to provide
- Inputs to the function.
- Runtime context (e.g. global variables defined outside the function)
For AWS Lambda function, the execution runtime environment is based on an Amazon Linux AMI. The code and dependencies are stored in the Function Code Package. In AWS terms, the code and dependencies are assets. As Lambda is used in event-driven system, the inputs to a Lambda function are information about the event that triggered the function and the context such as AWS request ID, remaining time and logging hook. The inputs are provided in an Event object and the context is provided to Lambda function through a Context object.
What happens when an event occurs and triggers a Lambda function?
As events occur, the code package will be downloaded from the S3 bucket, installed in the Lambda runtime environment, and invoked as needed. Once the environment (more precisely, the function container) is set up, it remains active for a while and can be used by subsequent Lambda invocation. If a Lambda invocation runs on a re-used function container, we say it's running on a warm container; otherwise, we say the invocation is experiencing a cold start. Here is the diagram copied from AWS website

Lambda Configuration
We can configure the following properties of a Lambda function:
- Function Memory
- Version and Aliases
- IAM Role
- Lambda Function Permissions
- Network Configuration
- Dead Letter Queue
- Timeout
Lambda Event Source
Lambda functions can be triggered by events from the following services:
- Amazon DynamoDB
- Amazon Kinesis
- Amazon MQ
- Amazon Managed Streaming for Apache Kafka
- self-managed Apache Kafka
- Amazon Simple Queue Service
There are three invocation types:
- RequestResopnse: Used for synchronous execution.
- Event: Used for asynchronous execution.
- DryRun Used to test that the invocation is permitted for the caller, but don't execute the function.
Here the synchronous and asynchronous execution is related to the request-response process. For a synchronous execution, we will wait for the completion of the execution before sending a response to the requester. On the contrary, for an asynchronous execution, we could reply to the requester immediately without waiting.
Services that invoke Lambda functions synchronously
- Elastic Load Balancing (Application Load Balancer)
- Amazon Cognito
- Amazon connect
- Amazon Lex
- Amazon Alexa
- Amazon API Gateway
- Amazon CloudFront (Lambda@Edge)
- Amazon Kinesis Data Firehose
- Amazon Simple Storage Service Batch
Services that invoke Lambda functions asynchronously
- Amazon Simple Storage Service
- Amazon Simple Notification Service
- Amazon Simple Email Service
- AWS CloudFormation
- Amazon CloudWatch Logs
- Amazon CloudWatch Events
- AWS CodeCommit
- AWS Config
- AWS IoT
- AWS IoT Events
- AWS CodePipeline
There are two invocation models:
- Push Model. This is used by the following resources/services:
- S3
- Amazon API Gateway
- SNS
- AWS CloudFormation
- AWS Cloudwath Events
- Amazon Alexa
- Pull Model. This is used by the following resources/services:
- DynamoDB stream
- Kinesis Stream
Quick Notes
How to create a Lambda function?
we can create a Lambda function through the AWS console or use CreateFunction API.
How to test Lambda function locally?
We can use AWS SAM Local for local testing
How to manage access?
Lambda function may need to access other AWS resources. The access should be granted through IAM role. In this way, we don't need to save the access key information for the execution.
----- END -----
©2019 - 2023 all rights reserved