Lambda function issue in AWS iOS Sdk

2019-09-11 07:04发布

问题:

I'm new to deal with AWS web services, i'm working with open-identity sending DeveloperAuthProviderName and Token in login dictionary of AWSCognitoCredentialsProvider class and then in a viecontroller i'm invoking a lambda funtion and its giving me error below. I have used CognitoSyncDemo App and also tried with importing all frameworks through pod but result is same. Please help me out of this.

AWSiOSSDKv2 [Error] AWSCredentialsProvider.m line:435 | __73-[AWSCognitoCredentialsProvider getCredentialsWithCognito:authenticated:]_block_invoke | GetCredentialsForIdentity failed. Error is [Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=7 "The operation couldn’t be completed. (com.amazonaws.AWSCognitoIdentityErrorDomain error 7.)" UserInfo=0x1700778c0 {__type=InvalidParameterException, message=Please provide a valid public provider}]

This is Appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityId:IdentityId identityPoolId:CognitoIdentityPoolId logins:@{DeveloperAuthProviderName:Token}];

    AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1
                                                                     credentialsProvider:credentialsProvider];

    [[AWSServiceManager defaultServiceManager] setDefaultServiceConfiguration:configuration];

    return YES;
}

This is the code in viewDidLoad in a viewController.

-(void)setUpCredentials{

    //initWithRegionType:CognitoRegionType identityId:IdentityId identityPoolId:CognitoIdentityPoolId logins:@{DeveloperAuthProviderName:Token}
AZCDeveoper class inherit  AWSEnhancedCognitoIdentityProvider class
    AZCDeveloper * identity = [[AZCDeveloper alloc] initWithRegionType:CognitoRegionType identityId:IdentityId accountId:nil identityPoolId:CognitoIdentityPoolId logins:@{DeveloperAuthProviderName:Token}];



    credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:CognitoRegionType
                                                               identityProvider:identity
                                                                  unauthRoleArn:nil
                                                                    authRoleArn:nil];


    // [identity refresh];

}

This is the lambda function invoked on a certain click

-(void)lamdaInvoker{



    AWSLambdaInvoker *lambdaInvoker = [AWSLambdaInvoker defaultLambdaInvoker];

    NSLog(@"LamdaInvoker:%@",lambdaInvoker);



    [[lambdaInvoker invokeFunction:@"Hello"
                    JSONObject:@{@"": @""}] continueWithBlock:^id(BFTask *task) {
        if (task.error) {
            NSLog(@"Error: %@", task.error);
            if ([task.error.domain isEqualToString:AWSLambdaInvokerErrorDomain]
                && task.error.code == AWSLambdaInvokerErrorTypeFunctionError) {
                NSLog(@"Function error: %@", task.error.userInfo[AWSLambdaInvokerFunctionErrorKey]);

            }
        }
        if (task.exception) {
            NSLog(@"Exception: %@", task.exception);

        }
        if (task.result) {
            NSLog(@"Result: %@", task.result);

            dispatch_async(dispatch_get_main_queue(), ^{

            });

        }
        return nil;
    }];


}

回答1:

hey from your question the approach I would take is focusing on the portion of the error that saysGetCredentialsForIdentity. I'm relatively new to AWS as well and I think that half the time I've encountered an error, it was because there was something about credentials that I was doing wrong. I'd make sure that the IAM user has the necessary credentials in order to invoke the lambda function.

If you think that you have that all correctly placed, then I'd make sure that you are setting up your configuration correctly.