有什么方法可以在Rust中的AWS SQS推送事件上触发AWS Lambda函数?

时间:2019-12-10 13:07:57

标签: rust aws-lambda amazon-sqs

我知道2个库rusoto和lambda_runtime用于AWS rust。但是我没有找到用lambda迭代aws sqs的任何方法。

我已经在rust中编写了这个lambda函数,并在lambda触发器中为一个sqs查询配置了该函数,但是每次触发我都找不到field_name。基本上,函数无法解析要发送到结构中的字符串json。

我已经用示例json对amb测试功能测试了lambda函数,并且它起作用了,然后我在sqs中推送了相同的json,但解析异常。

生锈的Lambda函数

#[macro_use]
extern crate serde;

#[macro_use]
extern crate log;

#[macro_use]
extern crate lambda_runtime;

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use lambda_runtime::error::HandlerError;
use lambda_runtime::Context;
#[allow(unused_imports)]
use log::{debug, error, info, warn};
use std::error::Error;

#[derive(Serialize, Deserialize)]
struct CustomInput {
    file_name: String,
    meta_data: HashMap<String, String>,
}

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

fn main() -> Result<(), Box<dyn Error>> {
    simple_logger::init_with_level(log::Level::Debug)?;
    lambda!(my_handler);

    Ok(())
}

fn my_handler(e: CustomInput, c: Context) -> Result<CustomOutput, HandlerError> {
    let msg = format!(
        "Got Dispatch Request For S3 File : {} , With meta_data {:?}",
        e.file_name, e.meta_data
    );
    debug!("{}", msg);
    Ok(CustomOutput { message: msg })
}

锈中的SQS写入功能

fn send_msg(sqs_client: &SqsClient, queue_url: &str, msg: String) -> SendMessageResult {
    let req = SendMessageRequest {
        queue_url: queue_url.to_owned(),
        message_body: msg,
        ..Default::default()
    };

    sqs_client.send_message(req).sync().expect("Failed to send msg")
}

#[test]
fn test_sqs_ops() {
    let sqs_client = create_sqs_client();

    let queues = list_queues(&sqs_client);

    let queue_url = queues.unwrap().get(0).unwrap().clone();

    let message = "{\"file_name\":\"456.csv\",\"meta_data\":{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"}}";
    send_msg(&sqs_client, queue_url.as_str(), message.to_string());
}

Cargo.toml文件

[package]
name = "sample-lambda-function"
version = "0.1.0"
authors = ["karan.patel"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rusoto_core = "0.42.0"
rusoto_sqs = "0.42.0"
#rusoto_s3 = "0.42.0"
serde = { version = "1.0.102", features = ['derive', 'rc'] }
serde_json = "1.0.41"
log = "0.4.8"
lambda_runtime = "0.1"
simple_logger = "^1"

[[bin]]
name = "bootstrap"
path = "src/main.rs"

我在lambda函数的日志中遇到错误。

ERROR [lambda_runtime::runtime] Could not parse event to type: missing field `file_name` at line 1 column 853

有人可以建议解决方案吗?

0 个答案:

没有答案