无服务器框架 - 在 AWS API 网关中将现有应用程序从 REST 切换到 HTTP

Serverless Framework - Switch existing application from REST to HTTP in AWS API Gateway

A​​WS just announced HTTP API 支持亚马逊 API 网关。这个新版本有一些非常令人印象深刻的价格和性能数据。最重要的是,AWS 表示使用 v2 的一般成本将比 v1 便宜 70%,延迟降低 50%。我很想在我现有的项目中尝试一下。

我在我的应用程序中使用无服务器框架。如何转换我现有的 API 以使用此新功能?这是我的 serverless.yml 文件的样子:

service: amitsn-blog-api

# Use the serverless-webpack plugin to transpile ES6
plugins:
  - serverless-webpack
  - serverless-offline

# serverless-webpack configuration
# Enable auto-packing of external modules
custom:
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true

provider:
  name: aws
  runtime: nodejs10.x
  stage: prod
  region: ap-south-1

  # 'iamRoleStatements' defines the permission policy for the Lambda function.
  # In this case Lambda functions are granted with permissions to access DynamoDB.
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
      Resource: "arn:aws:dynamodb:ap-south-1:*:*"
    - Effect: Deny
      Action:
        - logs:CreateLogGroup
        - logs:CreateLogStream
        - logs:PutLogEvents
      Resource: "*"

functions:
  # Defines an HTTP API endpoint that calls the main function in create.js
  # - path: url path is /posts
  # - method: POST request
  # - cors: enabled CORS (Cross-Origin Resource Sharing) for browser cross
  #     domain api call
  # - authorizer: authenticate using the AWS IAM role
  options:
    handler: options.main
    events:
      - http:
          path: posts
          method: options
          cors: true
  create:
    handler: create.main
    events:
      - http:
          path: posts
          method: post
          cors: true
          authorizer: aws_iam
  get:
  # Defines an HTTP API endpoint that calls the main function in get.js
  # - path: url path is /posts/{id}
  # - method: GET request
    handler: get.main
    events:
      - http:
          path: posts/{id}
          method: get
          cors: true
  list:
    # Defines an HTTP API endpoint that calls the main function in list.js
    # - path: url path is /posts
    # - method: GET request
    handler: list.main
    events:
      - http:
          path: posts
          method: get
          cors: true
          integration: lambda
          request:
            template:
              application/json: '{ "postType" : "$input.params(''postType'')" }'
  update:
    # Defines an HTTP API endpoint that calls the main function in update.js
    # - path: url path is /posts/{id}
    # - method: PUT request
    handler: update.main
    events:
      - http:
          path: posts/{id}
          method: put
          cors: true
          authorizer: aws_iam
  delete:
    # Defines an HTTP API endpoint that calls the main function in delete.js
    # - path: url path is /posts/{id}
    # - method: DELETE request
    handler: delete.main
    events:
      - http:
          path: posts/{id}
          method: delete
          cors: true
          authorizer: aws_iam

# Create our resources with separate CloudFormation templates
resources:
  # API Gateway Errors
  - ${file(resources/api-gateway-errors.yml)}

Serverless Framework 尚不支持 HTTP API,尽管我们目前正在努力解决!您可以在此处跟踪工作:https://github.com/serverless/serverless/issues/7052