This book is presented solely as-is and for educational purposes only. The author makes no claims to the accuracy of the information presented within. The code samples and tutorials are provided as complements to the book and may change at any time. All references to Amazon, Amazon Web Services, AWS, Lambda, or other Amazon Web Services affiliated products, as well as screenshots and code examples are done so for purposes of reference and do not imply trademark or other rights held by the author of this book.
1. Introduction
2. A Different Kind of Workload
3. This Book
4. Lambda Background
5. Hello World
6. Working with Events
7. The Context Object
8. Roles and Permissions
9. Dependencies and Resources
10. Logging
11. Testing Your Function
12. Hello S3 Object
13. When Lambda Isnt the Answer
14. Where Lambda Excels
15. Real-World Use Cases
16. Execution Environment
17. From Development to Deployment
18. Versioning and Aliasing
19. Costs
20. CloudFormation
21. AWS API Gateway
22. Lambda Competitors
23. The Future of Lambda
24. More Resources
25. Conclusion
1. Introduction
Software developers, developer operations engineers, systems administrators, and other IT workers are well aware that the process of running software usually requires significant forethought into how and where that software will run. Traditionally, companies required servers; physical boxes lined row upon row of a data center, each requiring the attention of an administrator or teams of engineers. Larger shops could easily employ a dozen team members to handle the hardware, security, networking, upgrades, and all other aspects of the life of a server.
As a reader of this book, you are most likely familiar with where the industry trended next: the cloud. Rather rapidly in the last few years, existing businesses have transferred large swaths of their networks to cloud providers, while new businesses have been built from the ground up solely relying on cloud-based infrastructure. Cloud providers like Google, DigitalOcean, Rackspace, and Amazon have surged in popularity, each providing a means of offloading the traditional tasks of hardware management. Despite the reluctance of some large companies, most industry experts agree that cloud-based environments (or, at the very least, hybrid environments) are the way of the future.
Without getting into the pros and cons of vendor lock-in, each of these cloud hosting providers has worked continuously to provide cloud-based solutions for traditional problems. Storage solutions, networking capabilities, direct data center connections, and computing power have been developed and continually improved to address the needs of thousands of companies that are now relying on these public cloud environments. With each passing year, more and more computing services are being offered by these companies.
The single largest entity of these cloud providers is Amazon. Its Amazon Web Services (AWS) business is now larger than all of its competitors combined and is growing at an incredible pace. With over 45 different offerings, AWS provides solutions that span storage, networking, computing, machine learning, and mobile device testing. One of the oldest products in the AWS catalog is Elastic Compute Cloud, or EC2, which allows developers to launch virtual server instances on-demand. After declaring parameters such as the amount of memory, disk space, and CPU size, administrators can provision a single virtual instance or up to thousands of running instances at once. The administrators and developers are then responsible for deploying code, running the applications, and ensuring that the instances remain healthy (are not consuming too much memory, disk space, etc.).
EC2 has been, and remains one of AWSs most popular core offerings. With root or administrator access to a base Linux or Windows machine, developers are free to run almost any kind of code imaginable. Millions of applications have been developed on top of EC2, and entire businesses (such as Heroku) use EC2 for their underlying infrastructure. In fact, using nothing but EC2, most organizations could run their entire workloads including storage, database, and routing needs. The availability and performance of EC2 on AWS is unparalleled.
2. A Different Kind of Workload
EC2 quickly became an extremely popular option for companies looking to run websites, manage databases, and balance traffic. As AWS grew as a business, so did the number of websites powered by its EC2 platform. Huge names like Netflix and Instagram are, or were for some time, running a majority of their sites and operations on AWS-powered services. Hundreds of thousands of domains can be resolved to an EC2 host.
At some point around late 2012 or early 2013, developers and managers at Amazon realized that, while a bulk of EC2 usage was web-related, an entire other category of usage existed: event-driven processing. Many companies were turning to EC2 to react to events in other parts of the infrastructure: images saved by users of a web service would trigger EC2 instances to automatically generate thumbnails; new log files being delivered would cause EC2 instances to process and make sense of the data; databases updates launched EC2 instances to process these changes and apply updates elsewhere. Essentially, EC2 was being used in a chain of events that could contain many moving parts, most utilizing additional AWS services.
To respond to this demand, AWS announced another product during the keynote of its 2014 Re:Invent conference in Las Vegas: AWS Lambda. According to Amazon, AWS Lambda is a compute service that runs your code in response to events and automatically manages the compute resources for you, making it easy to build applications that respond quickly to new information.
The announcement of Lambda was a huge game changer for many organizations that have traditionally relied on EC2 to process events, resize images, respond to database updates, or react to user clicks or website updates. Under the EC2 model, a developer and/or operations staff must: design code, deploy resources, configure event hooks, manage those resources, implement monitoring, respond to downtime, plan for scale, and react to surges in traffic with proper scaling policies. With Lambda, a developer only needs to: design code, configure a few options, and release the application.
Despite these advantages, Lambda is not yet designed for workloads that are not primarily event-driven. For example, Lambda functions cannot respond directly to HTTP requests (unless paired with AWS API Gateway, another recently-announced service), or run long-running processes in the background. Given their low timeouts (currently 300 seconds) and moderate memory allowances (up to 1.5 GB at the time of writing this book), Lambda functions are not designed to completely replace EC2; instead, they can replace some of the functionality for specific services and support an entirely new form of computing in the cloud.
3. This Book
This book will focus on AWS Lambda from the viewpoint of an AWS user who has not yet used the service. Hopefully you are familiar with AWS terms and concepts, and have launched projects on AWS before. While it is not necessary that you are a certified AWS expert, some hands-on experience will be helpful as we dive into the AWS Lambda service. With that in mind, I will assume that you have not used Lambda before, or have very little knowledge of it. Ill do my best to explain the terms and functionality surrounding it in a beginner-focused way.