0 votes
1 view
in AWS by (19.2k points)

I can't find any documentation or examples on how to invoke a Lambda function in Swift but I've tried to extrapolate from the documentation using Objective-C and I'm still getting errors:

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

It appears that I'm not passing in the parameters to the function correctly when I invoke the lambda function from swift because the script tries to write to DynamoDB but one of the parameters is empty (this lambda script works when I invoke it in javascript/node). 

    let lambda = AWSLambda.defaultLambda()

    let request = AWSLambdaInvocationRequest()

    var context = [String: String]()

    let jsonString = "{\"email\":\"[email protected]\",\"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

    })

 

1 Answer

0 votes
by (44.6k points)

Set the payload parameter to a map which contains the data you want to pass.

    let invocationRequest = AWSLambdaInvokerInvocationRequest()

    invocationRequest.functionName = "myFunction"

    invocationRequest.invocationType = AWSLambdaInvocationType.RequestResponse

    invocationRequest.payload = ["email" : "[email protected]", "name" : "example"]

 

    let lambdaInvoker = AWSLambdaInvoker.defaultLambdaInvoker()

    let task = lambdaInvoker.invoke(invocationRequest).continueWithSuccessBlock() { (task) -> AWSTask! in

        print("response: ", task.result)

    }

 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...