Serverless Function
With Python and
AWS Lambda
Hello!
Nice to meet you
My Name is Fitrah Elly Firdaus. I am Software
Engineer at Traveloka.
Contact me at:
▪ fitrah.firdaus@gmail.com
▪ @FEF123
Table of contents 1. AWS Lambda
2. API Gateway
3. Python With Lambda
3
1.
AWS
Lambda
4
What is
AWS
Lambda
• Run code without thinking about servers. Pay only for the
compute time you consume.
• First 1M requests per month are free.
• 400.000 GB-Seconds of compute time per month, free.
• $0.20 per 1M requests thereafter, $0.0000002 per request.
• $0.00001667 for every GB-Second used thereafter
• Supported Language: Python, Go, Java, NodeJS, C#
5
How It
Works
6
Use Cases • Data Processing
• Real Time File Processing
• Real Time Stream Processing
• Extract, Transform and Load
• Backend
• IOT Backends
• Mobile Backends
• Web Applications
7
Lambda
Designer
8
2. AWS API
Gateway
9
What is API
Gateway
• Create, Maintain, and secure API at any scale
• First 1M API Calls per month are free
• $4.25 per million API calls received, plus the cost of data
transfer out, in gigabytes.
10
What can be
integrate?
11
3. Python
With
Lambda
12
General
Syntax
Structure
def handler_name(event, context):
...
return some_value
13
What is
event?
• Usually python dict type
• list
• str
• int
• float
• NoneType
• Proxy Integration if using API Gateway
14
What is
context
• This information usually contain
• How much time is remaining before AWS Lambda
terminates your Lambda function
• Cloudwatch log group
• AWS Request ID
• Mobile Application function, if called from mobile apps
15
What is
context
Object
Methods
• get_remaining_time_in_millis()
• Returns the remaining execution time, in milliseconds,
until AWS Lambda terminates the function.
16
What is
context
Object
attributes
• function_name
• function_version
• invoked_function_arn
• memory_limit_in_mb
• aws_request_id
• log_group_name
• log_stream_name
• identity
• client_context
17
Format of a
Lambda
Function for
Proxy
Integration
{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"body": "...”
}
18
How to log
the handler
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def my_logging_handler(event, context):
logger.info('got event{}'.format(event))
logger.error('something went wrong’)
return 'Hello from Lambda!'
19
CloudWatch
20
How to
create
exception
def always_failed_handler(event, context):
raise Exception('I failed!')
21
LITTLE DEMO
https://github.com/fitrah-firdaus/Simple-Crud-
Lambda-Python
22
Thank you very much
for your time
23
If you have any questions about this document
please don’t hesitate to contact me at:
▪ fitrah.firdaus@gmail.com
▪ @FEF123
▪ @pythonID

Serverless Function With Python and AWS Lambda

Editor's Notes

  • #18 Information about the client application and device when invoked through the AWS Mobile SDK. It can be null. client_context.client.installation_id client_context.client.app_title client_context.client.app_version_name client_context.client.app_version_code client_context.client.app_package_name client_context.custom A dict of custom values set by the mobile client application. client_context.env A dict of environment information provided by the AWS Mobile SDK.