AWS Lambda: Practical Use Cases & Automations
AWS Lambda is a highly versatile serverless compute service used for both event-driven data processing and infrastructure automation. Below are key examples of how it is utilized in real-world scenarios.
1. Event-Driven Processing: Image Resizing
Scenario: Reducing the size of user-uploaded images to meet organizational standards.
The Workflow
- Upload: A user uploads a high-resolution image to an Amazon S3 bucket.
- Event Trigger: S3 generates an
ObjectCreatedevent, which automatically invokes the Lambda function. - Execution: The Lambda function runs the resizing script. Lambda supports multiple languages (Python, Node.js, Java, .NET, etc.), allowing you to use standard image-processing libraries.
- Output: The optimized image is saved back to a destination S3 bucket.
Required IAM Permissions
To securely execute this workflow, the Lambda function requires an Execution Role with the following policies attached:
- Lambda Execution:
AWSLambdaBasicExecutionRole(allows the function to run and write logs to CloudWatch). - S3 Access: Permissions to read from the source bucket (
s3:GetObject) and write to the destination bucket (s3:PutObject).
2. Dependency Management: Lambda Layers
When your code requires external libraries (e.g., Pillow for image processing or requests for API calls), package them into a Lambda Layer instead of the main deployment package.
- Why use layers? It keeps your core function code lightweight, allows you to share dependencies across multiple functions, and speeds up deployment.
- Documentation: Creating and sharing Python layers in AWS Lambda
Example: Downloading an Image from S3 (Python / Boto3)
Note: boto3 is included in the AWS Lambda Python runtime by default, so it does not require a custom layer.
import boto3
import os
s3_client = boto3.client('s3')
def download_image(bucket_name, object_key, download_path):
\"\"\"Downloads an image from S3 to the Lambda local /tmp storage\"\"\"
try:
s3_client.download_file(bucket_name, object_key, download_path)
print(f"Successfully downloaded {object_key} to {download_path}")
except Exception as e:
print(f"Error downloading file: {str(e)}")
raise e
- Cloud & DevOps Automations Lambda is a powerful tool for enforcing governance and automating repetitive SysAdmin tasks.
Use Case A: Resource Tagging Compliance
Organizations require strict tagging standards for cost tracking and security.
Trigger: An Amazon EventBridge rule (Cron Job) triggers the Lambda function every 1 hour.
Validation: The Python script uses boto3 to scan resources and check if mandatory tags exist.
Remediation: If tags are missing, Lambda automatically applies temporary tags (e.g., Compliance: Failed, Date_To_Terminate: <Date>).
Notification: Lambda triggers an Amazon SNS topic to send an email alert to the responsible team, warning them of the upcoming termination.
Use Case B: Instance State Management
To save costs, environments should only run when needed.
Action: Lambda scans for EC2 instances based on specific team tags (e.g., Team: usi-us).
Execution: Depending on the time of day, Lambda issues stop_instances(), start_instances(), or reboot_instances() commands to automate the environment’s uptime schedule.
