Serverless 简明教程

Serverless - Introduction

What is Serverless?

嗯,这个名字给你提供了一些提示。无需维护服务器就能进行计算 - 这就是无服务器计算(或简称无服务器)的关键。这一概念极具革命性和颠覆性。它已被广泛采用。一些新的应用程序从设计无服务器后端开始,而具有专用服务器的遗留应用程序也在慢慢迁移到无服务器架构。那么,是什么导致了无服务器的广泛采用?与所有事情一样,经济因素使无服务器变得非常有利。

Well, the name gives you quite some hints. Computation without the headache of maintaining a server − that’s the crux of serverless computing (or serverless in short). The concept is quite revolutionary and disruptive. It has been widely adopted. Several fresh applications begin by designing a serverless backend, and the legacy applications with dedicated servers are also slowly migrating to the serverless architecture. So what has led to the widespread adoption of serverless? As with everything, the economics have made serverless quite favorable.

你看,使用无服务器时,你只需为你使用的内容付费。想象一下你每天都需要对数据库执行一些例行维护。此过程每天可能需要花费大约 10 分钟。

You see, with serverless, you only pay for what you use. Imagine you need to perform some routine maintenance on your database every day. This process may take about 10 minutes every day.

现在,在没有无服务器计算的情况下,你的维护 cron 可能驻留在服务器中。除非在剩余时间里让服务器做其他事情,否则你可能最终为一项花费 10 分钟的任务支付 24 小时的费用。相当浪费钱,对吧?如果有人告诉你有一项新服务可以为你执行维护 cron 所需的 10 分钟精确收费,你会不会想简单地切换到那项新服务?这正是无服务器采用如此迅速和广泛的原因。它降低了多个组织的后端费用,还减少了服务器维护遇到的难题。

Now, in the absence of serverless computing, your maintenance cron may be residing in a server. Unless you have something else to do with your server in the remaining time, you may end up paying for 24 hours, for a task that takes 10 minutes. Quite a waste of money, right? What if you were told that there is a new service that will charge you exactly for those 10 minutes that your maintenance cron takes to execute? Won’t you want to simply switch to that new service? That’s exactly why serverless adoption has been so swift and widespread. It has brought down the backend bills for several organizations, and also reduced their server maintenance headache.

云服务提供商(AWS、Azure 等)不必担心在需要时恰好提供所需数量的无服务器应用程序。因此,在高负载期间,你可以调用多个无服务器应用程序,而在正常负载期间,你可以调用单个应用程序。当然,你仅需为高负载期间额外的调用付费。

The cloud service provider (AWS, Azure, etc.) takes the headache of making sure that the serverless application is available exactly when required, and in the quantity required. Thus, during high loads, you may invoke multiple serverless applications, while you may invoke a single application during normal load. And of course,you will pay for the extra invocations only for the duration of the high load.

What is Serverless, again?

上面解释的概念似乎很棒,但你该如何实现它?你为此需要一个框架。它被称为 serverless

The concept explained above seems great, but how do you implement it? You need a framework for that. It is called, uh, serverless.

无服务器框架帮助我们开发和部署设计为以无服务器方式运行的函数/应用程序。该框架向前迈进了一步,并负责部署运行我们的无服务器函数所需的整个堆栈。什么是堆栈?堆栈包括部署、存储和监控无服务器应用程序所需的所有资源。

The serverless framework helps us develop and deploy functions/ applications designed to run in a serverless fashion. The framework goes a step ahead and takes care of the deployment of the entire stack required for our serverless functions to run. What is a stack? Well, the stack comprises of all the resources that you will require for deploying, storing, and monitoring your serverless applications.

它包括实际函数/应用程序、存储容器、监控解决方案等等。例如,在 AWS 的上下文中,你的堆栈将包括你的实际 Lambda 函数、函数文件 S3 存储桶、链接到函数的 Cloudwatch 资源等。无服务器框架为我们创建了整个堆栈。这使我们可以完全专注于我们的功能。无服务器无需维护服务器并无服务器(框架)无需创建和部署运行我们函数所需的堆栈。

It includes the actual function/ application, storage containers, monitoring solutions, and a lot more.For example, in the context of AWS, your stack will consist of your actual Lambda function, S3 bucket for your function files, Cloudwatch resources linked to your function, and so on.The serverless framework creates this entire stack for us. This allows us to focus completely on our function. Serverless takes away the headache of maintaining a server and serverless (framework) takes away the headache of creating and deploying the stack necessary to run our function.

无服务器框架还负责为我们的功能/应用程序分配必要的权限。有些应用程序(我们将在本教程中看到示例)甚至需要将数据库链接到它们。无服务器框架再次负责创建和链接数据库。无服务器如何知道在堆栈中包含什么以及提供哪些权限?所有这些内容都将在 serverless.yml 文件中提及,这是本教程的重点。在后面的章节中会有更多内容。

The serverless framework also takes care of assigning the necessary permissions to our functions/ applications.Some applications (examples of which we will see in this tutorial) even require databases to be linked to them. Serverless framework again takes care of creating and linking the DBs. How does serverless know what to include in the stack and which permissions to provide? All of it is mentioned in the serverless.yml file, which will be our main focus in this tutorial. More on it in the upcoming chapters.

Serverless in AWS

AWS 的许多服务都属于“无服务器计算”的范畴。你可以找到整个有组织的列表 here 。有计算服务、集成服务,甚至还有数据存储服务(是的,AWS 甚至有无服务器数据库)。在整个教程中,我们将重点关注 AWS Lambda 函数。那么什么是 AWS Lambda?AWS Lambda 网站将其定义如下 -

There are many services of AWS that fall under the umbrella of 'serverless computing'. You can find the entire organized list here. There are Compute services, Integration services, even Data Storage services (yes, AWS even has Serverless DBs). We will be focusing on the AWS Lambda functions throughout the tutorial. So what is AWS Lambda? The AWS Lambda website defines it as follows −

AWS Lambda 是一种无服务器计算服务,可让你在无需配置或管理服务器、创建与工作负载感知的集群扩展逻辑、维护事件集成或管理运行时的情况下运行代码。

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes.

用外行术语来说,AWS Lambda 是你在 AWS 上进行无服务器计算的窗口。AWS Lambda 正是让无服务器概念如此流行的原因。你只需定义你的函数和触发器,然后你的函数将在你希望它被调用的时间内被调用,并且你只需为你函数执行所需的时间付费就行了。更重要的是,你可以将 AWS Lambda 与 AWS 提供的几乎所有其他服务链接起来 - EC2、S3、dynamoDB 等等。

In layperson terms, AWS Lambda is your window to serverless computing on AWS. It is AWS Lambda that has made the serverless concept so popular. All you need to do is define your function and the trigger to your function, and the function will be invoked exactly when you want it to be invoked, and you will be charged only for the time the function takes to execute. What’s more,you can link AWS Lambda with almost every other service that AWS provides − EC2, S3, dynamoDB, and so on.

因此,如果你已经是 AWS 生态系统的一部分,那么 Lambda 集成很无缝。如果你像我刚了解 AWS Lambda 时那样的 AWS 生态系统新手,它将成为进入 AWS 宇宙的一个好门户。

Therefore, if you have already been a part of the AWS ecosystem, then Lambda integration is quite seamless. If you are new to the AWS ecosystem like I was when I first learned about AWS Lambda, it will act as a good gateway to the AWS universe.

在本教程中,我们将学习使用无服务器框架部署 AWS Lambda 函数。是否感到激动?那么继续阅读下一章节来开始吧。

In this tutorial, we will be learning all about the deployment of AWS Lambda functions using the serverless framework. Are you excited? Then move on to the next chapter to get started.