aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.65k stars 3.91k forks source link

aws-apigateway: Endpoint response body before transformations: null #31978

Closed sterankin closed 55 minutes ago

sterankin commented 4 hours ago

Describe the bug

When using the API Gateway Lambda Integration, the function errors at the API Gateway with a 502 error, unless the function is an async function.

cdk version: "aws-cdk-lib": "^2.147.3",

Regression Issue

Last Known Working CDK Version

No response

Expected Behavior

Expect the valid lambda response to be accepted by the api gateway.

Current Behavior

Api Gateway Logs for the non-async function:

Received response. Status: 200, Integration latency: 25 ms Method completed with status: 502 Execution failed due to configuration error: Malformed Lambda proxy response Endpoint response body before transformations: null

Reproduction Steps

Simple GET on an API Gateway fails

import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';

export function handler(event: APIGatewayProxyEvent, context: Context): APIGatewayProxyResult {
  console.log('event', event);
  console.log('context', context);

  const response: APIGatewayProxyResult = {
    statusCode: 200,
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      message: 'Hello, World!',
      input: event,
    }),
  };

  return response;
}

However this works:

import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';

export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
  console.log('Received event:', JSON.stringify(event, null, 2));
  // Example of a fake async operation
  await new Promise((resolve) => setTimeout(resolve, 0));

  const response: APIGatewayProxyResult = {
    statusCode: 200,
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      message: 'Hello, World!',
      input: event,
    }),
  };

  return response;
};

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

n/a

Framework Version

No response

Node.js Version

18

OS

n/a

Language

TypeScript

Language Version

No response

Other information

No response

ashishdhingra commented 2 hours ago

@sterankin Good morning. Could you please confirm if you are using CDK or @types/aws-lambda? This repository is for AWS CDK. We do not maintain @types/aws-lambda. Kindly open the issue in the right repository https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/aws-lambda.

Thanks, Ashish

sterankin commented 1 hour ago

@ashishdhingra I am using CDK to build and deploy the API Gateway and associated integrated lambdas.

I am using the LambdaIntegration from aws-cdk-lib/aws-apigateway.

import { Code, Function, FunctionProps, Runtime } from 'aws-cdk-lib/aws-lambda'; import { LambdaIntegration, MethodLoggingLevel, MethodOptions, Period, TokenAuthorizer, } from 'aws-cdk-lib/aws-apigateway';

ashishdhingra commented 58 minutes ago

@ashishdhingra I am using CDK to build and deploy the API Gateway and associated integrated lambdas.

I am using the LambdaIntegration from aws-cdk-lib/aws-apigateway.

import { Code, Function, FunctionProps, Runtime } from 'aws-cdk-lib/aws-lambda'; import { LambdaIntegration, MethodLoggingLevel, MethodOptions, Period, TokenAuthorizer, } from 'aws-cdk-lib/aws-apigateway';

@sterankin Thanks for you response. In the code snippet that was shared in issue description, it's a lambda handler which uses @types/aws-lambda, with no reference to CDK. Could you please share end-to-end self-contained minimal reproduction code to troubleshoot the issue?

Referring to Define Lambda function handler in Node.js, looks like you are using callback version of Lambda handler.

Also, what is the Lambda runtime you are targeting. Latest Lambda runtimes come bundled with AWS SDK for JavaScript v3, so most likely this the the Lambda runtime issue, which might be enforcing async pattern. May be you should try opening the issue in AWS SDK for JavaScript repo here!

Thanks, Ashish

sterankin commented 55 minutes ago

Thanks will try there

github-actions[bot] commented 55 minutes ago

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.