This project contains scaffolding to build and deploy a simple Smithy-modeled Rust service in an AWS Lambda fronted by Amazon API Gateway. It can serve as an example to quickly get you started with a smithy-rs service running on AWS Lambda.
It comprises three directories:
smithyis a Gradle project that builds themodel/main.smithySmithy model using smithy-rs. smithy-rs is used as a Git submodule inside this directory.- The model is built using the
rust-server-codegenplugin, which produces a server SDK in the form of a Rust crate. - The version of smithy-rs used is the one that is pointed to by the Git
submodule, defined in
.gitmodules. At the moment this points to thesmithy-rs-release-0.55.xbranch, whose latest version is0.55.2. This version must also be specified and kept up to date in themodel/smithy-build.jsonfile, in theruntimeConfig.versions.DEFAULTkey.
- The model is built using the
simple-serviceis the implementation of the actual smithy-rs service, which depends on the generated server SDK crate. It uses the lambda_http and lambda_runtime crates to implement a Lambda handler that receives HTTP requests from API Gateway.cdkis an infrastructure-as-code project to deploy the necessary infrastructure to an AWS account. It uses the CDK and is written in TypeScript.
You will need the following installed:
- Java >= 11.
- Rust toolchain.
- Node >= 18.
- The AWS CDK v2.
cargo-lambda.
Make sure you retrieve the Git submodule when cloning or checking out the
repository. The submodule should live under the smithy/smithy-rs directory.
$ git clone --recurse-submodules https://github.com/david-perez/smithy-rs-lambda-cdk.gitBuilding comprises three steps.
- The server SDK needs to be code-generated using smithy-rs.
- The service using the server SDK needs to be built.
- The CDK project needs to be built.
The project can then be deployed to an AWS account.
$ cd smithy
$ ./gradlew assembleThis should produce the server SDK in the form of a regular Rust crate,
simple-service-server-sdk, in the top-level directory. You can build this
crate using cargo to ensure the code generation process produced a valid Rust
crate:
$ cd simple-service-server-sdk
$ cargo testBuild the Rust Lambda function implementing the smithy-rs service in release mode:
$ cd simple-service
$ cargo lambda build --release --x86-64The binary should appear under target/release. It should be named
bootstrap. Copy it over to the target/cdk/release directory, where the CDK
will look into:
$ mkdir --parents target/cdk/release
$ cp target/lambda/bootstrap/bootstrap target/cdk/release/bootstrap$ cd cdk
$ npm install
$ npm run buildThe first time you deploy the function, you need to invoke cdk bootstrap.
This is to deploy the CDK toolkit stack into an AWS environment, and has
nothing to do with the fact that our Rust binary is also named bootstrap.
$ cd cdk
$ cdk bootstrapTo deploy the Lambda function stack, run this command:
$ cdk deploy -y --allTo destroy the stack:
$ cdk destroy --all