Module 3: AWS Lambda
Introduction to AWS Lambda
AWS Lambda is a serverless compute service that lets you run code without provisioning or managing
servers. With Lambda, you can run code for virtually any type of application or backend service, all with
zero administration. Just upload your code, and Lambda takes care of everything required to run and
scale your code with high availability.
Key Concepts
Functions
A function in AWS Lambda is the core unit of deployment and execution. A function consists of code you
provide, along with some configuration settings. Lambda functions can be written in various languages,
including Node.js, Python, Ruby, Java, Go, and .NET Core.
● Code: The actual script or application logic you want to execute. This code can be uploaded
directly to Lambda, or it can be stored in an S3 bucket and referenced in Lambda.
● Configuration: Includes settings such as memory allocation, timeout duration, and environment
variables. Configuration settings help Lambda manage the execution environment for your
function.
Event Sources
Event sources are entities that publish events, which trigger Lambda functions to execute. These sources
can be AWS services, such as S3, DynamoDB, Kinesis, SNS, and others, or custom applications that you
set up to trigger Lambda functions via AWS SDK or API Gateway.
● Event-driven Model: Lambda functions are designed to be triggered by events, making it ideal
for real-time data processing, web services, and other scenarios where immediate execution is
required.
● Trigger Configuration: You configure triggers to define which event sources should invoke your
Lambda function and under what conditions.
Getting Started with AWS Lambda
Creating a Lambda Function
1. Open the AWS Management Console:
○ Navigate to the AWS Lambda service.
2. Create a Function:
○ Click on "Create function".
○ Choose an authoring method: "Author from scratch", "Use a blueprint", or "Browse
serverless app repository".
○ Provide the function name, runtime (e.g., Python 3.8, Node.js 14.x), and an execution
role.
3. Configure Function:
○ Set memory allocation and timeout settings.
○ Add environment variables if needed.
4. Write or Upload Code:
○ Use the inline code editor to write your function code, or upload a .zip file containing your
code and dependencies.
5. Set Up Event Source:
○ Choose an event source that will trigger your function, such as an S3 bucket, DynamoDB
table, or API Gateway.
6. Test the Function:
○ Create a test event in the Lambda console to simulate an event and verify your function
works as expected.
Example Code for a Simple Lambda Function in Python:
python
import json
def lambda_handler(event, context):
message = 'Hello, {}!'.format(event['name'])
return {
'statusCode': 200,
'body': json.dumps({'message': message})
}
Deploying and Managing Lambda Functions
1. Versioning and Aliases:
○ AWS Lambda allows you to create versions and aliases for your functions. This is useful
for managing updates and ensuring stable releases.
2. Monitoring and Logging:
○ Use Amazon CloudWatch to monitor Lambda function metrics, such as invocation count,
duration, and errors.
○ Enable logging to capture function logs and review them in CloudWatch Logs.
3. Scaling:
○ Lambda automatically scales your function in response to incoming requests. You can
control concurrency with reserved concurrency settings to limit the number of concurrent
executions.
Advanced Features and Use Cases
Security
● IAM Roles: Assign execution roles to Lambda functions to grant permissions for accessing other
AWS services.
● VPC Integration: Configure Lambda functions to access resources in your VPC, such as RDS
databases or EC2 instances.
Data Processing
● Real-time File Processing: Use Lambda to process files as they are uploaded to S3.
● Stream Processing: Use Lambda with Kinesis or DynamoDB streams to process data in real
time.
Automation
● Infrastructure Automation: Automate the deployment and management of AWS resources
using Lambda and CloudFormation.
● Scheduled Tasks: Use CloudWatch Events or EventBridge to schedule Lambda functions to run
at specific intervals.
Interesting Lambda Code Ideas
1. Image Processing:
○ Use Lambda to automatically resize and optimize images uploaded to an S3 bucket.
○ Example: Generate thumbnails for images uploaded to S3.
2. Real-time Data Processing:
○ Process data streams from IoT devices using Lambda and Kinesis.
○ Example: Analyze sensor data from IoT devices and store the results in DynamoDB.
3. Chatbot Backend:
○ Implement a backend for a chatbot using Lambda and API Gateway.
○ Example: Create a serverless chatbot for customer support that integrates with Slack or
Facebook Messenger.
4. Serverless REST API:
○ Build a RESTful API using Lambda and API Gateway.
○ Example: Create a CRUD API for managing a to-do list application.
5. Automated Security Checks:
○ Use Lambda to perform security checks on your AWS resources.
○ Example: Scan S3 buckets for publicly accessible objects and notify administrators.
6. PDF Generation:
○ Automatically generate PDFs from data or templates.
○ Example: Create invoices in PDF format based on order data and email them to
customers.
7. Log Analysis:
○ Analyze and aggregate logs from CloudWatch or other logging services.
○ Example: Detect and alert on specific patterns or anomalies in application logs.
8. Notification System:
○ Build a notification system using Lambda and SNS.
○ Example: Send SMS or email alerts based on specific events in your application.
9. Data Synchronization:
○ Synchronize data between different AWS services or external systems.
○ Example: Sync data from DynamoDB to Elasticsearch for search and analytics.
10. Sentiment Analysis:
○ Perform sentiment analysis on social media feeds or customer reviews.
○ Example: Analyze tweets mentioning your brand and categorize them as positive,
negative, or neutral.
Conclusion
AWS Lambda is a powerful service that enables you to build scalable and efficient serverless
applications. By understanding the key concepts, such as functions and event sources, and following the
steps to get started, you can leverage Lambda to automate tasks, process data, and build robust backend
services. With its flexible integration capabilities and automatic scaling, AWS Lambda opens up a wide
range of possibilities for innovative serverless solutions.
While AWS Lambda offers many powerful features for building serverless applications, it also has some
restrictions and limitations that you should be aware of when designing and deploying your functions.
These limitations are in place to ensure the efficient operation of Lambda and to prevent misuse or
overconsumption of resources.
AWS Lambda Restrictions and Limitations
Function and Deployment Package Limits
1. Deployment Package Size:
○ Compressed (.zip) file size: Up to 50 MB.
○ Uncompressed size: Up to 250 MB, including all dependencies.
2. Layers:
○ Each Lambda function can have up to 5 layers.
○ Each layer can be up to 50 MB when compressed.
3. Container Images:
○ The container image size can be up to 10 GB.
Resource Limits
4. Memory Allocation:
○ Lambda functions can allocate memory between 128 MB and 10,240 MB (10 GB), in 1
MB increments.
○ The amount of CPU available to a function scales with the amount of memory configured.
5. Execution Timeout:
○ The maximum execution timeout for a Lambda function is 15 minutes (900 seconds).
○ You can set the timeout for your function to any value from 1 second to 15 minutes.
6. Ephemeral Storage (/tmp) Space:
○ Each Lambda function has access to 512 MB of ephemeral storage in the /tmp directory.
○ This space is used for temporary storage during function execution and is not persistent
across invocations.
Invocation and Concurrency Limits
7. Request and Response Payload Size:
○ The maximum payload size for synchronous invocation requests (i.e., direct calls) is 6
MB.
○ The maximum payload size for asynchronous invocation requests (i.e., events from other
services) is 256 KB.
8. Concurrency Limits:
○ By default, each AWS account has a soft limit of 1,000 concurrent executions across all
functions in a region.
○ You can request an increase in this limit through the AWS Support Center if needed.
9. Burst Concurrency:
○ AWS Lambda can handle burst traffic, but the rate of increase in concurrency is limited to
about 500-3000 instances per minute, depending on the region and account.
Networking and Environment Limits
10. VPC Configuration:
○ When Lambda functions are configured to run inside a Virtual Private Cloud (VPC), they
can experience a cold start latency.
○ Ensure that Lambda functions have access to sufficient IP addresses in the subnets
where they are deployed.
11. Environment Variables:
○ Each Lambda function can have up to 4 KB of environment variables.
○ Environment variables are used to store configuration settings and sensitive information.
Execution Environment Limits
12. Lambda@Edge:
○ Lambda@Edge functions have specific limitations, such as a maximum deployment
package size of 1 MB and a maximum execution timeout of 5 seconds.
13. Language and Runtime Support:
○ Lambda supports several programming languages and runtimes, including Node.js,
Python, Java, Go, Ruby, and .NET Core.
○ Ensure that the runtime and language version you choose is supported by Lambda.
Logging and Monitoring Limits
14. Logging:
○ The maximum size for a single log event in CloudWatch Logs is 256 KB.
○ Logging excessive amounts of data can lead to increased costs and slower function
execution times.
IAM and Security Limits
15. IAM Role:
○ Lambda functions require an execution role with permissions to access AWS resources.
○ Be mindful of the principle of least privilege when assigning permissions to the execution
role.
Performance and Best Practices
16. Cold Starts:
○ Cold starts occur when a new instance of a Lambda function is invoked for the first time
or after being idle. Cold starts can cause latency issues.
○ To minimize cold starts, consider strategies like keeping functions warm through
scheduled invocations.
17. Function Timeout:
○ Functions should be designed to complete within the timeout limit to avoid unexpected
terminations.
○ Break down long-running tasks into smaller, manageable chunks.
18. Dependencies:
○ Minimize the size of your deployment package by including only necessary
dependencies.
○ Use Lambda layers to share common dependencies across multiple functions.
Conclusion
AWS Lambda is a powerful tool for building scalable and serverless applications, but it's important to be
aware of its limitations and constraints. By understanding these restrictions and designing your functions
accordingly, you can optimize performance, manage resource usage, and ensure the efficient operation
of your serverless applications. Keep these limitations in mind when planning, deploying, and managing
your Lambda functions to make the most of what AWS Lambda has to offer.
REFERENCES:
https://aws.amazon.com/pm/lambda/
https://aws.amazon.com/lambda/
https://docs.aws.amazon.com/lambda/
https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html
Free API to use with lambdas
https://sv443.net/jokeapi/v2/#getting-started
More Lambda code ideas
The simple Lambda function provided in the previous example logs a message to CloudWatch Logs when
triggered by a CloudWatch event. Here are a few ideas for how you could extend this Lambda function to
perform more useful tasks:
1. Send Notifications
You could extend the Lambda function to send notifications to users or administrators. For example, you
could use Amazon Simple Notification Service (SNS) to send emails or text messages.
const AWS = require('aws-sdk');
const sns = new AWS.SNS();
exports.handler = async (event) => {
const params = {
Message: 'Lambda function triggered by CloudWatch event',
Subject: 'CloudWatch Event Notification',
TopicArn: 'arn:aws:sns:your-region:your-account-id:your-topic'
};
await sns.publish(params).promise();
console.log("Notification sent");
return {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
};
2. Invoke Another Lambda Function
You could use this Lambda function to invoke another Lambda function. This is useful for creating chains
of Lambda functions or for breaking down complex tasks.
const AWS = require('aws-sdk');
const lambda = new AWS.Lambda();
exports.handler = async (event) => {
const params = {
FunctionName: 'AnotherLambdaFunction',
InvocationType: 'Event',
Payload: JSON.stringify(event)
};
await lambda.invoke(params).promise();
console.log("Invoked another Lambda function");
return {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
};
3. Fetch and Process Data from an API
You could use the Lambda function to fetch data from an external API and process it. For example, you
could fetch weather data and store it in an Amazon S3 bucket or a DynamoDB table.
const https = require('https');
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.handler = async (event) => {
const data = await new Promise((resolve, reject) => {
https.get('https://api.example.com/data', (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
resolve(data);
});
}).on("error", (err) => {
reject(err);
});
});
const params = {
Bucket: 'your-bucket-name',
Key: 'data.json',
Body: data,
ContentType: 'application/json'
};
await s3.putObject(params).promise();
console.log("Fetched and stored data in S3");
return {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
};
4. Clean Up Old Files in S3
You could use the Lambda function to clean up old files in an S3 bucket. This is useful for managing
storage costs and keeping your data organized.
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.handler = async (event) => {
const params = {
Bucket: 'your-bucket-name'
};
const data = await s3.listObjectsV2(params).promise();
const deleteParams = {
Bucket: 'your-bucket-name',
Delete: { Objects: [] }
};
data.Contents.forEach((item) => {
if (new Date(item.LastModified) < new Date(Date.now() -
30*24*60*60*1000)) { // 30 days
deleteParams.Delete.Objects.push({ Key: item.Key });
}
});
if (deleteParams.Delete.Objects.length > 0) {
await s3.deleteObjects(deleteParams).promise();
console.log("Deleted old files from S3");
} else {
console.log("No old files to delete");
}
return {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
};
5. Rotate Database Credentials
You could use the Lambda function to rotate database credentials stored in AWS Secrets Manager. This
is useful for improving security by regularly updating credentials.
const AWS = require('aws-sdk');
const secretsManager = new AWS.SecretsManager();
exports.handler = async (event) => {
const secretName = 'your-database-credentials';
const secret = await secretsManager.getSecretValue({ SecretId:
secretName }).promise();
const credentials = JSON.parse(secret.SecretString);
// Generate new credentials (this is just a placeholder, implement your
own logic)
const newCredentials = {
username: credentials.username,
password: 'newpassword'
};
await secretsManager.updateSecret({
SecretId: secretName,
SecretString: JSON.stringify(newCredentials)
}).promise();
console.log("Database credentials rotated");
return {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
};
These examples show how you can extend a simple Lambda function to perform a variety of tasks when
triggered by a CloudWatch event.