SAM无服务器隐式API与AWS :: Serverless :: Api [英] SAM Serverless implicit API vs AWS::Serverless::Api

查看:130
本文介绍了SAM无服务器隐式API与AWS :: Serverless :: Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在配置SAM模板并定义 AWS :: Serverless :: Function 时,有一些Event参数接受Api类型。这会创建API网关资源吗?此事件类型和独立的 AWS :: Serverless :: Api 资源之间有什么区别?

When configuring a SAM template and defining a AWS::Serverless::Function there is the Events param that accepts an Api type. Does this create an API Gateway resource? What is the difference between this event type and a standalone AWS::Serverless::Api resource?

推荐答案

问题询问有关SAM AWS :: Serverless :: Function类型的事件源块中引用的API,例如:

The question asks about the APIs referred to in the Event source block of a SAM AWS::Serverless::Function type, such as:

MyFunction:
  Type: AWS::Serverless::Function
  Properties:
    ...
    Events:
      MyApi:
        Type: Api
        Properties:
          Path: /resource
          Method: GET

正如文档在不同地方所提到的那样,它们在SAM中被称为隐式API。

As mentioned in the docs in various places, these are called "implicit APIs" in SAM.

SAM创建类型为AWS :: Serverless的资源:: Api来自在AWS :: Serverless :: Function资源上定义的Api事件的并集-但只有那些未引用(通过RestApiId属性)到模板中显式定义的AWS :: Serverless :: Api的事件。

SAM creates resources of type AWS::Serverless::Api from the union of Api events defined on AWS::Serverless::Function resources - but only those that do not refer (via the RestApiId property) to AWS::Serverless::Api defined explicitly in the template.

在后台,SAM收集所有这些隐式API,生成Swagger,并使用此Swagger创建隐式API。该API默认为一个无法配置的名为 Prod的StageName。

Behind the scenes, SAM collects all of these implicit APIs, generates a Swagger, and creates the implicit APIs using this Swagger. This API defaults to a StageName called "Prod" which cannot be configured.

如果您确实需要控制在Swagger中定义和记录API,则可以使用AWS :: Serverless: :Api资源应显式创建。然后将以这种方式进行引用:

If you do need control over defining and documenting the API in Swagger, an AWS::Serverless::Api resource should be created explicitly. It would then be referred to this way:

MyFunction:
  Type: AWS::Serverless::Function
  Properties:
    ...
    Events:
      MyApi:
        Type: Api
        Properties:
          Path: /resource
          Method: GET
          RestApiId: !Ref MyAPI  # Add this line

MyApi:
  Type: AWS::Serverless::Api
  Properties:
    StageName: Prod
    DefinitionBody:
      ...

所以它们之间的唯一区别是您对它们的配置有很大的控制权,而关键的考虑因素是您是否需要定义以下两者之一:

So the only difference between them is how much control you have over their configuration, and the key consideration is whether or not you need to define either:


  • StageName

  • 一个Swagger定义(通过DefinitionBody)

如果您需要控制这两个或任何一个,则需要明确定义您的API。否则,您可能可以使用隐式API。

If you need control over either or both of these, then you need to define your API explicitly. Otherwise, you can probably use the implicit APIs.

还请注意,SAM中的AWS :: Serverless :: Api资源被转换为类型为AWS ::的CloudFormation资源。 ApiGateway :: RestApi,AWS :: ApiGateway :: Stage和AWS :: ApiGateway :: Deployment。

Note also that AWS::Serverless::Api resources in SAM are "transformed" into CloudFormation resources of type AWS::ApiGateway::RestApi, AWS::ApiGateway::Stage, and AWS::ApiGateway::Deployment.

请注意,此信息是这三个信息的摘要。源文档:

Note that this information is a summary of information found in these three source docs:

  • https://github.com/awslabs/serverless-application-model/blob/develop/versions/2016-10-31.md#awsserverlessapi
  • https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  • https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst#implicit-apis

这篇关于SAM无服务器隐式API与AWS :: Serverless :: Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆