【发布时间】: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