Skip to content

Conversation

benbpyle
Copy link
Contributor

  • 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
Copy link
Contributor

@anitarua anitarua left a 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
Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

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?

Comment on lines 41 to 42
"cacheName": "SampleCache",
"topicName": "sample-topic"
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
npm -gi aws-cdk
npm install -gi aws-cdk

Comment on lines 27 to 33
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 },
})],
});
Copy link
Contributor

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();

@benbpyle
Copy link
Contributor Author

@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?

@anitarua
Copy link
Contributor

anitarua commented Jan 13, 2025

@benbpyle I tried adding that exclude array it suggested to the root Cargo.toml but still seeing the same error :/

Do you know if it's still possible to deploy the lambda if we go the other suggested route (empty [workspace] in the client-sdk-rust/example/aws/token-vending-machine/lambdas/Cargo.toml)? I'm not sure if that affects the Lambda-related bundling steps

@benbpyle
Copy link
Contributor Author

@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]
@benbpyle
Copy link
Contributor Author

@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 [workspace] caused Cargo Lambda to have issues.

@anitarua
Copy link
Contributor

@benbpyle I'll check it out, thanks!

Also could you re-sync this fork with main to get updated CI workflow file again?

@benbpyle
Copy link
Contributor Author

@benbpyle I'll check it out, thanks!

Also could you re-sync this fork with main to get updated CI workflow file again?

Done.

@anitarua
Copy link
Contributor

@benbpyle Thanks!

And I tested out the deployment and it went very smoothly! 🙌
The token vending machine lambda is so fast 😄

* Simplified the Scopes as released in v0.47.1
* Updated the README
@benbpyle
Copy link
Contributor Author

@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

@anitarua
Copy link
Contributor

@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?

Comment on lines +6 to +7
# [workspace]
#
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# [workspace]
#

--header 'Content-Type: application/json' \
--data '{
"cacheName": "SampleCache",
"topicName": "sample-topic"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"topicName": "sample-topic"
"topicName": "SampleTopic"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants