Currently, I am utilizing the CDK TypeScript Lambda stack which is connected to an API Gateway. Everything seems to be functioning perfectly when I use 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, my attempt to set the headers using a common constant resulted in 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,
});
An invalid lambda response was received with 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)']}
Any assistance would be greatly appreciated!