简体   繁体   English

如何从Python AWS Lambda函数访问context.identity?

[英]How to access context.identity from a Python AWS Lambda function?

My lambda function is receiving a context. 我的lambda函数正在接收上下文。

def lambda_handler(event, context):
    logger.info(context.function_name)
    logger.info(context.identity)
    logger.info(context.cognito_identity_id)
    logger.info(context.identity.cognito_identity_id)
    return 'hello world'

The log receives the function_name, and logs the memory address for context.identity . 日志接收function_name,并记录context.identity的内存地址。 But context.cognito_identity_id and context.identity.cognito_identity_pool_id are reported as None . 但是context.cognito_identity_idcontext.identity.cognito_identity_pool_id报告为None

How do I get cognito_identity_id populated? 如何填充cognito_identity_id

From Xcode debugger in the class built by AWS API Gateway I can see that the private variable `_configuration._credentialsProvider._identityId is set correctly. 从AWS API Gateway构建的类中的Xcode调试器,我可以看到私有变量`_configuration._credentialsProvider._identityId设置正确。 But this value isn't being passed thru to my AWS Lambda function. 但是此值并未通过我的AWS Lambda函数传递。

I can't find how to get is passed thru. 我找不到如何通过。 I've read this page where I'd expect it to be covered. 我已经阅读了本页面 ,希望可以覆盖该页面

Further: From the Lambda function I logged out dir(context.identity) giving ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', 'cognito_identity_id', 'cognito_identity_pool_id'] 进一步:从Lambda函数中,我注销了dir(context.identity)并给出了['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', 'cognito_identity_id', 'cognito_identity_pool_id']

This shows that cognito_identity_id and cognito_identity_id are actually present. 这表明cognito_identity_idcognito_identity_id实际上存在。

I think AWS Gateway -> AWS Lambda doesn't set-up Identity for free - I'm missing some configuration - a mapping template perhaps. 我认为AWS Gateway-> AWS Lambda无法免费设置身份-我缺少一些配置-也许是映射模板。

This is too long for a comment... 评论太久了...

You will need to look at the API Gateway documentation. 您将需要查看API Gateway文档。 I know that the Cognito identity ID is None if you 我知道,如果您知道Cognito身份ID为“ None

(a) invoke the lambda using the CLI with an (access key id,secret key) as credentials, (a)使用带有(access key id,secret key)作为凭据的CLI调用lambda,

(b) invoke the lambda using boto using (access key id,secret key) credentials, (b)使用(access key id,secret key)凭据使用boto调用lambda,

(c) invoke the lambda from the AWS console. (c)从AWS控制台调用lambda。

I invoke my Lambdas using Cognito credentials, and I get a non- None identity id in my context object. 我使用Cognito凭据调用Lambda,并且在上下文对象中获得非None身份ID。 To set up Cognito credentials you need to set up an identity pool with a role which is authorized to invoke the function (simple setup: create an unauthorized role and give it the lambda:invokeFunction permission for your function). 要设置Cognito凭据,您需要使用一个有权调用该函数的角色来设置身份池(简单设置:创建一个未经授权的角色,并为您的函数授予lambda:invokeFunction权限)。

I can provide Python or JavaScript code to do this, though it wouldn't answer your question because you specifically asked about invocation from the API Gateway. 我可以提供Python或JavaScript代码来执行此操作,尽管它不能回答您的问题,因为您专门询问了从API网关的调用。 But if you want it let me know. 但是,如果您想让我知道。

This is how I've made progress. 这就是我取得进步的方式。

Drop the GET method - it doesn't support the passing of Cognito identities thru to Lambda. 删除GET方法-它不支持通过Cognito身份传递给Lambda。

Use POST instead. 请改用POST。 For the POST's Integration Request select "Invoke with caller credentials" and create a Mapping template. 对于POST的集成请求,选择“使用呼叫者凭据调用”并创建一个映射模板。 The template needs to be "application/json" with the Template contents { "identity": "$input.params('identity')" } 模板必须为“ application / json”,其模板内容为{ "identity": "$input.params('identity')" }

With these additions, when the lambda function is called its `context.identity' parameter will be populated with the values of the caller's cognity identifier pool and id. 有了这些添加,当调用lambda函数时,将使用调用者的认知标识符池和id的值填充其`context.identity'参数。

From Python in your Lambda function be sure to access with context.identity and not context[identity] 确保从Lambda函数中的Python中访问context.identity而不是context[identity]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM