Skip to content

Commit

Permalink
Fix and improve Lambda deployment tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
gvdongen committed Jun 11, 2024
1 parent e4ef009 commit 16c5067
Showing 1 changed file with 31 additions and 81 deletions.
112 changes: 31 additions & 81 deletions docs/deploy/lambda/lambda-typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,133 +78,83 @@ This tutorial shows how to deploy a greeter service written with the Restate Typ
</Step>
<Step stepLabel="4" title="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:
Fill in the name of your function. You can leave the settings to the default.

![Create function](/img/create-function.png)
<details>
<summary>View</summary>

![Create function](/img/create-function.png)

</details>

Click `Create function`.

You should now see a function overview with your new function in it.

![Function overview](/img/function-overview.png)
<details>
<summary>View</summary>

![Function overview](/img/function-overview.png)
</details>

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.
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!
</Step>
<Step stepLabel="5" title="Testing your Lambda function">
The AWS console lets you test your Lambda functions.
Go to your Lambda function and click on the `Test` tab.
Here, you can send requests to your Lambda function.

You can test service registration by sending this event JSON to your Lambda function:

```json
{
"resource": "",
"stageVariables": null,
"body": "",
"httpMethod": "POST",
"headers": {
"content-type": "application/proto"
},
"queryStringParameters": {},
"pathParameters": {},
"requestContext": {},
"multiValueHeaders": {},
"multiValueQueryStringParameters": {},
"path": "/discover",
"isBase64Encoded": true
}
```
<Step stepLabel="5" title="Running Restate Server">
Run the Restate Server via one of the options listed in [the docs](/develop/local_dev#running-restate-server--cli-locally).
The response should have status code 200 with a long base64 encoded body representing the services,
methods and Protobuf metadata behind the Lambda endpoint.

To test the greet method, you can send the following event JSON:

```json
{
"resource": "",
"stageVariables": null,
"body": "AAAAAAAAABsKEAGI6GNZE3i3iJ6H90kF2CsSBQRQZXRlGAEEAAABAAAACHIGCgRQZXRl",
"httpMethod": "POST",
"headers": {
"content-type": "application/restate"
},
"queryStringParameters": {},
"pathParameters": {},
"requestContext": {},
"multiValueHeaders": {},
"multiValueQueryStringParameters": {},
"path": "/invoke/org.example.Greeter/MultiWord",
"isBase64Encoded": true
}
```
<details>
The response should be the following:

```json
{
"headers": {
"content-type": "application/restate"
},
"statusCode": 200,
"isBase64Encoded": true,
"body": "BAEAAAAAAA5yDAoKSGVsbG8gUGV0ZQ=="
}
```
<summary>Running Restate in a Docker container</summary>
The body is the base64 encoded string of the response, and stands for `{"value":"Hello Pete"}`.
</Step>
<Step stepLabel="6" title="Running Restate Server">
Run the Restate Server via one of the options listed in [the docs](/develop/local_dev#running-restate-server--cli-locally).
If you run Restate in a Docker container, then make sure it can using your local AWS creds (defined in ~/.aws):
You don't necessarily need to run the Restate runtime on AWS, but it does need to be able to obtain credentials to invoke your Lambda.
You can run the Restate runtime locally in a Docker container to test your Lambda function, using your local AWS creds (defined in ~/.aws).
```shell
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:VAR::RESTATE_VERSION
```
</details>
```shell
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:VAR::RESTATE_VERSION
```
</Step>
<Step stepLabel="7" title="Discovering the services behind the Lambda endpoint">
<Step stepLabel="6" title="Discovering the services behind the Lambda endpoint">
Connect to the Restate (e.g. via an SSH session if it is running on EC2) runtime and execute the registration command:
<Tabs groupId="interface" queryString>
<TabItem value="cli" label="CLI">
```shell
restate deployments register <LAMBDA_FUNCTION_ARN>
restate deployments register arn:aws:lambda:eu-central-1:000000000000:function:my-greeter:1
```
Replace the Lambda function ARN in the data field.
</TabItem>
<TabItem value="curl" label="curl">
```shell
curl localhost:9070/deployments -H 'content-type: application/json' -d '{"arn": "<LAMBDA_FUNCTION_ARN>" }'
curl localhost:9070/deployments -H 'content-type: application/json' -d '{"arn": "arn:aws:lambda:eu-central-1:000000000000:function:my-greeter:1" }'
```
Replace the Lambda function ARN in the data field.
</TabItem>
</Tabs>
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!

</Step>
<Step stepLabel="8" title="Invoke the handler">
<Step stepLabel="7" title="Invoke the handler">

```shell
curl localhost:8080/Greeter/greet -H 'content-type: application/json' -d '"Hi"'
Expand Down

0 comments on commit 16c5067

Please sign in to comment.