-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Create example of a Token Vending Machine #436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: Create example of a Token Vending Machine #436
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Lambda doesn't support node 16 anymore, I think node 18 at minimum
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should be bash
for this code block?
"cacheName": "SampleCache", | ||
"topicName": "sample-topic" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# [workspace] | |
# |
--header 'Content-Type: application/json' \ | ||
--data '{ | ||
"cacheName": "SampleCache", | ||
"topicName": "sample-topic" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"topicName": "sample-topic" | |
"topicName": "SampleTopic" |