Going Serverless With AWS 2
Going Serverless With AWS 2
Going Serverless With AWS 2
Serverless
With AWS
Companies need to own the challenges of patching and deploying to those server fleets
as their applications evolve.
Companies also must scale their server fleets to account for peak load and then
attempt to scale them down when and where possible to lower costs—all while
protecting the experience of end users and the integrity of internal systems.
Idle, underutilized servers prove to be costly and wasteful. Analysts estimate that as
many as 85 percent of servers in practice have underutilized capacity.
Libraries, artifacts, or compiled native binaries that can execute on top of the runtime
environment can be part of your function code package
Code can also be written in other programming languages (PHP, SmallTalk etc.), as
long as you stage and invoke that code from within one of the support languages in
the AWS Lambda runtime environment.
AWS provides a set of tools called AWS SAM Local to enable local testing of Lambda
functions.
When a Lambda function is invoked, code execution begins at what is called the
handler.
Java context) {
•
•
...
}
Node.js
• exports.handlerName = function(event, context, callback) {
• ... // callback parameter is optional
• }
Python
• def handler_name(event, context):
• ...
• return some_value
C#
• def handler_name(event, context):
• ...
• return some_value
The Event Object
The contents of the event parameter include all of the data and metadata your Lambda
function needs to drive its logic.
The event differs in structure and contents, depending on which event source created
it.
Note : It’s important to understand the central tenant when writing code for Lambda:
your code cannot make assumptions about state.
Warm and Cold Function Containers
Lambda Function Event Sources
You can associate your Lambda function with event sources occurring within AWS services that
will invoke your function as needed or use the Invoke API to directly Invoke the functions.
Invocation Patterns
Push Model – Lambda function is invoked every time a particular event occurs within
another AWS service(Example : S3, API Gateway, SNS, CloudFormation, CloudWatch events
and logs, Alexa)
Pull Model – Lambda polls a data source and invokes the function with any new records
that arrive at the data source, batching new records together in a single function invocation
(Example : DynamoDb, Kinesis Streams, SQS)
InvocationType Parameter
Lambda function can be executed synchronously or asynchronously. This parameter has three
possible values :
RequestResponse – Execute synchronously
Event – Execute asynchronously
DryRun – Test that invocation is permitted for the caller, but don’t execute the function.
Lambda Function Configuration
You have various configuration options to set that define how your code is executed
within Lambda –
Function Memory
You can allocate 128 MB of RAM up to 1.5 GB of RAM to your Lambda function.
The same dial will also influence the CPU and network resources available to your
function.
Timeout
You can designate the maximum amount of time a single function execution is allowed
to complete before a timeout is returned.
The maximum timeout for a Lambda function is 300 seconds.
IAM Role
You can assign an IAM role (called an execution role) to each of your Lambda
functions.
Lambda Function Permissions
With permissions, you declare a function policy that lists the AWS Resource Names
(ARNs) that are allowed to invoke a function.
Network Configuration
A Lambda function has two broad options for outbound network connectivity:
o Default
o VPC
Environmental Variables
Environment variables for Lambda functions enable you to dynamically pass data to
your function code and libraries without making changes to your code.
Dead Letter Queues
If an exception occurs when trying to invoke your function in these models, the
invocation will be attempted two more times (with back-off between the retries). After
the third attempt, the event is either discarded or placed onto a dead letter queue, if
you configured one for the function.
Shared Responsibility Model - Lambda
Lambda Runtime Environment
Storage and State
In addition to the highly adopted use cases, other domains in which companies are
using Serverless approach are -
Bigdata, such as MapReduce problems, high speed video transcoding etc.
Low Latency, custom processing for applications and assets delivered though CDNs
Connected Devices enabling serverless capabilities to run inside IoT Infrastructures
Custom logic and data handling in on-premise devices like Snowball Edge
A Case Study – Bustle.com
About Bustle
Bustle.com is a news, entertainment, lifestyle, and fashion website catering to women.
Bustle also operates Romper.com, a website focused on motherhood. Bustle is based in
Brooklyn, NY and is read by 50 million people each month.
The Challenge
Bustle was originally running on a third-party platform as a service (PaaS) but
migrated to using AWS OpsWorks to gain better scale and availability.
The site was built as a monolith and had trouble scaling.
There was also a fair amount of server management, automation, and monitoring
involved in order to keep the website running smoothly; this raised the barrier of entry
for new engineers to roll out new code changes.
The engineering team wanted to focus on areas where they thought they brought the
customer the most value, which was building front-end features instead of dealing with
operations.
The Solution
Bustle also built their own AWS Lambda and Amazon API
Gateway software-delivery tool, allowing developers to easily
do integration tests and deployments when they are ready to
release their code into production.