feat: Create example of a Token Vending Machine#436
feat: Create example of a Token Vending Machine#436benbpyle wants to merge 4 commits intomomentohq:mainfrom
Conversation
benbpyle
commented
Jan 11, 2025
- Port of the TypeScript Token vending machine with slight variation
- Implemented Lambda Function in Rust that leverages the Momento Auth Client to scope read/write to a specific Topic supplied by the client
- Infrastructure is deployed via CDK and TypeScript using Cargo Lambda
- README file with specifics about payloads and responses
* Port of the TypeScript Token vending machine with slight variation * Implemented Lambda Function in Rust that leverages the Momento Auth Client to scope read/write to a specific Topic supplied by the client * Infrastructure is deployed via CDK and TypeScript using Cargo Lambda * README file with specifics about payloads and responses
anitarua
left a comment
There was a problem hiding this comment.
New example is looking good, but I'm having trouble when trying to deploy it on my own.
Bundling asset RustTokenVendingMachineStack/TokenVendingMachineFunction/Code/Stage...
Error: × `cargo metadata` exited with an error: error: current package believes it's in a workspace when
│ it's not:
│ current: client-sdk-rust/example/aws/token-vending-machine/lambdas/Cargo.toml
│ workspace: client-sdk-rust/Cargo.toml
│
│ this may be fixable by adding `example/aws/token-vending-machine/lambdas` to the
│ `workspace.members` array of the manifest located at: client-sdk-rust/Cargo.toml
│ Alternatively, to keep it out of the workspace, add the package to the `workspace.exclude` array,
│ or add an empty `[workspace]` table to the package's manifest.
I tried adding the empty workspace table to client-sdk-rust/example/aws/token-vending-machine/lambdas/Cargo.toml but it produced another error when trying to deploy:
client-sdk-rust/example/aws/token-vending-machine/infra/node_modules/cargo-lambda-cdk/src/bundling.ts:203
throw new Error('the Cargo manifest is a workspace, use the option `binaryName` to specify the binary to build');
I don't think I've worked with cargo-lambda-cdk before. Have you run into this before?
|
|
||
| - Follow the [installation guide](https://doc.rust-lang.org/cargo/getting-started/installation.html) to install Rust and Cargo. | ||
| - You will also need the [cargo-lambda cargo extension](https://www.cargo-lambda.info/) | ||
| - The CDK code in this repo is written in TypeScript, so you will need `Node.js` version 16 or later, and a compatible |
There was a problem hiding this comment.
I think Lambda doesn't support node 16 anymore, I think node 18 at minimum
There was a problem hiding this comment.
@anitarua this is just to run CDK. The Lambda won't be deployed under the Node Runtime. I'll bump to 18 though as it's currently in LTS
|
|
||
| After the lambda is deployed, you can visit the AWS console and click the "Test" button to run it! You can use the test event in the file `lambdas/test-events/test-event-1.json`. The body of that event will match the `struct` in `lambdas/src/models.rs` documented as the input payload. The content will be base64 encoded because that's what API Gateway will do to your input. But when decoded, it will reveal this payload. | ||
|
|
||
| ```json |
There was a problem hiding this comment.
nit: should be bash for this code block?
| "cacheName": "SampleCache", | ||
| "topicName": "sample-topic" |
There was a problem hiding this comment.
nit: could these have the same styling? i.e. "sample-cache" and "sample-topic" or "SampleCache" and "SampleTopic"?
| To build and test the Lambda function | ||
|
|
||
| ```bash | ||
| npm -gi aws-cdk |
There was a problem hiding this comment.
| npm -gi aws-cdk | |
| npm install -gi aws-cdk |
| let scopes = DisposableTokenScope::Permissions::<String>(Permissions { | ||
| permissions: vec![Permission::TopicPermission(TopicPermission { | ||
| role: TopicRole::PublishSubscribe, | ||
| cache: CacheSelector::CacheName { name: cache_name }, | ||
| topic: TopicSelector::TopicName { name: topic_name }, | ||
| })], | ||
| }); |
There was a problem hiding this comment.
after upgrading momento to v0.47.1, this can be simplified to:
let scopes = PermissionScopes::topic_publish_subscribe(cache_name, topic_name).into();
|
@anitarua I had the same trouble deploying the other samples. Being that they look like they are in the root Workspace, should we exclude them? I didn't want to mess with the root Cargo.toml. What do you think? |
|
@benbpyle I tried adding that Do you know if it's still possible to deploy the lambda if we go the other suggested route (empty |
|
@anitarua Cargo Lambda complains about a binaryName field. I'll do some more digging and see if I can get this to work for us. |
Fixed the build by adding a workspace project in the Lambdas directory. Cargo Lambda had issues with making the Function's TOML an empty [workspace]
|
@anitarua can you pull now and try and build? I introduced a workspace for the Function so that it would build. Using an empty workspace like |
|
@benbpyle I'll check it out, thanks! Also could you re-sync this fork with main to get updated CI workflow file again? |
Done. |
|
@benbpyle Thanks! And I tested out the deployment and it went very smoothly! 🙌 |
* Simplified the Scopes as released in v0.47.1 * Updated the README
|
@anitarua made updates for the version to simplify the Scopes and edits to the README. Let me know if you need anything else to get this merged |
|
@benbpyle thanks! looks like there's a merge conflict and I think it's still using the old github actions workflow file, could you rebase against main again? |
| # [workspace] | ||
| # |
There was a problem hiding this comment.
| # [workspace] | |
| # |
| --header 'Content-Type: application/json' \ | ||
| --data '{ | ||
| "cacheName": "SampleCache", | ||
| "topicName": "sample-topic" |
There was a problem hiding this comment.
| "topicName": "sample-topic" | |
| "topicName": "SampleTopic" |