How to call Lambda function by iOS SDK?

后端 未结 2 617
走了就别回头了
走了就别回头了 2021-01-24 11:04

I have an iOS Shopping App and want to call a lambda Function for logging. But i dont want to call the lambda function by an API Gateway to save costs. Is there a possibility to

相关标签:
2条回答
  • 2021-01-24 11:31

    You can invoke lambda functions directly using the AWS iOS SDK. Here is a code snippet

    AWSLambda *lambda = [AWSLambda defaultLambda];
    AWSLambdaInvocationRequest *invocationRequest = [AWSLambdaInvocationRequest new];
    invocationRequest.functionName = @"functionname";
    invocationRequest.invocationType = AWSLambdaInvocationTypeRequestResponse;
    NSDictionary *parameters = ...
    
    invocationRequest.payload = [NSJSONSerialization dataWithJSONObject:parameters
                                                                options:kNilOptions
                                                                  error:nil];
    
    [lambda invoke:invocationRequest] 
    

    The integration test shown here https://github.com/aws-amplify/aws-sdk-ios/blob/335e4d82a641fdb9cdc84773bf115951e850b884/AWSLambdaTests/AWSLambdaTests.m#L110 demonstrates how you can invoke a lamdba function and validate the results.

    0 讨论(0)
  • 2021-01-24 11:48

    You can do it in Swift using the AWSLambda Pod :

    import AWSLambda
    
    // ...
    
    let request = AWSLambdaInvocationRequest()!
    request.functionName = "name of your lambda"
    request.invocationType = .requestResponse
    let params: [String: Any] = [:]
    request.payload = try! JSONSerialization.data(withJSONObject: params, options: [])
    
    AWSLambda.default().invoke(request) { response, error in
        if let error = error {
            print(error)
        } else {
            print(response!.debugDescription)
        }
    }
    

    Of course you'll need to setup correct permissions as described in the documentation AWS Lambda permissions

    0 讨论(0)
提交回复
热议问题