Deploy a Purescript function to AWS Lambda
Docker is needed for invoking the lambda locally with SAM Local. You can also use lighter alternatives like Colima for this.
Sam local can be used to invoke the lambda function in a realistic manner on your local machine
Purescript is a functional programming language that compiles to Javascript
Spago is a build tool and package manager for Purescript
Terraform is an infrastructure as code tool, we will use it to deploy our lambda function to AWS
in infra/main.tf
you should change elements marked with # TODO: Change as needed
so that they reflect your project.
Create a .env
file in the root of the project which matches the structure of the .env.example
provided. See here for help getting AWS secret keys
Run ./scripts/build.sh
and with any luck your code will be built to the dist folder. You can have a look and see whet purescript will generate from the purs files.
Run ./scripts/sam-invoke.sh
to call your lambda function locally.
Run ./scripts/tf-init.sh
to install all terraform modules and set up the S3 backend (where terraform will store the state of your project)
Run ./scripts/tf-plan.sh
to see what changes will be applied to your infrastructure. On the first run you should see alot of resources being created
Run ./scripts/tf-apply.sh
to see what changes will be applied to your infrastructure and type "yes" to apply these changes. After doing this you should be able to go into the AWS console to see the lambda function exists.
When you make any changes its a case of running the plan and apply scripts again to upload the new code or make any other changes.
There are two main sides to this codebase;
The Purescript code is located here. The entry point of the lambda is the handler function in Main.purs.
Our code as it stands will
- Handle the incoming lambda invocation
- Make an api call to the pokemon api
- Decode this api response into an internal Purescript type
- Encode this Purescript type to JSON
- Stringify this JSON
- return it to the user with a 200 status
If anything goes wrong in this process then we will return a 500
Yes this all seems a-lot of effort for what is essentially a proxy, but it will help to have examples of the encoding and decoding.
Here live our Purescript tests, implementation is left as an excercise for the reader.
Scripts for the development lifecycle
Contains the Terraform code that is used to deploy the lambda function to AWS
A package.json that is uploaded with the lambda function. The most important bit of this is that it sets type: "module"
so that AWS knows this JS code uses native ES Modules syntax
This is Purescript's dependency file, it lists all the packages needed to compile the project