Link

Creating, updating, and invoking AWS Lambda Functions in Haskell

Now that we’ve figured out how to compile our bootstrap binary, let’s wrap it all up into a single Haskell module, that can help us perform the following tasks from right within our GHCi session and without having to juggle between various tools (very important during rapid development):

  • compile our bootstrap binary
  • prepare the ZIP file
  • create or update the Lambda Function
  • invoke the Lambda Function

Preliminaries

Compiling the Lambda Function

Preparing the ZIP file

Note the difference between the prepareZipFile function given below, and the createZipFile function we used previousy. For the bootstrap binary and the libraries in lib/ to work on AWS Lambda, we need them to have have executable file-permissions. Unfortunately the zip library doesn’t make this process as easy as I’d like it to. (Related issues - #64 and #66)

“Upserting” the Lambda Function

This function is not very different from the version we wrote earlier

Invoking the Lambda Function without involving API Gateway

Note the difference between this function and the version that we had written earlier. Thanks to the module exposed by the lambda-function, instead of passing arguments around as stringified JSONs, we’ve been able to use appropriate Haskell types for them instead. In fact, even the return value is being parsed into an appropriate Haskell type.

However, this leads to a problem. There is nothing in this function’s code that indicates what the result type should be apart from saying that it should have an instance of the FromJSON type-class. There are many types that have an instance of the FromJSON type-class. So, what type does this function actually return?

Again, this is an unfortunate implication of Haskell’s type-system. There is no easy/built-in way in Haskell that allows us to determine the result type based on the Lambda Function’s name (which is a string literal). Therefore, every time you use this function, you will have to apply a type-annotation at the call-site to tell the compiler what type of result you’re expecting. Sometimes based on what you’re doing with the return value of this function, the compiler will be able to infer what the result type is. If not, you’ll get an “ambiguous type” error, and will be forced to provide the type-annotation.

Now you should be able to continiously update your Lambda Function without leaving your GHCi session (notice the type annotations):