我通过CloudFormation(用于函数)和swagger文件(用于API网关)创建了API网关和Lambda函数。
在部署后运行端点时,lambda事件的输入数据仅包含有效负载。
当我使用“ SAM local start-api”启动API并运行相同的端点时,该函数似乎收到了完整的 proxy 输入:
{
"httpMethod":"POST",
"body":"...",
"resource": "...",
"requestContext": {"...":"..."}
. . .
}
请帮助我检查我做错了什么...
这是我的Yaml端点:
/authorization/generate-token:
post:
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- name: "x-api-key"
in: "header"
required: true
type: "string"
responses:
200:
description: "200 response"
schema:
$ref: "#/definitions/Empty"
headers:
Access-Control-Allow-Origin:
type: "string"
- api_key: []
x-amazon-apigateway-integration:
uri: "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:ACCOUNT_NUMBER:function:generate_token/invocations"
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Origin: "'*'"
passthroughBehavior: "when_no_match"
httpMethod: "POST"
cacheNamespace: "8xok2h"
cacheKeyParameters:
- "method.request.header.x-api-key"
contentHandling: "CONVERT_TO_TEXT"
type: "aws"
options:
consumes:
- "application/json"
produces:
- "application/json"
responses:
200:
description: "200 response"
schema:
$ref: "#/definitions/Empty"
headers:
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Methods:
type: "string"
Access-Control-Allow-Headers:
type: "string"
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'POST,GET,OPTIONS'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type'"
method.response.header.Access-Control-Allow-Origin: "'*'"
requestTemplates:
application/json: "{\"statusCode\": 200}"
passthroughBehavior: "when_no_match"
type: "mock"
CloudFormation函数:
GenerateToken:
Type: 'AWS::Serverless::Function'
DependsOn:
- baseAuthRole
Properties:
FunctionName: generate_token
Handler: index.handler
Runtime: nodejs8.10
CodeUri: auth/generate_token
Description: ''
MemorySize: 128
Timeout: 3
Role: !Sub 'arn:aws:iam::${AWS::AccountId}:role/AUTH_ROLE'
Events:
Api1:
Type: Api
Properties:
RestApiId: !Ref BaseApi
Path: /authorization/generate-token
Method: POST
API网关声明:
BaseApi:
Type: AWS::Serverless::Api
Properties:
Name: ebiz-base
DefinitionBody:
Fn::Transform:
Name: AWS::Include
Parameters:
Location: base-api/swagger.yaml
StageName: !Ref stageName
感谢助手:)