[英]Adding parameters to api using cloudformation
我尝试了我在这里找到的 cloudformation 模板...... https://bl.ocks.org/ma.netikonline/c314952045eee8e8375b82bc7ec68e88
它按预期工作。 但我想为发布请求提供参数。 我的 Curl 命令应该看起来像这样......
curl -d "mynumber=12345" -X POST https://tyin2sswj2.execute-api.us-east-1.amazonaws.com/mycall
cloudformation模板中的API网关如何处理? 我已经将环境变量设置在 lambda function 级别。
不起作用的模板是这个......
https://raw.githubusercontent.com/shantanuo/cloudformation/master/updated/lambda_api.tpl.txt
很明显,我无法通过网关传递“mnumber”变量。
我已经更新了我的模板,现在它正确地部署了 function 和网关。 并且生成的 URL 仍然不起作用并显示“内部服务器错误”消息。
https://raw.githubusercontent.com/shantanuo/cloudformation/master/testapi.tpl.txt
您应该改为使用 HTTP 代理集成。 以下是 AWS 关于代理集成的一些信息: https : //docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-http-integrations.html
尝试从以下位置更改您的 RequestParameters:
RequestParameters:
method.request.querystring.mnumber: false
到
RequestParameters:
method.request.path.proxy: true
并正在整合:
RequestParameters:
integration.request.querystring.mnumber: "method.request.querystring.mnumber"
到
RequestParameters:
integration.request.path.proxy: 'method.request.path.proxy'
这是一个关于代理与 API 网关集成的好教程: https : //docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-http.html
您可以通过两种方式访问 mynumber
方法 1 最适用于 SAM 无服务器 API 模板。 定义“API 网关”和“Lambda”。 在Lambda定义中,调用API类型的Events:
这使得查询字符串由于事件属性而被自动选取。 这些参数可以在传递给所有 lambda 函数的事件响应中找到。 可以使用事件 object 中的multiValueQueryStringParameters或queryStringParameters访问它。
exports.getByDateHandler = async (event) => {
console.info(event.queryStringParameters);
console.info(event.multiValueQueryStringParameters);
}
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Description",
"Transform": ["AWS::Serverless-2016-10-31"],
"Resources": {
"getByDateFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "src/handlers/getByDate/get-by-date.getByIdHandler",
"Runtime": "nodejs14.x",
"Architectures": ["x86_64"],
"MemorySize": 128,
"Timeout": 100,
"Events": {
"Api": {
"Type": "Api",
"Properties": {
"Path": "/date",
"Method": "GET"
}
}
}
}
}
},
"Outputs": {
"WebEndpoint": {
"Description": "API Gateway endpoint URL for Prod stage",
"Value": {
"Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
}
}
}
}
我还没有测试过的方法 2 是通过定义“Lambda”、“API 网关”、“API 资源”和“API 方法”。 使用“API 方法”下的 URI 语句链接 Lambda。
对于这种方法,我只有一个 yaml 示例
MyLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Description: "Node.js Express REST API"
FunctionName: "get_list_function" (The name in AWS console)
Handler: lambda.handler
Runtime: nodejs12
MemorySize: 128
Role: <ROLE ARN>
Timeout: 60
apiGateway:
Type: "AWS::ApiGateway::RestApi"
Properties:
Name: "example-api-gw"
Description: "Example API"
ProxyResource:
Type: "AWS::ApiGateway::Resource"
Properties:
ParentId: !GetAtt apiGateway.RootResourceId
RestApiId: !Ref apiGateway
PathPart: '{proxy+}' OR "a simple string like "PetStore"
apiGatewayRootMethod:
Type: "AWS::ApiGateway::Method"
Properties:
AuthorizationType: NONE
HttpMethod: ANY
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
IntegrationResponses:
- StatusCode: 200
Uri: !Sub >-
arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyLambdaFunction.Arn}/invocations
ResourceId: !Ref ProxyResource
RestApiId: !Ref "apiGateway"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.