我尝试在Rust中创建一个AWS Lambda函数。一切都编译和部署良好,但调用失败。
我已经创建了一个最小可再现的例子(MRE),并包括了一个基于Docker的可重复性构建。请记住,这是一个MRE,所以你可能会看到一些东西,这是不必要的一个基本的hello world例子,但他们是不可或缺的原始项目,我试图排除故障。
- 代码**
main.rs:
use hello::search::search;
use lambda_runtime::{service_fn, Error};
#[tokio::main]
async fn main() -> Result<(), Error> {
eprintln!("in main now!");
let handler = service_fn(search);
match lambda_runtime::run(handler).await {
Ok(()) => eprintln!("Lambda handler exited successfully!"),
Err(err) => eprintln!("Lambda handler failed: {}", err),
}
Ok(())
}
search.rs:
use lambda_runtime::{LambdaEvent, Error};
use serde_json::{json, Value};
pub async fn search(_: LambdaEvent<Option<String>>) -> Result<Value, Error> {
eprintln!("hello, logs!");
Ok(json!({
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\"message\": \"hello, world!\"}"
}))
}
lib.rs:
pub mod search;
- 配置**
template.yml:
AWSTemplateFormatVersion: 2010-09-09
Description: >-
my-api
Transform:
- AWS::Serverless-2016-10-31
Resources:
HelloRustFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: HelloRust
Handler: bootstrap.is.real.handler
Runtime: provided.al2
MemorySize: 512
CodeUri: .
Events:
Api:
Type: Api
Properties:
Path: /
Method: POST
RestApiId:
Ref: ApiGatewayRestApi
Metadata:
BuildMethod: makefile
ApiGatewayRestApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Outputs:
ApiGatewayUrl:
Value: !Sub "https://${ApiGatewayRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
Description: "API Gateway URL"
Cargo.toml:
[package]
name = "hello"
version = "0.1.0"
authors = ["root <root@localhost>"]
edition = "2018"
autobins = false
[[bin]]
name = "bootstrap"
path = "src/main.rs"
[dependencies]
lambda_runtime = "0.7.3"
serde = "1.0.80"
serde_derive = "1.0.80"
serde_json = "1.0.33"
http = "0.2.1"
regex = "1.7.1"
async-std = "1.12.0"
tokio = "1.25.0"
# https://stackoverflow.com/questions/74755175/unable-to-compile-rust-aws-lambda-using-rusoto
rusoto_core = {version = "0.48.0", features = ["rustls"], default-features = false}
rusoto_dynamodb = {version = "0.48.0", features = ["rustls"], default-features = false}
[profile.release]
opt-level = 'z' # Optimize for size
lto = true # Enable link-time optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations
panic = 'abort' # Abort on panic
strip = true # Strip symbols from binary*
[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "target-feature=+crt-static"]
生成文件:
build-HelloRustFunction:
cargo build --release --target x86_64-unknown-linux-musl
cp ./target/x86_64-unknown-linux-musl/release/bootstrap $(ARTIFACTS_DIR)
- 构建和部署**
停靠文件:
# Docker build file, not used at runtime
FROM amazonlinux:2
RUN yum install -y openssl-devel gcc python3 python3-pip zip make tar gzip wget
RUN pip3 install awscli aws-sam-cli
# install musl
WORKDIR /root
RUN wget http://musl.libc.org/releases/musl-latest.tar.gz -O /tmp/musl-latest.tar.gz && \
tar zxvf /tmp/musl-latest.tar.gz && \
cd musl-* && \
./configure && \
make install
RUN ln -s /usr/local/musl/bin/musl-gcc /usr/bin
# install Rust
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
RUN rustup target add x86_64-unknown-linux-musl
# force crates.io update
RUN cargo search rusoto_dynamodb
CMD ["/bin/bash"]
构建_部署. sh:
#!/bin/bash
docker build -t my-rust-project .
docker run -it -v "$(pwd)":/root/project -w /root/project -v ${HOME}/.aws/credentials:/root/.aws/credentials:ro my-rust-project sam build
docker run -it -v "$(pwd)":/root/project -w /root/project -v ${HOME}/.aws/credentials:/root/.aws/credentials:ro my-rust-project sam deploy
- 问题是**
构建成功并创建AWS Lambda函数。
当我通过curl
调用API网关端点的函数时,我得到:
{" message ":"内部服务器错误"}
检查Cloudwatch日志,我看到in main now!
,但没有hello, logs!
。
就好像service_fn()
抽象在做一些奇怪的事情,从来没有调用我的search()
函数。
- 是什么导致了这一切**
1条答案
按热度按时间u5rb5r591#
问题出在
search.rs
中:将LambdaEvent<Option<String>>
更改为LambdaEvent<Option<Value>>
。遗憾的是,Rust Lambda运行时不记录解析请求json的失败。