| Crates.io | warp_lambda |
| lib.rs | warp_lambda |
| version | 0.1.4 |
| created_at | 2021-02-24 10:10:03.775313+00 |
| updated_at | 2022-04-20 14:36:26.527077+00 |
| description | A super simple crate to let you use [warp filters](https://github.com/seanmonstar/warp) with [aws lambda runtime](https://github.com/awslabs/aws-lambda-rust-runtime) |
| homepage | https://github.com/aslamplr/warp_lambda#warp_lambda |
| repository | https://github.com/aslamplr/warp_lambda |
| max_upload_size | |
| id | 359899 |
| size | 54,156 |
A super simple crate to let you use warp filters with aws lambda runtime
Add warp_lambda, warp and tokio to your dependencies:
tokio = { version = "1.2.0", features = [ "full" ]}
warp = "0.3"
warp_lambda = "0.1"
And then get started in your main.rs:
use warp::Filter;
#[tokio::main]
async fn main() {
// Your warp routes (filters)
let routes = warp::any().map(|| "Hello, World!");
// Convert them to a warp service (a tower service implmentation)
// using `warp::service()`
let warp_service = warp::service(routes);
// The warp_lambda::run() function takes care of invoking the aws lambda runtime for you
warp_lambda::run(warp_service)
.await
.expect("An error occured");
}
Relevant parts copied over from https://github.com/awslabs/aws-lambda-rust-runtime
To deploy the basic sample as a Lambda function using the AWS CLI, we first need to manually build it with cargo. Since Lambda uses Amazon Linux, you'll need to target your executable for an x86_64-unknown-linux-musl platform.
Run this script once to add the new target:
$ rustup target add x86_64-unknown-linux-musl
Compile one of the examples as a release with a specific target for deployment to AWS:
$ cargo build --example hello_world --release --target x86_64-unknown-linux-musl
For a custom runtime, AWS Lambda looks for an executable called bootstrap in the deployment package zip. Rename the generated basic executable to bootstrap and add it to a zip archive.
$ cp ./target/release/examples/hello ./bootstrap && zip lambda.zip bootstrap && rm bootstrap
Now that we have a deployment package (lambda.zip), we can use the AWS CLI to create a new Lambda function. Make sure to replace the execution role with an existing role in your account!
$ aws lambda create-function --function-name rustTest \
--handler doesnt.matter \
--zip-file fileb://./lambda.zip \
--runtime provided \
--role arn:aws:iam::XXXXXXXXXXXXX:role/your_lambda_execution_role \
--environment Variables={RUST_BACKTRACE=1} \
--tracing-config Mode=Active
Note: --cli-binary-format raw-in-base64-out is a required
argument when using the AWS CLI version 2. More Information
Alternatively, you can build a Rust-based Lambda function in a docker mirror of the AWS Lambda provided runtime with the Rust toolchain preinstalled.
Running the following command will start a ephemeral docker container which will build your Rust application and produce a zip file containing its binary auto-renamed to bootstrap to meet the AWS Lambda's expectations for binaries under target/lambda/release/{your-binary-name}.zip, typically this is just the name of your crate if you are using the cargo default binary (i.e. main.rs)
# build and package deploy-ready artifact
$ docker run --rm \
-v ${PWD}:/code \
-v ${HOME}/.cargo/registry:/root/.cargo/registry \
-v ${HOME}/.cargo/git:/root/.cargo/git \
softprops/lambda-rust
Recommended to use API Gateway with HTTP API with following parameters.
API Type: HTTP
Method: ANY
Resource Path: /{proxy+}
MIT