In the lambda_1 crate's folder, add the following dependencies to Cargo.toml:
[dependencies]
chrono = "0.4"
lambda_runtime = { git = "https://github.com/awslabs/aws-lambda-rust-runtime" }
log = "0.4"
rand = "0.6"
rusoto_core = {version = "0.35.0", default_features = false, features=["rustls"]}
rusoto_dynamodb = {version = "0.35.0", default_features = false, features=["rustls"]}
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
simple_logger = "1.0"
uuid = { version = "0.7", features = ["v4"] }
If you read the previous chapters, you will be familiar with all crates from the list, including lambda_runtime, which we used in the previous section of this chapter. Let's look at the types in src/main.rs that we will use from this crate:
use chrono::Utc;
use lambda_runtime::{error::HandlerError, lambda, Context};
use log::debug;
use rand::thread_rng;
use rand::seq::IteratorRandom;
use rusoto_core::Region;
use rusoto_dynamodb::{AttributeValue, DynamoDb, DynamoDbClient, PutItemError, PutItemInput, PutItemOutput};
use serde_derive::{Serialize, Deserialize};
use std::collections::HashMap;
use std::error::Error;
use uuid::Uuid;
We used the preceding types to implement the following sequence of actions:
- Parsing the request
- Finding (generating) a Unicorn instance that will be declared later
- Adding a record to the DynamoDb table
Our main function only calls a handler function that performs these steps:
fn main() -> Result<(), Box<dyn Error>> {
simple_logger::init_with_level(log::Level::Debug)?;
debug!("Starting lambda with Rust...");
lambda!(handler);
Ok(())
}
We also initialized the logger to have to read them with CloudWatch services.