API Gateway mapping template is not being run

45 views Asked by At

I have an AWS API Gateway REST API with a POST method that calls a lambda function. I want the lambda function to be able to reference some specific information in the request context, specifically Cognito user identity ID, but that information is not being set. I suspect it needs to be explicitly passed through in a template mapping. I'm using the default "method request passthrough" code for that template mapping, but the information I'm looking for is still null. As best I can tell, the mapping is not being run at all.

In the CloudWatch logs, I can see that the data I want is not in the request context available to the lambda function:

requestContext: { resourceId: 'sk1vz6', authorizer: { claims: [Object] }, resourcePath: '/addHorse', httpMethod: 'POST', extendedRequestId: 'RL8J1Fj6CYcEA8A=', requestTime: '07/Jan/2024:20:45:37 +0000', path: '/dev/addHorse', accountId: '224814880454', protocol: 'HTTP/1.1', stage: 'dev', domainPrefix: 'ejxzz5dwwc', requestTimeEpoch: 1704660337899, requestId: 'f5ff1ed8-2d03-4697-9218-f946ce4be7fc', identity: { cognitoIdentityPoolId: null, accountId: null, cognitoIdentityId: null, caller: null, sourceIp: '174.168.178.24', principalOrgId: null, accessKey: null, cognitoAuthenticationType: null, cognitoAuthenticationProvider: null, userArn: null, userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', user: null }

I'm specifically trying to get requestContext.identity.cognitoIdentityId populated. I have successfully logged in to Cognito on the client side, and I can see that an authToken is being passed (and I see it in the logs). The mapping template is for content type application/json, and my JSON request is content type application/json:

var requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: authToken }, body: raw, redirect: 'follow' };

I tried changing the mapping Template "cognitoIdentityId" to be "foo," and then deploying and re-running. The logs still show its value as "null," making me suspect the mapping template isn't being run. (I tried a variety of other ways of changing the mapping template's output and none were reflected in the logs.)

  1. Is there something I'm doing wrong that's preventing the mapping template from running?
  2. Is this even the right approach to get the cognito information to be passed to the lambda?
0

There are 0 answers