noamyg
noamyg

Reputation: 3104

'Invalid lambda response received' when specifying headers through an imported const

I'm using CDK typescript lambda stack, connected to an API Gateway. Everything works just fine when I'm sending the following response:

const res = await request<ResponseModel>(req);
return {
    statusCode: res.status,
    headers: {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Credentials': true
    },
    body: JSON.stringify(res.data)
};

However, I've tried to set the headers with a common const, and the result is a failure:

// common-headers.ts
export const CommonResponseHeaders = {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Credentials': true
};

//function.ts
import { CommonResponseHeaders } from '../../common/common-headers';

const res = await request<ResponseModel>(req);
return {
    statusCode: res.status,
    headers: CommonResponseHeaders,
    body: JSON.stringify(res.data)
};

//stack.ts
const function = {
    name: 'myFunction',
    runtime: Runtime.NODEJS_14_X,
    entry: path.join(__dirname, './function.ts'),
    vpcSubnets: {
      subnetType: SubnetType.PRIVATE_WITH_EGRESS
    },
    handler: 'handler',
    environment: {
      ...SomeDefaultVariables
    }
  }
const lambda = new NodejsFunction(this, function.name, function);
const functionUrl = lambda.addFunctionUrl({
authType: FunctionUrlAuthType.NONE,
cors: {
  allowedOrigins: ['*'],
}
});
new CfnOutput(this, `${function.name}-FunctionUrl`, {
value: functionUrl.url,
});

Invalid lambda response received: Invalid API Gateway Response Keys: {'trace', 'errorType', 'errorMessage'} in {'errorType': 'TypeError', 'errorMessage': "Cannot read property 'trim' of undefined", 'trace': ["TypeError: Cannot read property 'trim' of undefined", ' at Object. (/var/task/index.js:10924:40)', ' at Module._compile (internal/modules/cjs/loader.js:1085:14)', ' at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)', ' at Module.load (internal/modules/cjs/loader.js:950:32)', ' at Function.Module._load (internal/modules/cjs/loader.js:790:12)', '
at Module.require (internal/modules/cjs/loader.js:974:19)', ' at require (internal/modules/cjs/helpers.js:101:18)', ' at _tryRequireFile (/var/runtime/UserFunction.js:72:32)', ' at _tryRequire (/var/runtime/UserFunction.js:160:20)', ' at _loadUserApp (/var/runtime/UserFunction.js:219:12)']}

Help would be much appreciated!

Upvotes: 5

Views: 962

Answers (2)

Prashant
Prashant

Reputation: 366

It's causing issue because it couldn't able to detect your imports properly.

Did you correctly imported common-headers.ts file so your CommonResponseHeaders?

You should try to specify paths in another file named config.json where you could do something like this:

Just make sure you add the common-headers.ts file path in include.

{
    "compilerOptions": {
        ...
    },
    "include": [
        "./src/**/*",
        "./common/**/*"
    ]
}

Now try to deploy using CDK, this should include your common-headers.ts properly and will fix your issue.

Note: Still not working? Maybe check your import statement and see if you correctly importing CommonResponseHeaders from common-headers.ts file or not

Upvotes: 0

Cloudkollektiv
Cloudkollektiv

Reputation: 14729

There is a mismatch between the headers your code returns and what API Gateway expects. Looking at your code example, if your response succeeds, it returns the correct headers. According to the AWS documentation, the Lambda function must return output of the following format:

{
    statusCode: "...",            // a valid HTTP status code
    headers: { 
        custom-header: "..."      // any API-specific custom header
    },
    body: "...",                  // a JSON string.
    isBase64Encoded:  true|false  // for binary support
}

So something else in your project is causing this error, and this error is not handled properly. If you encounter an error, you should return a 4xx or 5xx status code in the format described above. However, since your example is pretty small, I suppose this will not throw any errors. It is probably caused by the CDK framework in which your code is loaded. Specifically, I would look at the following things:

  1. There is a related issue about AWS amplify, where someone misspelled approot, where it should have been appRoot. He got exactly the same error complaining about the trim function.

  2. You must configure your TypeScript transpilation settings to match the Node.js runtime that you plan to use. Check out this documentation from AWS for more information on runtimes.

  3. When using NodeJS 14.x, you should double-check if your imports match the requirements for that specific version. A mistake is easily made when copying code from somewhere else. I suggest you carefully read this post, it describes that in your case local files should be imported as follows (although that might seem weird):

     // Actual file is ../../common/common-headers.ts
     import { CommonResponseHeaders } from '../../common/common-headers.js';
    

Upvotes: 0

Related Questions