forked from aws/aws-lambda-rust-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
94 lines (80 loc) · 2.89 KB
/
Copy pathmain.rs
File metadata and controls
94 lines (80 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use aws_sdk_dynamodb::Client;
use lambda_http::{run, service_fn, tracing, Body, Error, Request, Response};
use serde::{Deserialize, Serialize};
use serde_dynamo::to_attribute_value;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Item {
pub p_type: String,
pub age: String,
pub username: String,
pub first: String,
pub last: String,
}
/// This is the main body for the function.
/// Write your code inside it.
/// You can see more examples in Runtime's repository:
/// - <https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples>
async fn handle_request(db_client: &Client, event: Request) -> Result<Response<Body>, Error> {
// Extract some useful information from the request
let body = event.body();
let s = std::str::from_utf8(body).expect("invalid utf-8 sequence");
//Log into Cloudwatch
tracing::info!(payload = %s, "JSON Payload received");
//Serialze JSON into struct.
//If JSON is incorrect, send back 400 with error.
let item = match serde_json::from_str::<Item>(s) {
Ok(item) => item,
Err(err) => {
let resp = Response::builder()
.status(400)
.header("content-type", "text/html")
.body(err.to_string().into())
.map_err(Box::new)?;
return Ok(resp);
}
};
//Insert into the table.
add_item(db_client, item.clone(), "lambda_dyno_example").await?;
//Deserialize into json to return in the Response
let j = serde_json::to_string(&item)?;
//Send back a 200 - success
let resp = Response::builder()
.status(200)
.header("content-type", "text/html")
.body(j.into())
.map_err(Box::new)?;
Ok(resp)
}
#[tokio::main]
async fn main() -> Result<(), Error> {
// required to enable CloudWatch error logging by the runtime
tracing::init_default_subscriber();
//Get config from environment.
let config = aws_config::load_from_env().await;
//Create the DynamoDB client.
let client = Client::new(&config);
run(service_fn(|event: Request| async {
handle_request(&client, event).await
}))
.await
}
// Add an item to a table.
// snippet-start:[dynamodb.rust.add-item]
pub async fn add_item(client: &Client, item: Item, table: &str) -> Result<(), Error> {
let user_av = to_attribute_value(item.username)?;
let type_av = to_attribute_value(item.p_type)?;
let age_av = to_attribute_value(item.age)?;
let first_av = to_attribute_value(item.first)?;
let last_av = to_attribute_value(item.last)?;
let request = client
.put_item()
.table_name(table)
.item("username", user_av)
.item("account_type", type_av)
.item("age", age_av)
.item("first_name", first_av)
.item("last_name", last_av);
tracing::info!("adding item to DynamoDB");
let _resp = request.send().await?;
Ok(())
}