Search code examples
swiftamazon-web-servicesaws-lambdaaws-mobilehubamazon-mobile-hub

How to invoke lambda function using AWS Mobile Hub Helper?


I can't currently invoke a lambda in Swift using AWS Mobile Helper.

I've succeed to configure my project to identify user using Cognito User Pools and Cognito Federated Identities.

But now invoking lambda function lead to a crash: terminating with uncaught exception of type NSException

Before crash there are the following log:

AWSiOSSDK v2.4.11 [Debug] AWSInfo.m line:122 | -[AWSServiceInfo     initWithInfoDictionary:checkRegion:] | Couldn't read the region configuration from Info.plist for the client. Please check your `Info.plist` if you are providing the SDK configuration values through `Info.plist`.
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The service configuration is `nil`. You need to configure `Info.plist` or set `defaultServiceConfiguration` before using this method.'

And here is the code:

let lambda = AWSLambda.defaultLambda()

lambda.invoke(myRequest, completionHandler: {
            (myAnswer: AWSLambdaInvocationResponse?, error: NSError?) in

   print("ERROR HERE: \(error)")
   let payload = myAnswer?.payload
   print("PAYLOAD HERE: \(payload)")

})

Solution

  • If you are using the Mobile Hub or Mobile Hub Helper you should use the classes that it provides. When you instantiate the CloudLogic singleton it sets the service configuration up.

    So rather than AWSLambda.defaultLambda you should use AWSCloudLogic.defaultCloudLogic it's methods will manage the permissions issues as soon as it is instantiated.

    You can get an example by using the mobile hub to download a sample app that uses cloud logic.

    I put together a brief diagram of the mobile hub

    mobile hub diagram