AWS Lambda
AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. It is designed to enable developers to run code without provisioning or managing servers. It executes code in response to events and automatically manages the computing resources required by that code. Lambda allows you to execute code for almost any application or backend service without any administrative overhead. You’re billed solely for the compute time you use; there are no fees when your code isn’t active.
How it works!
Terminologies
Context Object :
- Provides methods and properties that provide information about the invocation, function, and runtime environment
- Passed to your function by Lambda at runtime
- Example: aws_request_id, function_name, memory_limit_in_mb
Event Object :
- The JSON-formatted document contains data for the function to process
- Contains information from the invoking service
- Lambda runtime converts the event to an object
- Example: input arguments, invoking service arguments
Destinations:
- Send results to a destination
- Asynchronous invocations can define destinations for successful and failed events
- AWS recommends to use destination instead of DLQ
Environment Variables :
- Environment variable = key / value pair in “String” form
- Adjust the function behavior without updating code
- The environment variables are available to your code
- Helpful to store secrets
- Environment variables (4 KB)
Lambda Characteristics
- By default, your Lambda function is launched outside your own VPC
- For launching Lambda inside VPC, AWS creates Elastic Network Interfaces(ENI)
- A lambda in VPC has no internet access, and it is provided by NAT – IG.
- It has RAM from 128MB to 10GB
- Default 3 seconds timout and max 15min.
- Lambda has execution context i.e a temporary runtime environment that initializes any external dependencies of your lambda code.
- Lambda can use the /tmp directory with Max Size 10B.
- Layers: Externalize Dependencies to re-use them.
- Concurrency limit: up to 1000 concurrent executions
- Lambda has versions and you can give alias to particular versions
- Lambda function deployment size (compressed .zip): 50 MB
- Size of uncompressed deployment (code + dependencies): 250 MB
AWS Integrations
AWS Pricing
- Pay per calls:
- The first 1,000,000 requests are free
- $0.20 per 1 million requests thereafter ($0.0000002 per request)
- Pay per duration
- 400,000 GB-seconds of compute time per month for FREE
- After that $1.00 for 600,000 GB-seconds
Use Cases
Thumbnail Generator
In this guide, you’ll set up a Lambda function to resize images uploaded to an Amazon Simple Storage Service (Amazon S3) bucket. Whenever you upload an image to your bucket, Amazon S3 triggers your Lambda function. This function then generates a thumbnail of the image and saves it to another Amazon S3 bucket.
Step1 : Create S3 source destination Buckets
- Create an S3 bucket tradesman.pic & bucket tradesman.pic-resized(make sure the second bucket has a naming convention of firstbucket-resized).
- Upload a test pic in tradesman.pic bucket
Step2: Create Policies, Roles
Create a policy: LambdaS3Policy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:PutLogEvents",
"logs:CreateLogGroup",
"logs:CreateLogStream"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": "arn:aws:s3:::*/*"
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": "arn:aws:s3:::*/*"
}
]
}
Create a role: LambdaS3Role
For Trusted entity type, select AWS service, and for Use case, select Lambda.
Step2: Create Lambda Thumbnail generator Code
Download code:
https://github.com/jonesjalapatgithub/thumbnailgenerator
Run mvn clean package to generate jar file.
Step3: Create Lambda Function
Create a function with the following values
Upload S3
Edit the Runtime and update the handler method
Step4: Create Trigger
Step5: Test
Upload document to source bucket
The thumbnail is generated automatically in the second bucket with a reduced size