【问题标题】:CognitoIdentityCredentials is not authorized to perform: lambda:InvokeFunction on resourceCognitoIdentityCredentials 无权执行:lambda:InvokeFunction on resource
【发布时间】:2019-02-12 07:51:24
【问题描述】:

我正在尝试从 iOS 客户端调用 lambda 函数。我的代码如下所示:

要获取凭据,在 appDelegate 中:

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions:

    [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


    // Uncomment to turn on logging, look for "Welcome to AWS!" to confirm success
    AWSDDLog.add(AWSDDTTYLogger.sharedInstance)
    AWSDDLog.sharedInstance.logLevel = .error


    // Instantiate AWSMobileClient to get AWS user credentials
    return AWSMobileClient.sharedInstance().interceptApplication(application, didFinishLaunchingWithOptions: launchOptions)

}

并在 viewController 上调用:

class ViewController: UIViewController {
let lambdaInvoker = AWSLambdaInvoker.default()
let jsonObject: [String: Any] = ["key1" : "value1",
                                 "key2" : 2 ,
                                 "key3" : [1, 2],
                                 "isError" : false]

@IBAction func button(_ sender: Any) {
    print("pressed")
    lambdaInvoker.invokeFunction("myTest", jsonObject: jsonObject)
        .continueWith(block: {(task:AWSTask<AnyObject>) -> Any? in
            if( task.error != nil) {
                print("Error: \(task.error!)")
                return nil
            }

            // Handle response in task.result
            if let JSONDictionary = task.result as? NSDictionary {
                print("Result: \(JSONDictionary)")
                print("resultKey: \(JSONDictionary["resultKey"])")
            }
            return nil
        })
}

它会抛出这个错误:

... Message=User: arn:aws:sts::103314601078:assumed-role/Cognito_testpoolUnauth_Role/CognitoIdentityCredentials is not authorized to perform: lambda:InvokeFunction on resource ...

我也设置了这个角色:

{
"roleName": "myRoleTest",
  "policies": [
    {
      "document": {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Sid": "Stmt1464440182000",
            "Effect": "Allow",
            "Action": [
              "lambda:InvokeAsync",
              "lambda:InvokeFunction"
            ],
            "Resource": [
              "*"
            ]
          }
        ]
      }

我知道我需要为该资源添加权限才能调用该函数,但我找不到在哪里或如何做!我会很感激任何帮助...

【问题讨论】:

    标签: ios swift aws-lambda aws-cognito


    【解决方案1】:

    好的,我不知道这是否对任何人有用,但我解决了这个问题。事实证明,要正确使用 AWS 开发工具包,首先您需要创建一个身份池。如您所见,我做了所有这些,并将池 id 和区域添加到配置文件中。我错过的是您还需要向身份池添加权限才能使用 lambda 服务。

    因此,创建身份池后,您将拥有两个新角色,一个是 auth,一个是 unauth。您应该转到 IAM 控制台、角色,找到有问题的角色(在我的情况下为 unauth)并将策略修改为如下内容:

    {  
    
    
    "Version":"2012-10-17",
       "Statement":[  
          {  
             "Effect":"Allow",
             "Action":[  
                "mobileanalytics:PutEvents",
                "cognito-sync:*"
             ],
             "Resource":[  
                "*"
             ]
          },
          {  
             "Effect":"Allow",
             "Action":[  
                "lambda:invokefunction"
             ],
             "Resource":[  
                "arn:aws:lambda:us-east-1:account-id:function:yourFunctionName"
             ]
          }
       ]
    }
    

    在此之后,您的资源应该能够调用 lambda 函数。

    如果这不是最好的方法,请指出!

    编辑:

    实际上有一个名为 AWS Lambda Role 的托管策略可以让您毫无问题地调用。

    【讨论】:

    • 非常感谢!你实际上为我省去了很多麻烦,因为我不明白错误来自哪里。希望这篇文章能传达给其他需要帮助的人
    猜你喜欢
    • 2017-02-02
    • 2019-01-25
    • 1970-01-01
    • 2021-07-09
    • 2016-09-26
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多