LoginSignup
11
12

More than 5 years have passed since last update.

Lambda function内からInvokeしたユーザのcognito IDを取得する方法

Last updated at Posted at 2015-11-24

cognito IDをいわゆるユーザIDのようなユニークな識別子として使っている場合、Lambda function内からcognito IDを取得したいですよね。
たったこれだけなんですが、すぐ見つかりそうで探し方が悪いのか中々見つからなかったため書いときます。

exports.handler = function(event, context) {
    console.log('Cognity identity ID =', context.identity.cognitoIdentityId);
};

なおAWSコンソールからLambda functionをテストするなど、cognitoを経由せずにInvokeした場合は以下のようにエラーになります。要注意。

TypeError: Cannot read property 'cognitoIdentityId' of undefined

他にもcontextにはLambda functionに関するいくつかの情報が入ってます。
ログストリーム名とかはデバッグ時に便利かも。

console.log('remaining time =', context.getRemainingTimeInMillis());
// remaining time = 9876
console.log('functionName =', context.functionName);
// functionName = functionName
console.log('AWSrequestID =', context.awsRequestId);
// AWSrequestID = 1111111-aaaa-2222-bbbb-3333333333
console.log('logGroupName =', context.logGroupName);
// logGroupName = /aws/lambda/functionName
console.log('logStreamName =', context.logStreamName);
// logStreamName = 2015/11/24/[$LATEST]1234567890abcdef1234567890abcdef 
console.log('clientContext =', context.clientContext);
// clientContext = undefined
console.log('Cognity identity ID =', context.identity.cognitoIdentityId);
// cognito id: ap-northeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx

参考

http://docs.aws.amazon.com/ja_jp/lambda/latest/dg/nodejs-prog-model-context.html
http://stackoverflow.com/questions/29928401/how-to-get-the-cognito-identity-id-in-aws-lambda
http://stackoverflow.com/questions/32325015/how-to-get-cognito-id-in-a-lambda-function

11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12