2

I tried the cloudformation template that I found here... https://bl.ocks.org/magnetikonline/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 -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? I have already set the environment variable at lambda function level.


The template that does not work is this...

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.


I have updated my template and now it deploys function and gateway corretly. And still the URL generated does not work and shows "internal server error" message.

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

0

2 Answers 2

1

You should change to using HTTP proxy integration. Here is some info from AWS on proxy integration: https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-http-integrations.html

Try changing your RequestParameters from:

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your response. But it is still not working. I have updated my question.
When you say the URL generated shows "internal server error", can you elaborate a bit on where you are seeing that? Do you reach the lambda function or does the error occur before even getting to the lambda function?
Changing GET to POST resolved the issue. forums.aws.amazon.com/thread.jspa?threadID=209420 Is it possible to use CDK for this template so that it will be easy to manage in python?
Yes, here is a link to the CDK API reference for python: docs.aws.amazon.com/cdk/api/latest/python/index.html and more specifically to the API Gateway section: docs.aws.amazon.com/cdk/api/latest/python/…
Thanks for that link. But can you show an example that implements the same concept mentioned in this question using python?
0

There are two ways you can access mynumber

Method 1 which works best with the SAM serverless API template. Define "API Gateway" and "Lambda". In Lambda definitions, call Events of type API:

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. It can be accessed with multiValueQueryStringParameters or queryStringParameters from the event object.

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". Linking the Lambda using the URI statement under "API Method".

for this method I only have a yaml example

    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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.