Skip to content

The example code does not work #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nabezokodaikon opened this issue Aug 18, 2020 · 6 comments
Closed

The example code does not work #254

nabezokodaikon opened this issue Aug 18, 2020 · 6 comments

Comments

@nabezokodaikon
Copy link

An error occurs in the top use lambda::{handler_fn, Context}; of hello-without-macro.rs.

use lambda::{handler_fn, Context};

The error details are as follows.

unresolved import `lambda`  use of undeclared type or module `lambda`

I am adding lambda_runtime = "0.2.1" to Cargo.toml's [dependencies].

@nabezokodaikon nabezokodaikon changed the title The example code does not work. The example code does not work Aug 18, 2020
@nabezokodaikon
Copy link
Author

If you describe in cargo.toml as follows, it was done.

lambda = { git = "https://github.com/awslabs/aws-lambda-rust-runtime/", branch = "master"}

@rimutaka
Copy link
Contributor

@nabezokodaikon, sorry we had to put you through this. The official crate is a bit out of date.

Can you try using this reference to help us test the latest version?

lambda = { git = "https://github.com/rimutaka/aws-lambda-rust-runtime.git", branch = "davidbarsky/update-readme"}

It relates to PR #244 and has some major improvements, including better error handling.
I'll be on standby if you spot any issues. I'm trying it with my Lambdas right now as well.
The code, docs and examples are here https://github.com/rimutaka/aws-lambda-rust-runtime/tree/davidbarsky/update-readme

Any feedback or questions are welcome.

@nabezokodaikon
Copy link
Author

Thank you response.

On that branch, I got the following error.

error: future cannot be shared between threads safely
  --> src/main.rs:92:5
   |
92 |     lambda::run(handler_fn(my_handler)).await?;
   |     ^^^^^^^^^^^ future returned by `my_handler` is not `Sync`
   |
   = help: the trait `std::marker::Sync` is not implemented for `dyn std::future::Future<Output = std::result::Result<rusoto_dynamodb::generated::PutItemOutput, rusoto_core::error::RusotoError<rusoto_dynamodb::generated::PutItemError>>> + std::marker::Send`
note: future is not `Sync` as it awaits another future which is not `Sync`
  --> src/main.rs:65:11
   |
65 |     match client.put_item(create_serials).await {
   |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ await occurs here on type `std::pin::Pin<std::boxed::Box<dyn std::future::Future<Output = std::result::Result<rusoto_dynamodb::generated::PutItemOutput, rusoto_core::error::RusotoError<rusoto_dynamodb::generated::PutItemError>>> + std::marker::Send>>`, which is not `Sync`

Can not because the client.put_item is the await even though the lambda::run function is seeking the synchronization function.

My code is below.

use lambda::{handler_fn, Context};
use log::error;
use rusoto_core::Region;
use rusoto_dynamodb::{
    AttributeValue, DeleteItemInput, DynamoDb, DynamoDbClient, GetItemInput, PutItemInput,
    UpdateItemInput,
};
use serde_derive::{Deserialize, Serialize};
use simple_error::bail;
use std::collections::HashMap;

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[derive(Deserialize)]
struct CustonEvent {
    name: String,
}

#[derive(Serialize)]
struct CustomOutput {
    message: String,
}

async fn put(e: CustonEvent) -> Result<CustomOutput, Error> {
    let mut create_key: HashMap<String, AttributeValue> = HashMap::new();
    create_key.insert(
        String::from("user_id"),
        AttributeValue {
            s: Some(e.name),
            ..Default::default()
        },
    );

    create_key.insert(
        String::from("timestamp"),
        AttributeValue {
            n: Some(1.to_string()),
            ..Default::default()
        },
    );

    create_key.insert(
        String::from("comment"),
        AttributeValue {
            s: Some("本日は雨なり".to_string()),
            ..Default::default()
        },
    );

    create_key.insert(
        String::from("state"),
        AttributeValue {
            n: Some(3.to_string()),
            ..Default::default()
        },
    );

    let create_serials = PutItemInput {
        table_name: String::from("ywt-table"),
        item: create_key,
        ..Default::default()
    };

    let client = DynamoDbClient::new(Region::UsEast1);
    match client.put_item(create_serials).await {
        Ok(result) => match result.attributes {
            Some(_) => println!("some"),
            None => println!("none"),
        },
        Err(error) => {
            panic!("Error: {:?}", error);
        }
    };

    Ok(CustomOutput {
        message: format!("Success"),
    })
}

async fn my_handler(e: CustonEvent, c: Context) -> Result<CustomOutput, Error> {
    if e.name == "" {
        error!("Empty request_id in request {}", c.request_id);
        bail!("Empty request_id in request");
    }

    put(e).await
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    simple_logger::init_with_level(log::Level::Debug)?;
    lambda::run(handler_fn(my_handler)).await?;
    Ok(())
}

@rimutaka
Copy link
Contributor

rimutaka commented Aug 19, 2020 via email

@rimutaka
Copy link
Contributor

@nabezokodaikon, I fixed that bug. The same branch should work OK now. I'll be on standby if there are any other problems.

@nabezokodaikon
Copy link
Author

I confirmed that it works normally. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants