简体   繁体   English

Cognito身份未传递到AWS Lambda函数

[英]Cognito Identity Not Passed Into AWS Lambda function

I have set up the AWS SDK for iOS to authenticate using Amazon Cognito. 我已经设置了适用于iOS的AWS SDK,以使用Amazon Cognito进行身份验证。 Here is the code that does that: 这是执行此操作的代码:

let credentials = AWSCognitoCredentialsProvider(regionType: .USEast1,
                                                    identityPoolId: IdentityManager.identityPoolId,
                                                    identityProviderManager: self)

AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = AWSServiceConfiguration(region: .USEast1,
                                                                                                    credentialsProvider: credentials)

I can use this credentials provider to successfully get an identity id. 我可以使用此凭据提供程序成功获取身份标识。 From here I attempt to set up an API Gateway generated client to make a call to my lambda function. 从这里我尝试建立一个API网关生成的客户端来调用我的lambda函数。

let client = APIGatewayClient.defaultClient()

client.endpointGet() { task in


}

I've confirmed that the APIGatewayClient has the AWSCognitoCredentialsProvider attached to it's configuration property and stepped through the request signing code to make sure it's requests get signed properly. 我已经确认APIGatewayClient已将AWSCognitoCredentialsProvider附加到其configuration属性,并逐步执行请求签名代码以确保其请求得到正确签名。

Here is the Lambda function being called by the client: 以下是客户端调用的Lambda函数:

exports.handler = function(event, context) {

    console.log('context: ' + require('util').inspect(context));

    if (context.identity) {

        context.succeed("found cognito identity")

    } else {

        return context.fail(new Error("cognito identity not found"))
    } 
}

The problem is that context.identity is null even though the request is being signed with the AWSCognitoCredentialsProvider . 问题是即使请求是使用AWSCognitoCredentialsProvider签名的, context.identity也是null。 What additional steps do I need to take to ensure Lambda recognizes my request is signed with a Cognito identity and populates the related field in the context object? 我需要采取哪些其他步骤来确保Lambda识别我的请求是使用Cognito身份签名并填充context对象中的相关字段?

In order to have the Cognito identity object available in your lambda function which is invoked through API gateway, you need to enable "Invoke with caller credentials" on the API Gateway "Integration Request" page of the API gateway resource. 为了使您的lambda函数中的Cognito标识对象可用,该函数通过API网关调用,您需要在API网关资源的API网关“集成请求”页面上启用“调用调用者凭据”。 This will inject the identity object in lambda context which will have cognitoIdentityId and cognitoIdentityPoolId within it. 这将在lambda上下文中注入标识对象,其中将包含cognitoIdentityId和cognitoIdentityPoolId。

Some similar posts: 一些类似的帖子:

SO: AWS API Gateway: How to pass IAM identity to Lambda function? SO: AWS API Gateway:如何将IAM身份传递给Lambda函数?

AWS Forums: https://forums.aws.amazon.com/message.jspa?messageID=669060#669060 AWS论坛: https ://forums.aws.amazon.com/message.jspa?messageID = 669060#669060

Hope this helps. 希望这可以帮助。

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

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