簡體   English   中英

無服務器框架 - 在 AWS API Gateway 中將現有應用程序從 REST 切換到 HTTP

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

AWS 剛剛宣布了對 Amazon API Gateway 的 HTTP 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)}

無服務器框架尚不支持 HTTP API,盡管我們目前正在研究它! 您可以在此處跟蹤工作: https : //github.com/serverless/serverless/issues/7052

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM