AWS Lambda + TypeScript
You can run your Restate services as serverless functions on AWS Lambda.
First, make sure you create a Lambda endpoint in your Restate service, as described in the TypeScript SDK docs.
The easiest way to run Restate handlers on AWS Lambda is to use the Restate CDK construct library.
To deploy a Restate service as a Lambda function, you can follow the guidelines of AWS for deploying plain TypeScript NodeJS functions. Restate does not add any complexity to this. You build a zip file containing the application code and dependencies and upload this to AWS Lambda. If you are using the Restate node template, then you can create a zip file with:
npm run bundle
AWS Lambda assumes that the handler can be found under index.handler
in the uploaded code.
By default, this is also the case for the Lambda functions developed with the Restate SDK.
Once your Lambda handler is deployed, register it with Restate as described in the registration docs. Make sure you first publish a new version of the Lambda function before registering it with Restate.
Tutorial
This tutorial shows how to deploy a greeter service written with the Restate TypeScript SDK on AWS Lambda.
- TypeScript: Latest stable version of NodeJS >= v18.17.1 and npm CLI >= 9.6.7
- Optional but recommended: Install the Restate Server & CLI
- An AWS account with permissions for Lambda.
Create a zip file from the code base
Now, we need to create a zip file that includes the service code and the required dependencies to run it. To build the code and make the zip file, do
npm run bundle
Deploying the Lambda function via the AWS console
Go to the Lambda UI in the AWS console. Click on Create function
.
Fill in the name of your function. You can leave the settings to the default.
View
Click Create function
.
You should now see a function overview with your new function in it.
View
The next step is uploading the zip file with our function code.
Open the Code
tab in the section below the function overview.
Click on Upload from
and select your zip file.
You should now see the uploaded code in the browser editor.
By default, Lambda assumes that your handler can be found under index.handler
.
So this means that you should have the Restate Lambda handler assigned to export const handler
in the file src/app.ts
, as shown in the code.
This handler will then be included in index.js
after creating the zip, and be used by AWS Lambda as the entry point of the Lambda function.
To change that you can scroll down to Runtime settings
and change the handler reference.
Finally, let's publish a new version of our Lambda function.
Go to the tab Versions
and click Publish new version
and then Publish
.
Our Lambda function should now be working!
Running Restate Server
Run the Restate Server via one of the options listed in the docs.
Running Restate in a Docker container
If you run Restate in a Docker container, then make sure it can using your local AWS creds (defined in ~/.aws):
docker run -e AWS_PROFILE -v ~/.aws/:/root/.aws --name restate_dev --rm -p 8080:8080 -p 9070:9070 -p 9071:9071 --add-host=host.docker.internal:host-gateway docker.io/restatedev/restate:1.1
Registering the service
Connect to the Restate (e.g. via an SSH session if it is running on EC2) runtime and execute the registration command:
- CLI
- curl
restate deployments register arn:aws:lambda:eu-central-1:000000000000:function:my-greeter:1
curl localhost:9070/deployments -H 'content-type: application/json' -d '{"arn": "arn:aws:lambda:eu-central-1:000000000000:function:my-greeter:1" }'
Make sure you replace the Lambda function ARN with the one you deployed, including it's version tag (here 1
).
When executing this command, you should see the discovered services printed out!
Invoke the handler
curl localhost:8080/Greeter/greet -H 'content-type: application/json' -d '"Hi"'
The Greeter service should say hi back.
Congratulations, you managed to run your first Restate Lambda handler!
Here are some next steps for you to try:
- Add a new method to the greeter function and redeploy the Lambda function with the new methods enabled.
- Create and deploy a new Lambda function that calls the greeter function.