简体   繁体   English

使用cloudformation向api添加参数

[英]Adding parameters to api using cloudformation

I tried the cloudformation template that I found here... https://bl.ocks.org/ma.netikonline/c314952045eee8e8375b82bc7ec68e88我尝试了我在这里找到的 cloudformation 模板...... https://bl.ocks.org/ma.netikonline/c314952045eee8e8375b82bc7ec68e88

It works as expected.它按预期工作。 But I will like to provide parameters to the post request.但我想为发布请求提供参数。 My Curl command should look something like this...我的 Curl 命令应该看起来像这样......

curl -d "mynumber=12345" -X POST https://tyin2sswj2.execute-api.us-east-1.amazonaws.com/mycall

How do I handle it at API gateway in the cloudformation template? cloudformation模板中的API网关如何处理? I have already set the environment variable at lambda function level.我已经将环境变量设置在 lambda function 级别。


The template that does not work is this...不起作用的模板是这个......

https://raw.githubusercontent.com/shantanuo/cloudformation/master/updated/lambda_api.tpl.txt https://raw.githubusercontent.com/shantanuo/cloudformation/master/updated/lambda_api.tpl.txt

As it is clear that I am not able to pass the "mnumber" variable through the gateway.很明显,我无法通过网关传递“mnumber”变量。


I have updated my template and now it deploys function and gateway corretly.我已经更新了我的模板,现在它正确地部署了 function 和网关。 And still the URL generated does not work and shows "internal server error" message.并且生成的 URL 仍然不起作用并显示“内部服务器错误”消息。

https://raw.githubusercontent.com/shantanuo/cloudformation/master/testapi.tpl.txt https://raw.githubusercontent.com/shantanuo/cloudformation/master/testapi.tpl.txt

You should change to using HTTP proxy integration.您应该改为使用 HTTP 代理集成。 Here is some info from AWS on proxy integration: https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-http-integrations.html以下是 AWS 关于代理集成的一些信息: https : //docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-http-integrations.html

Try changing your RequestParameters from:尝试从以下位置更改您的 RequestParameters:

RequestParameters:
        method.request.querystring.mnumber: false

to

RequestParameters:
        method.request.path.proxy: true

and under integration from:并正在整合:

RequestParameters:
        integration.request.querystring.mnumber: "method.request.querystring.mnumber"

to

RequestParameters:
          integration.request.path.proxy: 'method.request.path.proxy'

This is a good tutorial on proxy integration with API Gateway: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-http.html这是一个关于代理与 API 网关集成的好教程: https : //docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-http.html

There are two ways you can access mynumber您可以通过两种方式访问 mynumber

Method 1 which works best with the SAM serverless API template.方法 1 最适用于 SAM 无服务器 API 模板。 Define "API Gateway" and "Lambda".定义“API 网关”和“Lambda”。 In Lambda definitions, call Events of type API:在Lambda定义中,调用API类型的Events:

This makes it where query strings are automatically picked up due to the event property.这使得查询字符串由于事件属性而被自动选取。 The parameters can be found in the event response that is passed into all lambda functions.这些参数可以在传递给所有 lambda 函数的事件响应中找到。 It can be accessed with multiValueQueryStringParameters or queryStringParameters from the event object.可以使用事件 object 中的multiValueQueryStringParametersqueryStringParameters访问它。

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/"
            }
        }
    }
}

Method 2 which I havent tested is by defining the "Lambda", "API Gateway", "API Resource" and "API Methods".我还没有测试过的方法 2 是通过定义“Lambda”、“API 网关”、“API 资源”和“API 方法”。 Linking the Lambda using the URI statement under "API Method".使用“API 方法”下的 URI 语句链接 Lambda。

for this method I only have a yaml example对于这种方法,我只有一个 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM