0

我正在使用有关如何创建 RESTful CRUD API的 AWS 官方教程AWS::Serverless::Api,并且 YML 模板不包含 AWS声明。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  Example template for an HTTP API that creates, updates, and deletes items in DynamoDB
  
Globals:
  Function:
    Timeout: 10

Resources:
  DDBHandlerFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: dynamo-handler/
      Handler: app.handler
      Runtime: nodejs12.x
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref ItemsTable
      Events:
        GetAllItems:
          Type: HttpApi
          Properties:
            Path: /items
            Method: GET
        GetAnItem:
          Type: HttpApi
          Properties:
            Path: /items/{id}
            Method: GET
        DeleteAnItem:
          Type: HttpApi
          Properties:
            Path: /items/{id}
            Method: DELETE
        CreateOrUpdateItem:
          Type: HttpApi
          Properties:
            Path: /items
            Method: PUT

  ItemsTable:
    Type: AWS::Serverless::SimpleTable
    Properties:
      PrimaryKey:
        Name: id
        Type: String
      TableName: http-crud-tutorial-items

Outputs:
  ApiEndpoint:
    Description: "The invoke URL for our HTTP API"
    Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com/items"
  Function:
    Description: "DynamoDB handler function ARN"
    Value: !GetAtt DDBHandlerFunction.Arn

但是,在另一个使用RestApi的教程示例中,可以在 YML 模板中找到 API 资源的声明。

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Lambda application that calls the Lambda API.
Resources:
  api:
    Type: AWS::Serverless::Api
    Properties:
      StageName: api
      TracingEnabled: true
      OpenApiVersion: 3.0.2
  function:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs12.x
      CodeUri: function/.
      Description: Call the AWS Lambda API
      Timeout: 10
      # Function's execution role
      Policies:
        - AWSLambdaBasicExecutionRole
        - AWSLambda_ReadOnlyAccess
        - AWSXrayWriteOnlyAccess
      Tracing: Active
      Events:
        getEndpoint:
          Type: Api
          Properties:
            RestApiId: !Ref api
            Path: /
            Method: GET

这是为什么?这是设计使然,因为HttpApi被认为是“轻量级的”?

4

0 回答 0