A custom runtime layer that runs "Effectful" lambda functions.
The idea is to have an option to implement lambda functions returning Effect
type from effect library.
First, you will need to deploy the layer to your AWS account. Clone this repository and run the build-layer
script to prepare the layer for distribution. Then run the publish-layer
script to publish the layer to your AWS account.
git clone https://github.com/floydspace/aws-lambda-effect-runtime.git
cd aws-lambda-effect-runtime
pnpm install
pnpm publish-layer
Once you publish the layer to your AWS account, you can create a Lambda function that uses the layer.
Example of an effectful lambda handler implementing the EffectHandler
type from @effect-aws/lambda
:
import type { SNSEvent } from "aws-lambda"
import { Effect } from "effect"
import type { EffectHandler } from "@effect-aws/lambda"
// Define your effect handler
export const handler: EffectHandler<SNSEvent, never> = (event, context) => {
// Your effect logic here
return Effect.logInfo("Hello, World!")
}
or with global layer:
import type { SNSEvent } from "aws-lambda"
import { Effect, Logger } from "effect"
import type { EffectHandlerWithLayer } from "@effect-aws/lambda"
// Define your effect handler with global layer
export const handler: EffectHandlerWithLayer<SNSEvent, never> = {
handler: (event, context) => {
// Your effect logic here
return Effect.logInfo("Hello, World!")
},
layer: Logger.pretty,
};
The global layer will be constructed on lambda cold-start and finalized on shutdown. (How cool is that, huh!)
You can transpile your handler function to JavaScript or bundle it. Here's how you can do it using esbuild
:
- Run
esbuild src/handler.ts --bundle --platform=node --target=node22 --external:@effect/platform --external:effect --outfile=dist/handler.js
- Zip the
/dist
folder
Note: Make sure you exclude effect
and @effect/platform
from the bundle. Those dependencies are provided by the layer.
Once you've written your Lambda function, you need to configure a new Lambda function to the layer. The following steps apply to configuring in the console, CloudFormation, CDK, Terraform, or any other configuration management option for AWS:
- Create the Lambda function
- Set the Runtime to custom with Amazon Linux 2023
- Set the handler to
<handler-file-name>.<handler-function-name>
(e.g.src/handler.effectHandler
) - Set the architecture to whichever architecture you configured when you built/deployed the Lambda Layer
- Attach the Lambda Layer to your new function
- Upload the zip file from step 2. You can do this in the console directly, upload to S3 and set that as the location for the handler file in Lambda, or use something like CDK to manage this for you.