Deploying Restate TypeScript services on AWS Lambda
This tutorial shows how to deploy a greeter service written with the Restate TypeScript SDK on AWS Lambda via AWS console.
- The prerequisites for running Restate TS services
- 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 Server (e.g. via an SSH session if it is running on EC2) 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.