rust调用api
by Michael Habib
迈克尔·哈比卜(Michael Habib)
Before this year’s AWS re:Invent, there was no supported way to write AWS Lambda functions with Rust. I often come across the use of the Go runtime as a wrapper for the underlying Rust code. At re:Invent, AWS announced that its lambda functions will now be able to support any language. Users will be able to take advantage of this by using the new runtime API. Alongside that announcement, AWS also open sourced a Rust runtime and posted a quick how-to here.
在今年的AWS re:Invent之前,还没有支持使用Rust编写AWS Lambda函数的方法。 我经常遇到使用Go运行时作为基础Rust代码的包装的情况。 在re:Invent上,AWS宣布其lambda函数现在将能够支持任何语言。 用户将可以通过使用新的运行时API来利用此功能。 除了该公告,AWS还开源了Rust运行时,并在此处发布了快速操作方法。
The goal of this post is to provide enough information to get up and running with API Gateway and Rust. We will be creating a simple AWS Lambda function that will serve a single API Gateway resource. A more intermediate example using DynamoDB will be released on YouTube in March. If you are interested in being notified when that is released follow me on Twitter. Although still in progress, the code used in that video is on the intermediate
branch of the repo.
这篇文章的目的是提供足够的信息来启动和运行API Gateway和Rust。 我们将创建一个简单的AWS Lambda函数,该函数将为单个API网关资源提供服务。 使用DynamoDB的更中间的示例将在3月在YouTube上发布。 如果您有兴趣在发布时收到通知,请在Twitter上关注我。 尽管仍在进行中,但该视频中使用的代码在存储库的intermediate
分支上。
Docker
码头工人
AWS Account
AWS账户
AWS CLI (optional)
AWS CLI(可选)
Rust (optional)
防锈(可选)
Terraform (optional)
地形(可选)
Clone and fork the repo from here. AWS setup instructions can be found here.
从此处克隆并分叉仓库。 AWS设置说明可在此处找到。
To get started run git checkout scratch
and edit the Cargo.toml
in the users folder.
要开始运行git checkout scratch
并在users文件夹中编辑Cargo.toml
。
The [[bin]]
section at the bottom of the file tells cargo to generate an executable with that name. This is needed because AWS Lambda deployment packages expect a bootstrap file. Now let's shift our focus to the actual code that will handle the incoming API Gateway requests. Edit users/src/main.rs
so that it matches the file below.
文件底部的[[bin]]
部分告诉货物生成具有该名称的可执行文件。 这是必需的,因为AWS Lambda部署程序包需要引导文件。 现在,我们将重点转移到处理传入的API网关请求的实际代码上。 编辑users/src/main.rs
,使其与下面的文件匹配。
We start off by declaring a User struct that will be used for our responses. We then define a router that matches on the request method. The three functions responsible for creating responses are not_allowed
, add_user
and get_users
. Each function builds a response, deals with a potential error, and finally sends the response back.
我们首先声明一个将用于响应的User结构。 然后,我们定义一个与request方法匹配的路由器。 负责创建响应的三个函数为not_allowed
, add_user
和get_users
。 每个函数都会构建一个响应,处理潜在的错误,最后将响应发送回去。
The lambda_http
crate conveniently lets us turn server JSON values into a response with into_response()
. This is done in add_user
and get_users
when we go from a user to a JSON value then finally into a response.
lambda_http
板条箱可方便地使我们使用into_response()
将服务器JSON值转换为响应。 当我们从一个用户转到一个JSON值,然后最终返回一个响应时,这是在add_user
和get_users
完成的。
Keep in mind that none of the POST requests will actually add any users to any databases. All the add_user
method is doing here is deserializing the body and sending it back.
请记住,任何POST请求都不会实际上将任何用户添加到任何数据库中。 add_user
方法在此所做的全部工作是反序列化正文并将其发送回。
Bundling your code and dependencies for lambda can sometimes be tricky. At the time of this writing, SAM does not have a build command to bundle cargo projects. Instead of SAM, we will be using Docker and Terraform to create these deployment packages and more. Add a Dockerfile
to the users folder with the following contents.
将代码和lambda依赖项捆绑在一起有时可能很棘手。 在撰写本文时, SAM还没有构建命令来捆绑货物项目。 我们将使用Docker和Terraform而不是SAM来创建这些部署包以及更多。 将具有以下内容的Dockerfile
添加到users文件夹。
A lambci build image is used as the base image because they provide a pretty seamless building process. The Dockerfile above can be broken into five main parts:
将lambci构建映像用作基础映像,因为它们提供了非常无缝的构建过程。 上面的Dockerfile可以分为五个主要部分:
We also need a Dockerfile that will specifically be used for deploying our API Gateway resources. Create a file called Dockerfile.api
with the following contents.
我们还需要一个专门用于部署API网关资源的Dockerfile。 使用以下内容创建一个名为Dockerfile.api
的文件。
Now let's create a build script that will drive the process of creating and applying our Terraform plans. A Terraform plan is just a proposal on the resources it wants to create. Add the following contents to a build.sh
and deploy.sh
.
现在,让我们创建一个构建脚本,该脚本将驱动创建和应用Terraform计划的过程。 Terraform计划只是有关要创建的资源的建议。 将以下内容添加到build.sh
和deploy.sh
。
The build and deploy scripts leverage Docker and Terraform to create or update our AWS resources. In a nutshell, the Terraform plan to provision resources is created when build.sh
is run and that plan is then applied when running deploy.sh
. Terraform is out of the scope of this post but you can check out all the code in users/terraform
. To build and deploy our lambda resources run ./build.sh && ./deploy.sh
.
构建和部署脚本利用Docker和Terraform创建或更新我们的AWS资源。 简而言之,当运行build.sh
时会创建用于配置资源的Terraform计划,然后在运行deploy.sh
时应用该计划。 Terraform不在本文的讨论范围内,但是您可以在users/terraform
查看所有代码。 要构建和部署我们的lambda资源,请运行./build.sh && ./deploy.sh
。
If everything went well, you should see the tail of your output like the one below. Keep in mind that the Dockerfiles assume AWS_* credentials are set as environment variables.
如果一切顺利,您应该看到输出的尾部,如下所示。 请记住,Dockerfile假定将AWS_ *凭证设置为环境变量。
To now deploy our API Gateway resources run ./build.sh --api && ./deploy.sh --api.
Like the above command, if everything went well you should see the tail of your output like the one below.
现在要部署我们的API网关资源,请运行./build.sh --api && ./deploy.sh --api.
就像上面的命令一样,如果一切顺利,您应该看到输出的尾部,如下所示。
Make sure to take note of the base_url
that is displayed after deploying. To test the API, we will need to send a GET and POST request to
/users . You can use a program like Postman or the command line like the screenshot below. The two commands shown in the image assume that jq is installed.
确保记下部署后显示的base_url
。 要测试API,我们需要向
/ users发送GET和POST请求。 您可以使用Postman之类的程序,也可以使用以下屏幕截图之类的命令行。 镜像中显示的两个命令假定已安装jq。
To destroy all the resources created in this post run ./destroy.sh && ./destroy.sh --state
.
要销毁此./destroy.sh && ./destroy.sh --state
创建的所有资源,请运行./destroy.sh && ./destroy.sh --state
。
I hope you found this post useful in getting up and running with API Gateway and Rust. Feel free to comment, ask questions, or suggest a topic I can write about next.
我希望您发现这篇文章对使用API Gateway和Rust进行启动和运行很有帮助。 随时发表评论,提出问题或提出我可以写的下一个话题。
P.S. Follow me on Twitter
PS在推特上关注我
翻译自: https://www.freecodecamp.org/news/going-serverless-with-rust-and-api-gateway-aa5d1502c32e/
rust调用api