Serverless 简明教程

Serverless - Service

您不会希望为部署的每个函数创建一个单独的 serverless.yml 文件。那会非常乏味。幸运的是,无服务器提供了在相同的 serverless.yml 文件中部署多个函数的条款。所有这些函数都属于一个名为“服务”的组。服务名称通常是在 serverless.yml 文件中定义的第一件事。

You wouldn’t want to create a separate serverless.yml file for each function that you deploy. That would be very tedious. Luckily, serverless has provisions to deploy multiple functions within the same serverless.yml file. All these functions belong to a single group called 'service'. The service name is often the first thing defined in a serverless.yml file.

service: my-first-service

provider:
   name: aws
   runtime: python3.6
   stage: prod
   region: us-east-2
   profile: yash-sanghvi

   functions:
      func1:
      handler: handler1.func1

   func2:
      handler: handler2.func2

服务中的所有函数在部署后,在 AWS Lambda 控制台上的名称格式如下 − service_name-stage_name-function_name 。因此,上述示例中的两个函数在部署后将采用以下名称 − my-first-service-prod-func1my-first-service-prod-func2 。stage 参数可帮助您区分代码开发的不同阶段。

All the functions within a service, when deployed, take the following name format on the AWS Lambda console − service_name-stage_name-function_name. Thus, the two functions in the example above,when deployed, will take the names − my-first-service-prod-func1 and my-first-service-prod-func2. The stage parameter helps you differentiate within the different stages of code development.

因此,如果您的函数处于开发阶段,您可能使用 stage dev ;如果它处于测试阶段,您可能使用 stage test ;如果它处于生产中,您可能使用 stage prod 。这样,您可以确保对 dev 阶段所做的更改不会影响生产代码。阶段名称没有固定定义。 dev, test, prod 仅是示例。

Thus, if your function is in the development stage, you may use the stage dev; if it is in the testing stage, you may use the stage test; if it is in production, you may use the stage prod. This way, you can ensure that changes made to the dev stage don’t affect the production codes. The stage names are not set in stone. dev, test, prod are just examples.

您可以使用您选择的任何阶段名称。请注意,如果您触发了 API Gateway lambdas(在后面的章节中会有更多信息),则每个阶段的端点都将不同。

You can have any stage name of your choice. Please note that if you have API Gateway triggered lambdas (more on that in a later chapter), then your endpoints will be different for each stage.

此外,如果您转到 AWS Lambda 控制台较少使用的“Applications”部分,您将能够看到整个服务的阶段。

Additionally, if you go to the lesser-used 'Applications' section of the AWS Lambda Console, you will be able to see the entire service with the stage.

aws lambda

如果您单击所选服务的阶段组合,您将能够在一个地方看到服务使用的所有资源 − Lambda 函数、API 网关、Events 规则、日志组、S3 存储桶,一切。

If you click on the service and stage combination of your choice, you will be able to see all the resources used by the service in one place − Lambda functions,API Gateways, Events Rules, Log groups, S3 buckets, everything.

hello world python dev

更有趣的是,您可以转到 Monitoring 选项卡,查看服务整体的性能 → 调用次数、平均持续时间、错误计数等。您可以了解哪些函数对您的账单贡献最大。当您的服务中有多个功能时,监控每个单独功能的性能变得非常困难。服务级别的 Monitoring 选项卡在此处提供了很大帮助。

What’s even more interesting is that you can go to the Monitoring tab and see the performance of your service as a whole → Number of invocations, average duration, error count, etc. You can get an idea of which function is contributing the most to your bill. When you have several functions within your service, monitoring the performance of each individual function becomes quite difficult. The service-level Monitoring Tab helps a lot here.

deployments

最后,Deployments 选项卡可帮助您查看服务的过去所有部署以及部署的状态。

Finally, the Deployments tab helps you see all the past deployments of the service and the status of the deployments.

monitoring