How to call AWS Lambda function in Swift

I cannot find any documentation or examples on how to call the lambda function in Swift , but I tried to extrapolate it from the documentation using Objective-C, and I still get errors:

"Error in myFunction: ValidationException: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes"

It seems like I am passing parameters to the function incorrectly when I call the lambda function from swift, because the script is trying to write to DynamoDB, but one of the parameters is empty (this lambda script works when I call it in javascript / node).

  let lambda = AWSLambda.defaultLambda() let request = AWSLambdaInvocationRequest() var context = [String: String]() let jsonString = "{\"email\":\" example@example.com \",\"name\":\"example\"}" let jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding) request.clientContext = jsonData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) request.functionName = "myFunction" lambda.invoke(request).continueWithBlock( { (currentTask: AWSTask!) -> AWSTask in if (currentTask.error != nil) { // failed to execute. print("Error executing: ", currentTask.error) task.setError(currentTask.error) } else { print("token: ", currentTask.result) task.setResult(currentTask.result) } return currentTask }) 
+7
amazon-web-services swift aws-lambda
source share
2 answers

You need to set the payload parameter to the card containing the data you want to transfer.

  let invocationRequest = AWSLambdaInvokerInvocationRequest() invocationRequest.functionName = "myFunction" invocationRequest.invocationType = AWSLambdaInvocationType.RequestResponse invocationRequest.payload = ["email" : " example@example.com ", "name" : "example"] let lambdaInvoker = AWSLambdaInvoker.defaultLambdaInvoker() let task = lambdaInvoker.invoke(invocationRequest).continueWithSuccessBlock() { (task) -> AWSTask! in print("response: ", task.result) } 
+5
source share

Ryan Fitzgerald's answer gives me some errors during compilation, but I had success with this version:

First, I have an initialization function with access credentials. Please note that this is not the recommended secure access method for production code, but it is suitable for testing and other purposes. It also assumes that you have a Constants.swift file in which you define the constants listed:

 func initializeLambda() { let credentialsProvider = AWSStaticCredentialsProvider.init(accessKey:Constants.AWS_ACCESS_KEY, secretKey: Constants.AWS_SECRET_KEY) let defaultServiceConfiguration = AWSServiceConfiguration(region: Constants.AWS_REGION, credentialsProvider: credentialsProvider) AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = defaultServiceConfiguration } 

Otherwise, we can provide a version similar to the previous version. I deleted the "let task" because the "task" is not used in his example. In addition, I have included the logic circuit of some JSON parsing, which you are likely to do inside the call task. Finally, I changed to continueWithBlock (). If you use continueWithSuccessBlock (), you will not enter this block if Amazon Lambda reaches its timeout or if something goes wrong with the request and, as a rule, you want these situations to be handled here.

  self.initializeLambda() //Call our previously written initialization function let invocationRequest = AWSLambdaInvokerInvocationRequest() invocationRequest.functionName = "functionName" invocationRequest.invocationType = AWSLambdaInvocationType.RequestResponse invocationRequest.payload = ["key1" : "value1", "key2" : "value2"] let lambdaInvoker = AWSLambdaInvoker.defaultLambdaInvoker() lambdaInvoker.invoke(invocationRequest).continueWithBlock() { (task: AWSTask) -> AWSTask in print("response: ", task.result) //In here you'll likely be parsing a JSON payload if let payload: AnyObject = task.result?.payload { if let error: AnyObject = payload.objectForKey("error") { //If there is an error key in the JSON dictionary... } else { //If the JSON dictionary has no error key... } return task; } } 

Tested and tested as functional on Swift 2.2 in Xcode 7.3.

+2
source share

All Articles