我正在尝试使用AWS SAM在Lambda和API Gateway之间进行简单集成。我想自定义lambda的输入 - 应用一些 requestTemplates 。但那些似乎被忽略了。
以:
运行sam local start-api
curl -v -XPOST -H "Content-type: application/json" -d '{"jarmil":"prdel"}' http://localhost:3000/
输出结果为:
START RequestId: 43594e8a-c3af-4f47-9d85-6c605131f02a Version: $LATEST
Processing event {'httpMethod': 'POST', 'body': '{"jarmil":"prdel"}', 'resource': '/', 'requestContext': {'resourcePath': '/', 'httpMethod': 'POST', 'stage': 'prod', 'identity': {'sourceIp': '127.0.0.1:53210'}}, 'queryStringParameters': {}, 'headers': {'Accept': '*/*', 'Content-Length': '18', 'Content-Type': 'application/json', 'User-Agent': 'curl/7.43.0'}, 'pathParameters': None, 'stageVariables': None, 'path': '/'}
END RequestId: 43594e8a-c3af-4f47-9d85-6c605131f02a
似乎可以像创建默认API一样工作。我打印了这个活动。但似乎我的API定义完全被忽略了。我总是在没有应用requestTemplates
的情况下取回原始事件。症状:
produces
,将任何格式错误的招摇RestApiId
(不存在的引用)时 - 无需更改type
代替AWS::Serverless::Api
时 - 无效我的template.yml
:
AWSTemplateFormatVersion : '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: POC
Resources:
ScheduleFunction:
Type: 'AWS::Serverless::Function'
Properties:
Runtime: python3.6
Handler: lambda_function2.lambda_handler
Events:
ApiRoot:
Type: Api
Properties:
RestApiId: !Ref ScheduleApi
Path: /
Method: ANY
ScheduleApi:
Type: 'AWS::Serverless::Api'
Properties:
StageName: dev
DefinitionUri: swagger.yml
我的swagger.yml
:
swagger: 2.0
info:
title: "Scheduling API"
consumes:
- application/json
produces:
- application/json
paths:
/:
post:
x-amazon-apigateway-integration:
httpMethod: post
type: aws
requestTemplates:
application/json: "#input x"
lambda_function2.py
:
def lambda_handler(event, context):
print("Processing event", event)
return event
This example看起来与我想要实现的几乎相同。也不起作用。