Search code examples
amazon-web-servicesaws-lambdaaws-api-gateway

How do I test my function parameters in a API Gateway proxy lambda function created with AWS SDK?


I created a REST API with multiple Lambda function integrations. Here is an example of one function.

export const handler = (event: APIGatewayEvent, params: { Bucket: any; Key: any; }) =>{
    var method = event.httpMethod;
    const bucketName = params.Bucket;
    const keyName = params.Key;
    if (method === "POST") {
        const resp = client.getSignedUrl('getObject', {
            Bucket: bucketName,
            Key: keyName,
            Expires: 100 //time to expire in seconds
        })
        return {
            statusCode: 200,
            headers: {},
            body: JSON.stringify(resp)
        };}

    return {
        statusCode: 400,
        headers: {},
        body: "Bad Request"
    };
}

When I try to test my function with Postman, I am unsure if I am including these parameters in the right area OR I had set them up incorrectly in my function.

Postman example

I receive these errors in my lambda logs. Thanks.

"errorType": "MultipleValidationErrors",
    "errorMessage": "There were 2 validation errors:\n* MissingRequiredParameter: Missing  required key 'Bucket' in params\n* MissingRequiredParameter: Missing required key 'Key' in params"

Solution

  • How is your Lambda function invoked. DO you pass it Event JSON?

    I can answer this by referencing my example. I have a Lambda function named upload where I use Event JSON that specifies a file name. The Lambda function generates a presigned URL by invoking presigner.presignPutObject().

    enter image description here

    The Lambda function is implemented using the Java run-time API and you can see the logic where i get the value of filename.

    enter image description here

    Next, I setup an API Gateway endpoint that accepts a PUT. That API Gateway uses this Lambda function.

    Now I can invoke the API Gateway endpoint by sending a PUT Request using Postman. When I do - i pass JSON - as shown here:

    enter image description here

    The Lambda function gets the filename and the PUT request is successful and a presigned URL is returned. SO you need to check how to invoke the LAMBDA function. If you are using Event JSON, you still need to pass Event JSON from the Postman request. In your POSTMAN PIC - you are not passing Event JSON.