Serverless-Architectures-With-Aws-Lambda Documentation
Serverless-Architectures-With-Aws-Lambda Documentation
November 2017
Notices
This document is provided for informational purposes only. It represents AWS’s
current product offerings and practices as of the date of issue of this document,
which are subject to change without notice. Customers are responsible for
making their own independent assessment of the information in this document
and any use of AWS’s products or services, each of which is provided “as is”
without warranty of any kind, whether express or implied. This document does
not create any warranties, representations, contractual commitments,
conditions or assurances from AWS, its affiliates, suppliers or licensors. The
responsibilities and liabilities of AWS to its customers are controlled by AWS
agreements, and this document is not part of, nor does it modify, any agreement
between AWS and its customers.
This whitepaper is meant to provide you with a broad overview of AWS Lambda,
its features, and a slew of recommendations and best practices for building your
own serverless applications on AWS.
•
This paper has been archived
High availability – Serverless applications have built-in availability
and fault tolerance. You don't need to architect for these capabilities
because
For thethe services
latest running thecontent,
technical applicationrefer
providetothem
thebyAWS
default.
• Whi–tepapers
No idle capacity & Guides
You don't have to pay forpage:
idle capacity. There is no
need to pre-provision or over-provision capacity for things like compute
https://aws.amazon.com/whitepapers
and storage. There is no charge when your code isn’t running.
The AWS Cloud provides many different services that can be components of a
serverless application. These include capabilities for:
Page 1
Amazon Web Services – Serverless Architectures with AWS Lambda
This whitepaper will focus on AWS Lambda, the compute layer of your
serverless application where your code is executed, and the AWS developer tools
and services that enable best practices when building and maintaining
serverless applications with Lambda.
Page 2
Amazon Web Services – Serverless Architectures with AWS Lambda
With Lambda, you can run code for virtually any type of application or backend
service. Lambda runs and scales your code with high availability.
This paper has been archived
Each Lambda function you create contains the code you want to execute, the
configuration
For the that defines
latest how your code
technical is executed
content, and,to
refer optionally,
the AWS one or
more event sources that detect events and invoke your function as they occur.
Whitepapers & Guides section.
These elements are covered in more detail in the nextpage:
https://aws.amazon.com/whitepapers
An example event source is API Gateway, which can invoke a Lambda function
anytime an API method created with API Gateway receives an HTTPS request.
Another example is Amazon SNS, which has the ability to invoke a Lambda
function anytime a new message is posted to an SNS topic. Many event source
options can trigger your Lambda function. For the full list, see this
documentation.10 Lambda also provides a RESTful service API, which includes
the ability to directly invoke a Lambda function.11 You can use this API to
execute your code directly without configuring another event source.
You don’t need to write any code to integrate an event source with your Lambda
function, manage any of the infrastructure that detects events and delivers them
to your function, or manage scaling your Lambda function to match the number
of events that are delivered. You can focus on your application logic and
configure the event sources that cause your logic to run.
Page 3
Amazon Web Services – Serverless Architectures with AWS Lambda
Your Lambda function runs within a (simplified) architecture that looks like the
one shown in Figure 2.
Once you configure an event source for your function, your code is invoked
when the event occurs. Your code can execute any business logic, reach out to
external web services, integrate with other AWS services, or anything else your
application requires. All of the same capabilities and software design principles
that you’re used to for your language of choice will apply when using Lambda.
This paper has been archived
Also, because of the inherent decoupling that is enforced in serverless
applications through integrating Lambda functions and event sources, it’s a
natural fit to build microservices using Lambda functions.
For the latest technical content, refer to the AWS
Whitepapers
With a basic understanding of serverless&principles
Guidesandpage:
Lambda, you might be
ready to start writing some code. The following resources will help you get
https://aws.amazon.com/whitepapers
started with Lambda immediately:
Page 4
Amazon Web Services – Serverless Architectures with AWS Lambda
Let’s begin our deep dive by further expanding and explaining each of the major
components of Lambda that we described in the introduction: function code,
event sources, and function configuration.
When you create a Lambda function (through the AWS Management Console,
or using the CreateFunction API) you can reference the S3 bucket and object
Page 5
Amazon Web Services – Serverless Architectures with AWS Lambda
key where you’ve uploaded the package.18 Alternatively, you can upload the code
package directly when you create the function. Lambda will then store your code
package in an S3 bucket managed by the service. The same options are available
when you publish updated code to existing Lambda functions (through the
UpdateFunctionCode API).19
As events occur, your code package will be downloaded from the S3 bucket,
installed in the Lambda runtime environment, and invoked as needed. This
happens on demand, at the scale required by the number of events triggering
your function, within an environment managed by Lambda.
The Handler
When a Lambda function is invoked, code execution begins at what is called the
handler. The handler is a specific code method (Java, C#) or function (Node.js,
Python) that you’ve created and included in your package. You specify the
handler when creating a Lambda function. Each language supported by Lambda
has its own requirements for how a function handler can be defined and
referenced within the package.
This paper has been archived
The following links will help you get started with each of the supported
languages.
For the latest technical content, refer to the AWS
Language
Whitepapers & Guides page:
Example Handler Definition
20
Java
https://aws.amazon.com/whitepapers
MyOutput output handlerName(MyEvent event, Context context) {
...
Node.js21
exports.handlerName = function(event, context, callback) {
...
// callback parameter is optional
Python22
def handler_name(event, context):
...
return some_value
Page 6
Amazon Web Services – Serverless Architectures with AWS Lambda
C#23
myOutput HandlerName(MyEvent event, ILambdaContext context) {
...
Once the handler is successfully invoked inside your Lambda function, the
runtime environment belongs to the code you’ve written. Your Lambda function
is free to execute any logic you see fit, driven by the code you’ve written that
starts in the handler. This means your handler can call other methods and
functions within the files and classes you’ve uploaded. Your code can import
third-party libraries that you’ve uploaded, and install and execute native
binaries that you’ve uploaded (as long as they can run on Amazon Linux). It can
also interact with other AWS services or make API requests to web services that
it depends on, etc.
Page 7
Amazon Web Services – Serverless Architectures with AWS Lambda
o Java24
o Node.js25
o Python26
o C#27
This means that your code cannot make any assumptions that state will be
preserved from one invocation to the next. However, each time a function
container is created and invoked, it remains active and available for subsequent
invocations for at least a few minutes before it is terminated. When subsequent
invocations occur on a container that has already been active and invoked at
least once before, we say that invocation is running on a warm container.
When an invocation occurs for a Lambda function that requires your function
code package to be created and invoked for the first time, we say the invocation
is experiencing a cold start.
Page 8
Amazon Web Services – Serverless Architectures with AWS Lambda
Overall, each language that Lambda supports has its own model for packaging
source code and possibilities for optimizing it. Visit this page to get started with
each of the supported languages.28
Page 9
Amazon Web Services – Serverless Architectures with AWS Lambda
Invocation Patterns
There are two models for invoking a Lambda function:
• Pull Model – Lambda polls a data source and invokes your function
with any new records that arrive at the data source, batching new
records together in a single function invocation (for example, new
records in an Amazon Kinesis or Amazon DynamoDB stream).
• DryRun – Test that the invocation is permitted for the caller, but don’t
This paper has been archived
execute the function.
Each event source dictates how your function can be invoked. The event source
is also For the latest
responsible technical
for crafting content,
its own event refer
parameter, todiscussed
as we the AWS earlier.
Whitepapers & Guides page:
The following tables provide details about how some of the more popular event
https://aws.amazon.com/whitepapers
sources can integrate with your Lambda functions. You can find the full list of
supported event sources here.30
Page 10
Amazon Web Services – Serverless Architectures with AWS Lambda
Page 11
Amazon Web Services – Serverless Architectures with AWS Lambda
Amazon SNS
Page 12
Amazon Web Services – Serverless Architectures with AWS Lambda
Amazon Alexa
Page 13
Amazon Web Services – Serverless Architectures with AWS Lambda
Whitepapers &Real-time
Guides page: of streaming log
alerting/monitoring
statements or other application events.
https://aws.amazon.com/whitepapers
Function Memory
To define the resources allocated to your executing Lambda function, you’re
provided with a single dial to increase/decrease function resources:
memory/RAM. You can allocate 128 MB of RAM up to 1.5 GB of RAM to your
Lambda function. Not only will this dictate the amount of memory available to
Page 14
Amazon Web Services – Serverless Architectures with AWS Lambda
your function code during execution, but the same dial will also influence the
CPU and network resources available to your function.
When calling the Invoke API or creating an event source for your Lambda
function, you can also specify a specific version of the Lambda function to be
executed.33 If you don’t provide a version number, or use the ARN that doesn’t
contain the version number, $LATEST is invoked by default.
Page 15
Amazon Web Services – Serverless Architectures with AWS Lambda
Invoking your Lambda functions by their version numbers can be useful during
testing and operational activities. However, we don’t recommend having your
Lambda function be triggered by a specific version number for real application
traffic. Doing so would require you to update all of the triggers and clients
invoking your Lambda function to point at a new function version each time you
wanted to update your code. Lambda aliases should be used here, instead. A
function alias allows you to invoke and point event sources to a specific Lambda
function version.
However, you can update what version that alias refers to at any time. For
example, your event sources and clients that are invoking version number 5
through the alias live may cut over to version number 6 of your function as
soon as you update the live alias to instead point at version number 6. Each
alias can be referred to within the ARN, similar to when referring to a function
version number:
arn:aws:lambda:[region]:[account]:function:[fn_name]:[alias]
Here are some example suggestions for Lambda aliases and how you might use
them:
Page 16
Amazon Web Services – Serverless Architectures with AWS Lambda
Creating a good, documented strategy for your use of function aliases enables
you to have sophisticated serverless deployment and operations practices.
IAM Role
AWS Identity and Access Management (IAM) provides the capability to create
IAM policies that define permissions for interacting with AWS services and
APIs.34 Policies can be associated with IAM roles. Any access key ID and secret
access key generated for a particular role is authorized to perform the actions
defined in the policies attached to that role. For more information about IAM
best practices, see this documentation.35
In the context of Lambda, you assign an IAM role (called an execution role) to
each of your Lambda functions. The IAM policies attached to that role define
what AWS service APIs your function code is authorized to interact with. There
are two benefits:
•
For the latest technical content, refer to the AWS
Your source code is decoupled from its own security posture. If a
Whittoepapers
developer attempts & Lambda
change your Guidesfunction
page:code to integrate
with a https://aws.amazon.com/whitepapers
service that the function doesn’t have access to, that integration
will fail due to the IAM role assigned to your function. (Unless they have
used IAM credentials that are separate from the execution role, you
should use static code analysis tools to ensure that no AWS credentials
are present in your source code).
It’s important to assign each of your Lambda functions a specific, separate, and
least-privilege IAM role. This strategy ensures that each Lambda function can
evolve independently without increasing the authorization scope of any other
Lambda functions.
Page 17
Amazon Web Services – Serverless Architectures with AWS Lambda
For pull model event sources (for example, Kinesis streams and DynamoDB
streams), you need to ensure that the appropriate actions are permitted by the
IAM execution role assigned to your Lambda function. AWS provides a set of
managed IAM roles associated with each of the pull-based event sources if you
don’t want to manage the permissions required. However, to ensure least
privilege IAM policies, you should create your own IAM roles with resource-
specific policies to permit access to just the intended event source.
Network Configuration
Executing your Lambda function occurs through the use of the Invoke API that
is part of the AWS Lambda service APIs; so, there is no direct inbound network
access to your function to manage. However, your function code might need to
integrate with external dependencies (internal or publically hosted web services,
AWS services, databases, etc.). A Lambda function has two broad options for
outbound network connectivity:
Page 18
Amazon Web Services – Serverless Architectures with AWS Lambda
However, if your use case requires private connectivity, use the VPC option with
Lambda. For deeper guidance if you plan to deploy your Lambda functions
within your own VPC, see this documentation.36
Environment Variables
Software Development Life Cycle (SDLC) best practice dictates that developers
separate their code and their config. You can achieve this by using environment
variables with Lambda. Environment variables for Lambda functions enable you
to dynamically pass data to your function code and libraries without making
changes to your code. Environment variables are key-value pairs that you create
and modify as part of your function configuration. By default, these variables
are encrypted at rest. For any sensitive information that will be stored as a
Lambda function environment variable, we recommend you encrypt those
values using the AWS Key Management Service (AWS KMS) prior to function
creation, storing the encrypted cyphertext as the variable value. Then have your
This paper has been archived
Lambda function decrypt that variable in memory at execution time.
Here are some examples of how you might decide to use environment variables:
For the latest technical content, refer to the AWS
• Whitepapers
Log settings (FATAL, & Guides
ERROR, INFO, DEBUG,page:
etc.)
• https://aws.amazon.com/whitepapers
Dependency and/or database connection strings and credentials
Each version of your Lambda function can have its own environment variable
values. However, once the values are established for a numbered Lambda
function version, they cannot be changed. To make changes to your Lambda
function environment variables, you can change them to the $LATEST version
and then publish a new version that contains the new environment variable
values. This enables you to always keep track of which environment variable
values are associated with a previous version of your function. This is often
important during a rollback procedure or when triaging the past state of an
application.
Page 19
Amazon Web Services – Serverless Architectures with AWS Lambda
A dead letter queue is either an SNS topic or SQS queue that you have
designated as the destination for all failed invocation events. If a failure event
occurs, the use of a dead letter queue allows you to retain just the messages that
failed to be processed during the event. Once your function is able to be invoked
again, you can target those failed events in the dead letter queue for
reprocessing. The mechanisms for reprocessing/retrying the function
This paper has been archived
invocation attempts placed on to your dead letter queue is up to you. For more
information about dead letter queues, see this tutorial.37 You should use dead
For the
letter queues latest
if it’s technical
important content,that
to your application refer to the AWS
all invocations of your
Lambda function complete eventually, even if execution is delayed.
Whitepapers & Guides page:
Timeout https://aws.amazon.com/whitepapers
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 at the time of this publication, which means a
single invocation of a Lambda function cannot execute longer than 300 seconds.
You should not always set the timeout for a Lambda function to the maximum.
There are many cases where an application should fail fast. Because your
Lambda function is billed based on execution time in 100-ms increments,
avoiding lengthy timeouts for functions can prevent you from being billed while
a function is simply waiting to timeout (perhaps an external dependency is
unavailable, you’ve accidentally programmed an infinite loop, or another similar
scenario).
Also, once execution completes or a timeout occurs for your Lambda function
and a response is returned, all execution ceases. This includes any background
Page 20
Amazon Web Services – Serverless Architectures with AWS Lambda
Page 21
Amazon Web Services – Serverless Architectures with AWS Lambda
The following is a brief list of serverless security best practices that should apply
to many serverless use cases, although your own specific security and
compliance requirements should be well understood and might include more
than we describe here.
This paper has been archived
• One IAM Role per Function
For the latest technical content, refer to the AWS
Each and every Lambda function within your AWS account should have
Whwith
a 1:1 relationship itepapers & Guides
an IAM role. page:functions begin with
Even if multiple
exactlyhttps://aws.amazon.com/whitepapers
the same policy, always decouple your IAM roles so that you can
ensure least privilege policies for the future of your function.
For example, if you shared the IAM role of a Lambda function that
needed access to an AWS KMS key across multiple Lambda functions,
then all of those functions would now have access to the same
encryption key.
You should not have any long-lived AWS credentials included within
your Lambda function code or configuration. (This is a great use for
static code analysis tools to ensure it never occurs in your code base!)
For most cases, the IAM execution role is all that’s required to integrate
with other AWS services. Simply create AWS service clients within your
code through the AWS SDK without providing any credentials. The SDK
automatically manages the retrieval and rotation of the temporary
Page 22
Amazon Web Services – Serverless Architectures with AWS Lambda
This code snippet is all that’s required for the AWS SDK for Java to
create an object for interacting with a DynamoDB table that
automatically sign its requests to the DynamoDB APIs, using the
temporary IAM credentials assigned to your function.39
However, there might be cases where the execution role is not sufficient
for the type of access your function requires. This can be the case for
some cross-account integrations your Lambda function might perform,
or if you have user-specific access control policies through combining
Amazon Cognito40 identity roles and DynamoDB fine-grained access
control.41 For cross-account use cases, you should grant your execution
role should be granted access to the AssumeRole API within the AWS
Security Token Service and integrated to retrieve temporary access
This paper has been archived
credentials.42
• Persisting Secrets
There are cases where you may have long-lived secrets (for example,
database credentials, dependency service access keys, encryption keys,
etc.) that your Lambda function needs to use. We recommend a few
options for the lifecycle of secrets management in your application:
Page 23
Amazon Web Services – Serverless Architectures with AWS Lambda
• Using Secrets
• VPC Security
Page 24
Amazon Web Services – Serverless Architectures with AWS Lambda
Keep in mind that these practices and policies impact the way that your
Lambda functions connect to their dependencies. Invoking a Lambda
function still occurs through event sources and the Invoke API (neither
are affected by your VPC configuration).
High Availability
High-availability is important for production applications. The availability
posture of your Lambda function depends on the number of Availability Zones it
can be executed in. If your function uses the default network environment, it is
Page 25
Amazon Web Services – Serverless Architectures with AWS Lambda
Fault Tolerance
If the application availability you need requires you to take advantage of
multiple AWS Regions, you must take this into account up front in your design.
It’s not a complex exercise to replicate your Lambda function code packages to
multiple AWS Regions. What can be complex, like most multi-region
application designs, is coordinating a failover decision across all tiers of your
application stack. This means you need to understand and orchestrate the shift
This paper has been archived
to another AWS Region—not just for your Lambda functions but also for your
event sources (and dependencies further upstream of your event sources) and
persistence layers. In the end, a multi-region architecture is very application-
For the latest technical content, refer to the AWS
specific. The most important thing to do to make a multi-region design feasible
is to account for it in Wh
your idesign
tepapers & Guides page:
up front.
https://aws.amazon.com/whitepapers
Recovery
Consider how your serverless application should behave in the event that your
functions cannot be executed. For use cases where API Gateway is used as the
event source, this can be as simple as gracefully handling error messages and
providing a viable, if degraded, user experience until your functions can be
successfully executed again.
For asynchronous use cases, it can be very important to still ensure that no
function invocations are lost during the outage period. To ensure that all
received events are processed after your function has recovered, you should take
advantage of dead letter queues and implement how to process events placed on
that queue after recovery occurs.
Page 26
Amazon Web Services – Serverless Architectures with AWS Lambda
The following graph shows how the ideal memory allocation to an example
function can allow for both better cost and lower latency. Here, the additional
compute cost per 100 ms for using 512 MB over the lower memory options is
outweighed by the amount of latency reduced in the function by allocating more
resources. But after 512 MB, the performance gains are diminished for this
Page 27
Amazon Web Services – Serverless Architectures with AWS Lambda
particular function’s logic, so the additional cost per 100 ms now drives the total
cost higher. This leaves 512 MB as the optimal choice for minimizing total cost.
The memory usage for your function is determined per invocation and can be
viewed in CloudWatch Logs.53 On each invocation a REPORT: entry is made, as
shown below.
This paper has been archived
REPORT RequestId: 3604209a-e9a3-11e6-939a-754dd98c7be3 Duration:
For the latest technical content, refer to the AWS
12.34 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory
Used: 18 MB
Whitepapers & Guides page:
https://aws.amazon.com/whitepapers
By analyzing the Max Memory Used: field, you can determine if your function
needs more memory or if you over-provisioned your function's memory size.
Page 28
Amazon Web Services – Serverless Architectures with AWS Lambda
If your application does not experience large peaks or valleys within its traffic
patterns, or does not have user experiences blocked on Lambda function
response times, we recommend you choose the language you’re already most
comfortable with.
Here are a few examples of how you can improve the performance of your
This paper has been archived
function code when a warm container is invoked:
• Keep alive and reuse connections (HTTP, database, etc.) that were
established during a previous invocation.
Finally, you should do the following to limit the amount of time that a cold start
takes for your Lambda function:
Page 29
Amazon Web Services – Serverless Architectures with AWS Lambda
3. Trim your function code package to only its runtime necessities. This
reduces the amount of time that it takes for your code package to be
downloaded from Amazon S3 ahead of invocation.
Understanding Your Application Performance
To get visibility into the various components of your application architecture,
which could include one or more Lambda functions, we recommend that you
use AWS X-Ray.54 X-Ray lets you trace the full lifecycle of an application request
through each of its component parts, showing the latency and other metrics of
each component separately, as shown in the following figure.
Logging
Each language runtime for Lambda provides a mechanism for your function to
deliver logged statements to CloudWatch Logs. Making adequate use of logs
goes without saying and isn’t new to Lambda and serverless architectures. Even
though it’s not considered best practice today, many operational teams depend
Page 30
Amazon Web Services – Serverless Architectures with AWS Lambda
• Create a custom metric and integrate directly with the API required from
your Lambda function as it’s executing. This has the fewest
dependencies and will record the metric as fast as possible. However, it
does require you to spend Lambda execution time and resources
integrating with another service dependency. If you follow this path,
ensure that your code for capturing metrics is modularized and reusable
across your Lambda functions instead of tightly coupled to a specific
Lambda function.
Page 31
Amazon Web Services – Serverless Architectures with AWS Lambda
• Capture the metric within your Lambda function code and log it using
the provided logging mechanisms in Lambda. Then, create a
CloudWatch Logs metric filter on the function streams to extract the
metric and make it available in CloudWatch. Alternatively, create
another Lambda function as a subscription filter on the CloudWatch
Logs stream to push filtered log statements to another metrics solution.
This path introduces more complexity and is not as near real-time as the
previous solution for capturing metrics. However, it allows your function
to more quickly create metrics through logging rather than making an
external service request.
Deployment
Performing a deployment in Lambda is as simple as uploading a new function code
package, publishing a new version, and updating your aliases. However, these steps
should only be pieces of your deployment process with Lambda. Each deployment
process is application-specific. To design a deployment process that avoids negatively
disrupting your users or application behavior, you need to understand the relationship
between each Lambda function and its event sources and dependencies. Things to
consider are:
This paper has been archived
• Parallel version invocations – Updating an alias to point to a new
version of a Lambda function happens asynchronously on the service
For
side.the latest
There will betechnical content,
a short period of time thatrefer tofunction
existing the AWS
containers containing the previous
Whitepapers & source
Guides code package will continue to
page:
be invoked alongside the new function version the alias has been
updatedhttps://aws.amazon.com/whitepapers
to. It’s important that your application continues to operate as
expected during this process. An artifact of this might be that any stack
dependencies being decommissioned after a deployment (for example,
database tables, a message queue, etc.) not be decommissioned until
after you’ve observed all invocations targeting the new function version.
Page 32
Amazon Web Services – Serverless Architectures with AWS Lambda
important for your processes to roll back to the function version that was
previously deployed.
Load Testing
Load test your Lambda function to determine an optimum timeout value. It’s
important to analyze how long your function runs so that you can better
determine any problems with a dependency service that might increase the
concurrency of the function beyond what you expect. This is especially
important when your Lambda function makes network calls to resources that
may not handle Lambda’s scaling.
Right-Sizing
As covered in Performance Efficiency, it’s an anti-pattern to assume that the
smallest resource size available to your function will provide the lowest total
cost. If your function’s resource size is too small, you could pay more due to a
longer execution time than if more resources were available that allowed your
function to complete more quickly.
See the section Choosing the Optimal Memory Size for more details.
Page 33
Amazon Web Services – Serverless Architectures with AWS Lambda
Batch Size
Some Lambda event sources allow you to define the batch size for the number of
records that are delivered on each function invocation (for example, Kinesis and
DynamoDB). You should test to find the optimal number of records for each
batch size so that the polling frequency of each event source is tuned to how
quickly your function can complete its task.
Page 34
Amazon Web Services – Serverless Architectures with AWS Lambda
The AWS Serverless Application Model (AWS SAM) enables you to have a
simpler experience when building serverless applications and get the benefits of
infrastructure as code. AWS SAM is an open specification abstraction layer on
top of AWS CloudFormation.57 It provides a set of command line utilities that
enable you to define a full serverless application stack with only a handful of
lines of JSON or YAML, package your Lambda function code together with that
infrastructure definition, and then deploy them together to AWS. We
recommend using AWS SAM combined with AWS CloudFormation to define
This paper has been archived
and make changes to your serverless application environment.
For
There is the latest
a distinction, technical
however, betweencontent,
changes thatrefer
occurtat
o the
the AWS
infrastructure/environment level and application code changes occurring within
Whitepapers & Guides page:
existing Lambda functions. AWS CloudFormation and AWS SAM aren’t the only
tools requiredhttps://aws.amazon.com/whitepapers
to build a deployment pipeline for your Lambda function code
changes. See the CI/CD section of this whitepaper for more recommendations
about managing code changes for your Lambda functions.
Page 35
Amazon Web Services – Serverless Architectures with AWS Lambda
debugger, and quickly iterate changes without having to deploy a new code
package to AWS.
The following example (written in Java) shows poor practices where the core
business logic of an application is tightly coupled to Lambda. In this example,
the business logic is created within the handler method and depends directly on
Lambda event source objects.
Page 36
Amazon Web Services – Serverless Architectures with AWS Lambda
Warm Containers—Caching/Keep-Alive/Reuse
This paper has been archived
As mentioned earlier, you should write code that takes advantage of a warm
function container. This means scoping your variables in a way that they and
For the
their contents canlatest technical
be reused content,
on subsequent refer
invocations to possible.
where the AWS This is
especially impactful for
Wh things like bootstrapping
itepapers & Guides configuration, keeping
page:
external dependency connections open, or one-time initialization of large
https://aws.amazon.com/whitepapers
objects that can persist from one invocation to the next.
Control Dependencies
The Lambda execution environment contains many libraries such as the AWS
SDK for the Node.js and Python runtimes. (For a full list, see the Lambda
Execution Environment and Available Libraries.59) To enable the latest set of
features and security updates, Lambda periodically updates these libraries.
These updates can introduce subtle changes to the behavior of your Lambda
function. To have full control of the dependencies your function uses, we
recommend packaging all your dependencies with your deployment package.
Trim Dependencies
Lambda function code packages are permitted to be at most 50 MB when
compressed and 250 MB when extracted in the runtime environment. If you are
including large dependency artifacts with your function code, you may need to
Page 37
Amazon Web Services – Serverless Architectures with AWS Lambda
trim the dependencies included to just the runtime essentials. This also allows
your Lambda function code to be downloaded and installed in the runtime
environment more quickly for cold starts.
Fail Fast
Configure reasonably short timeouts for any external dependencies, as well as a
reasonably short overall Lambda function timeout. Don’t allow your function to
spin helplessly while waiting for a dependency to respond. Because Lambda is
billed based on the duration of your function execution, you don’t want to incur
higher charges than necessary when your function dependencies are
unresponsive.
Handling Exceptions
You might decide to throw and handle exceptions differently depending on your
use case for Lambda. If you’re placing an API Gateway API in front of a Lambda
function, you may decide to throw an exception back to API Gateway where it
might be transformed, based on its contents, into the appropriate HTTP status
code and message for the error that occurred. If you’re building an
asynchronous data processing system, you might decide that some exceptions
This paper has been archived
within your code base should equate to the invocation moving to the dead letter
queue for reprocessing, while other errors can just be logged and not placed on
the dead letter queue. You should evaluate what your decide failure behaviors
For the latest technical content, refer to the AWS
are and ensure that you are creating and throwing the correct types of
Wh
exceptions within your itepapers
code & Guides
to achieve that behavior.page:
To learn more about
handling exceptions, see the following for details about how exceptions are
https://aws.amazon.com/whitepapers
defined for each language runtime environment:
• Java60
• Node.js61
• Python62
• C#63
Code Management Best Practices
Now that the code you’ve written for your Lambda functions follows best
practices, how should you manage that code? With the development speed that
Lambda enables, you might be able to complete code changes at a pace that is
unfamiliar for your typical processes. And the reduced amount of code that
serverless architectures require means that your Lambda function code
Page 38
Amazon Web Services – Serverless Architectures with AWS Lambda
represents a large portion of what makes your entire application stack function.
So having good source code management of your Lambda function code will
help ensure secure, efficient, and smooth change management processes.
The main purpose of organizing your code this way is to help ensure that all of
the code that contributes to the code package of a particular Lambda function is
independently versioned and committed to, and defines its own dependencies
This paper has been archived
and those dependencies’ versions. Each Lambda function should be fully
decoupled from a source code perspective from other Lambda functions, just as
it will be when it’s deployed. You don’t want to go through the process of
For the latest technical content, refer to the AWS
modernizing an application architecture to be modular and decoupled with
Lambda only to be left Wh itepapers
with a monolithic&and
Guides page: code base.
tightly coupled
https://aws.amazon.com/whitepapers
Release Branches
We recommend that you create a repository or project branching strategy that
enables you to correlate Lambda function deployments with incremental
commits on a release branch. If you don’t have a way to confidently correlate
source code changes within your repository and the changes that have been
deployed to a live Lambda function, an operational investigation will always
begin with trying to identify which version of your code base is the one currently
deployed. You should build a CI/CD pipeline (more recommendations for this
later) that allows you to correlate Lambda code package creation and
deployment times with the code changes that have occurred with your release
branch for that Lambda function.
Page 39
Amazon Web Services – Serverless Architectures with AWS Lambda
Testing
Time spent developing thorough testing of your code is the best way to ensure
quality within a serverless architecture. However, serverless architectures will
enforce proper unit testing practices perhaps more than you’re used to. Many
developers use unit test tools and frameworks to write tests that cause their
code to also test its dependencies. This is a single test that combines a unit test
and an integration test but that doesn’t perform either very well.
It’s important to scope all of your unit test cases down to a single code path
within a single logical function, mocking all inputs from upstream and outputs
from downstream. This allows you to isolate your test cases to only the code that
you own. When writing unit tests, you can and should assume that your
dependencies behave properly based on the contracts your code has with them
as APIs, libraries, etc.
It’s similarly important for your integration tests to test the integration of your
code to its dependencies in an environment that mimics the live environment.
Testing whether a developer laptop or build server can integrate with a
downstream dependency isn’t fully testing if your code will integrate
This paper has been archived
successfully once in the live environment. This is especially true of the Lambda
environment, where you code doesn’t have ownership of the events that are
going toFor the latest
be delivered technical
by event content,
sources and you don’trefer toability
have the the AWS
to create
the Lambda runtime environment outside of Lambda.
Whitepapers & Guides page:
Unit Tests https://aws.amazon.com/whitepapers
With what we’ve said earlier in mind, we recommend that you unit test your
Lambda function code thoroughly, focusing mostly on the business logic outside
your handler function. You should also unit test your ability to parse
sample/mock objects for the event sources of your function. However, the bulk
of your logic and tests should occur with mocked objects and functions that you
have full control over within your code base. If you feel that there are important
things inside your handler function that need to be unit tested, it can be a sign
you should encapsulate and externalize the logic in your handler function
further. Also, to supplement the unit tests you’ve written, you should create
local test automation using AWS SAM Local that can serve as local end-to-end
testing of your function code (note that this isn’t a replacement for unit testing).
Page 40
Amazon Web Services – Serverless Architectures with AWS Lambda
Integration Testing
For integration tests, we recommend that you create lower lifecycle versions of
your Lambda functions where your code packages are deployed and invoked
through sample events that your CI/CD pipeline can trigger and inspect the
results of. (Implementation depends on your application and architecture.)
Continuous Delivery
We recommend that you programmatically manage all of your serverless
deployments through CI/CD pipelines. This is because the speed with which you
will be able to develop new features and push code changes with Lambda will
allow you to deploy much more frequently. Manual deployments, combined
with a need to deploy more frequently, often result in both the manual process
becoming a bottleneck and prone to error.
AWS CodeBuild – Can be used for the build state of your pipeline. Use it to
build your code, execute unit tests, and create a new Lambda code package.
Then, integrate with AWS SAM to push your code package to Amazon S3 and
push the new package to Lambda via AWS CloudFormation.
Page 41
Amazon Web Services – Serverless Architectures with AWS Lambda
After your new version is published to your Lambda function through AWS
CodeBuild, you can automate your subsequent steps in your AWS CodePipeline
pipeline by creating deployment-centric Lambda functions. They will own the
logic for performing integration tests, updating function aliases, determining if
immediate rollbacks are necessary, and any other application-centric steps
needed to occur during a deployment for your application (like cache flushes,
notification messages, etc.). Each one of these deployment-centric Lambda
functions can be invoked in sequence as a step within your AWS CodePipeline
pipeline using the Invoke action. For details on using Lambda within AWS
CodePipeline, see this documentation.64
In the end, each application and organization has its own requirements for
moving source code from repository to production. The more automation you
can introduce into this process, the more agility you can achieve using Lambda.
Conclusion
Building serverless applications on AWS relieves you of the responsibilities and
constraints that servers introduce. Using AWS Lambda as your serverless logic
layer enables you to build faster and focus your development efforts on what
differentiates your application. Alongside Lambda, AWS provides additional
serverless capabilities so that you can build robust, performant, event-driven,
reliable, secure, and cost-effective applications. Understanding the capabilities
and recommendations described in this whitepaper can help ensure your
Page 42
Amazon Web Services – Serverless Architectures with AWS Lambda
Contributors
The following individuals and organizations contributed to this document:
Notes
1 https://aws.amazon.com/lambda/
2 https://aws.amazon.com/api-gateway/
This paper has been archived
3 https://aws.amazon.com/s3/
4 https://aws.amazon.com/dynamodb/
For the latest technical content,
refer to the AWS
5 https://aws.amazon.com/sns/
Whitepapers & Guides page:
6 https://aws.amazon.com/sqs/
https://aws.amazon.com/whitepapers
7 https://aws.amazon.com/step-functions/
8
https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/WhatIsCloud
WatchEvents.html
9 https://aws.amazon.com/kinesis/
10 http://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-
function.html
11 http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html
12 http://docs.aws.amazon.com/lambda/latest/dg/get-started-create-
function.html
13 https://github.com/awslabs/aws-serverless-workshops
Page 43
Amazon Web Services – Serverless Architectures with AWS Lambda
14 https://aws.amazon.com/blogs/compute/scripting-languages-for-aws-
lambda-running-php-ruby-and-go/
15 http://docs.aws.amazon.com/lambda/latest/dg/current-supported-
versions.html
16 https://github.com/awslabs/aws-sam-local
17 http://docs.aws.amazon.com/lambda/latest/dg/limits.html
18 http://docs.aws.amazon.com/lambda/latest/dg/API_CreateFunction.html
19
http://docs.aws.amazon.com/lambda/latest/dg/API_UpdateFunctionCode.ht
ml
20 http://docs.aws.amazon.com/lambda/latest/dg/java-programming-
model.html
21 http://docs.aws.amazon.com/lambda/latest/dg/programming-model.html
22 http://docs.aws.amazon.com/lambda/latest/dg/python-programming-
model.html
This paper has been archived
23 http://docs.aws.amazon.com/lambda/latest/dg/dotnet-programming-
model.html
24 For the latest technical content, refer to the AWS
http://docs.aws.amazon.com/lambda/latest/dg/java-logging.html
25 Whitepapers & Guides page:
http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-
logging.htmlhttps://aws.amazon.com/whitepapers
26 http://docs.aws.amazon.com/lambda/latest/dg/python-logging.html
27 http://docs.aws.amazon.com/lambda/latest/dg/dotnet-logging.html
28 http://docs.aws.amazon.com/lambda/latest/dg/programming-model-
v2.html
29 http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html
30 http://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-
function.html
31 http://docs.aws.amazon.com/lambda/latest/dg/API_PublishVersion.html
32
http://docs.aws.amazon.com/lambda/latest/dg/API_UpdateFunctionCode.ht
ml
33 http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html
Page 44
Amazon Web Services – Serverless Architectures with AWS Lambda
34 http://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html
35 http://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html
36 http://docs.aws.amazon.com/lambda/latest/dg/vpc.html
37 https://aws.amazon.com/blogs/compute/robust-serverless-application-
design-with-aws-lambda-dlq/
38 http://d0.awsstatic.com/whitepapers/architecture/AWS_Well-
Architected_Framework.pdf
39 https://aws.amazon.com/sdk-for-java/
40 https://aws.amazon.com/cognito/
41
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/speci
fying-conditions.html
42
http://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetC
This paper has been archived
redentialsForIdentity.html
Page 45
Amazon Web Services – Serverless Architectures with AWS Lambda
52 http://docs.aws.amazon.com/lambda/latest/dg/vpc.html#vpc-setup-
guidelines
53
http://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIs
CloudWatchLogs.html
54 http://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html
55 http://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html
56 https://github.com/awslabs/serverless-application-model
57 https://github.com/awslabs/serverless-application-model
58 https://github.com/awslabs/aws-sam-local
59 http://docs.aws.amazon.com/lambda/latest/dg/current-supported-
versions.html
60 http://docs.aws.amazon.com/lambda/latest/dg/java-exceptions.html
61 http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-mode-
exceptions.htmlThis paper has been archived
62 http://docs.aws.amazon.com/lambda/latest/dg/python-exceptions.html
63 http://docs.aws.amazon.com/lambda/latest/dg/dotnet-exceptions.html
For the latest technical content, refer to the AWS
64 http://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-
Whitepapers & Guides page:
lambda-function.html
https://aws.amazon.com/whitepapers
65 https://aws.amazon.com/codestar/
66 https://github.com/awslabs/aws-serverless-workshops
67 https://aws.amazon.com/serverless/
Page 46