[英]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.