【发布时间】:2019-12-12 07:57:34
【问题描述】:
AWS just announced 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)}
【问题讨论】:
标签: amazon-web-services aws-api-gateway serverless-framework serverless aws-serverless