AWS Lambda
Allows you to execute your code without server. It is a Platform as a service (PAAS), where you can
choose environment, push the code and run it.
Why Lambda:
1. Serverless architecture
2. No virtual machines to be created
3. Monitor performance
4. Code freely
How it works:
When we upload the code to lambda, it executes the code on a predefined server. Once the code is
triggered, lambda will help in provision and managing.
How to use Lambda
There are two ways:
1. Author from scratch
2. Use a blueprint
Create a function in AWS Lambda using blueprint
Search lambda
Create a function
Select use a blueprint
Filter: hello-world
Select hello-world-python
Configure
Basic Information
Function name
Execution Role
Create new role with basic Lambda permissions
Create function
Once the function is created, click on test
From dropdown select configure test event
Create new event
Event name
In the code, replace value1 with hello world (or any text of which will be the output)
Create
To test your function
Click test (New tab called as execution results will open where we get the output)
Create function from scratch with integrated S3
Create function
Author from Scratch
Function name
Language (python 3.9)
Architecture (86-64)
Change the default execution role
Create a new role from AWS policy
Role name
Policy template
Search S3
AWS S3 read-only
Create function
Open and replace the default code with your code
You can use below code
(((----------------------------------------------------------------------------------------------------------------
import json
import urllib.parse
import boto3
print('Loading function')
s3 = boto3.client('s3')
def lambda_handler(event, context):
#print("Received event: " + json.dumps(event, indent=2))
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
try:
response = s3.get_object(Bucket=bucket, Key=key)
print("CONTENT TYPE: " + response['ContentType'])
return response['ContentType']
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in
the same region as this function.'.format(key, bucket))
raise e
))))))----------------------------------------------------------------------------------------------------------------
Add trigger
Select trigger
S3
Select bucket name from S3 (Create a S3 bucket)
All object create event
Check-in box for recursive loop
Click add
Open S3
Upload something to bucket
Open Lambda function
Monitor
Invocation shows as 1 due to one item added in S3 bucket
Log
Log stream
You can see the type of document
Monitor Lambda functions
Lambda monitors functions and reports metrics through AWS CloudWatch. It also tracks the
requests, latency per request, number of requests
When you click on monitor:
Invocations: The number of times code was executed
Duration: Time taken for execution
Success/ Failure rate: Code results
Delete Lambda Function
Select the function
Delete the function