AWS SDK Lambda Invoke fails as input is not Base-64 encoded

874 Views Asked by At

When I am trying to invoke a Lambda function (below is my code) , I am getting an error in CloudWatch Logs saying the input is not a valid Base-64 string.

  String payload = "xxxxxxxxxxx";
  
  SdkBytes payloadBytes = SdkBytes.fromUtf8String(payload);

  String routingUri = getRoutingUri();
 
  InvokeRequest invokeRequest = InvokeRequest.builder()
                                   .functionName(routingUri)
                                   .payload(payloadBytes)
                                   .invocationType("RequestResponse")
                                   .build();

  InvokeResponse result = lambdaClient.invoke(invokeRequest);
Error  :  System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

I tried various ways to encode the request in Base-64 but nothing seems to be working

  • My AWS SDK version is 2.15.14
  <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>bom</artifactId>
      <version>2.15.14</version>
  </dependency>

Please Help.

2

There are 2 best solutions below

1
On

Here you need to endcode the string using Base64.Encoder from java.util package

0
On

It depends on the type of data you are setting in the payload for example if one of the variables you are sending to invoke is a byte[] you would do it like this


public byte[] invokeFunction(byte[] data,String certificate_serial) {
        String dataBase64 = Base64.getUrlEncoder().encodeToString(data);
            // Need a SdkBytes instance for the payload
            SdkBytes payload = SdkBytes.fromUtf8String("{\n" + " \"nameOfTheVariableInTheLambdaYouAreInvoking\": \"" + dataBase64 +  "\" \n" + "}");