Creating the deployment package of the rust lambda
Ok let's begin with how to build the standard template of lambda. You can find the runtime for rust in aws lambda here aws lambda rust runtime. They have some nice ways of building and deploying the lambda there as well so go try them out or stick around and see how I did it.
Basic lambda functions
use lambda_runtime::{handler_fn, Context, Error}; use log::LevelFilter; use serde_json::{json, Value}; use simple_logger::SimpleLogger; #[tokio::main] async fn main() -> Result<(), Error> { // required to enable CloudWatch error logging by the runtime // can be replaced with any other method of initializing `log` SimpleLogger::new() .with_level(LevelFilter::Info) .init() .unwrap(); let func = handler_fn(my_handler); lambda_runtime::run(func).await?; Ok(()) } pub(crate) async fn my_handler(event: Event, _ctx: Context) -> Result<Value, Error> { match event.request_context.event_type { EventType::Connect => on_connect(event).await, EventType::Disconnect => on_disconnect(event).await, } Ok(json!({ "statusCode":200 })) } #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "UPPERCASE")] enum EventType { Connect, Disconnect, }
This is a minimal version of the rust code. In main it runs the lambda runtime which will accept the requests directed by AWS and forward them to my_handler. This will be the basis for the connect and disconnect lambda.
The event you get from the AWS websocket apigateway will contain information about the event-type. In this case we expect either connect or disconnect. You can instrument which lambda will be called in the cdk construct.
Packages
In the cargo toml you write down which packages you need I'll include it here. Because the executable has to be named bootstrap the [[bin]] has bootstrap. I also left in the part where I reference other package which are the parts with the shared_libs.
the rustls feature is really handy dor package your lambda but more on that later!
[package]
name = "connect_disconnect"
version = "0.1.0"
edition = "2018"
[[bin]]
name = "bootstrap"
path = "src/main.rs"
[dependencies]
lambda_runtime = "0.3.0"
log = "0.4.14"
serde = "1.0.126"
simple_logger = "1.11.0"
tokio = "1.6.1"
aws_lambda_events = "0.4.0"
http = "0.2.4"
serde_json = "1.0.68"
lazy_static = "1.4.0"
rusoto_dynamodb = {version = "0.47.0", default_features = false, features=["rustls"]}
rusoto_core = {version = "0.47.0", default_features = false, features=["rustls"]}
serde_dynamodb = "0.9.0"
rand = "0.8.4"
player_access = { path = "../shared_libs/player_access"}
websocket_messaging = { path = "../shared_libs/websocket_messaging"}
plays = { path = "../shared_libs/plays"}
Ok, so the basis is done. Let's move on to what to do in the connect and disconnect.
Thanks for reading
I enjoyed trying to make everything and I hope you'll enjoy reading it. If you are excited to have a chat or think I could improve feel free to write me on LinkedIn. I also have portfolio website where I put most of what I make and host trial projects portfolio.rohanengosia.com.