Skip to content

Instantly share code, notes, and snippets.

@naokiri
Created October 11, 2020 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naokiri/aa9559fecbbe7e546df1e4d54795f192 to your computer and use it in GitHub Desktop.
Save naokiri/aa9559fecbbe7e546df1e4d54795f192 to your computer and use it in GitHub Desktop.
aws-lambda-runtime 0.3 or lambda example
extern crate lambda;
#[macro_use]
extern crate serde;
use rusoto_dynamodb::{DynamoDbClient, DynamoDb, PutItemInput, AttributeValue};
use rusoto_core::Region;
use std::collections::HashMap;
use uuid::Uuid;
use lambda::{handler_fn, Context};
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
#[tokio::main]
async fn main() -> Result<(), Error> {
let func = handler_fn(handler);
lambda::run(func).await?;
Ok(())
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CustomEvent {
param: String,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CustomResult {
result: String,
}
async fn handler(event: CustomEvent, _context: Context) -> Result<CustomResult, Error> {
let client = DynamoDbClient::new(Region::ApNortheast1);
let mut input = PutItemInput::default();
let mut entry = HashMap::new();
entry.insert("id".to_string(), AttributeValue { s: Option::from(Uuid::new_v4().to_hyphenated().to_string()), ..Default::default() });
entry.insert("name".to_string(), AttributeValue { s: Option::from(event.param), ..Default::default() });
input.item = entry;
input.table_name = "example".to_string();
let result = client.put_item(input).await?;
Ok(CustomResult { result: format!("it = '{:?}'", result) }) # 例としてDebugを返しているが、PutItemの場合resultには特に何もない
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment