Funqy AWS Lambda Binding

指南中将对快速入门代码进行介绍,向您展示如何将 Funqy 函数部署到 AWS Lambda。

The guide walks through quickstart code to show you how you can deploy Funqy functions to AWS Lambda.

可以使用 AWS Lambda Java 运行时部署 Funqy 函数,或者在需要更小的内存占用和更快的冷启动时间时,您可以构建原生可执行文件并使用 Lambda 自定运行时。

Funqy functions can be deployed using the AWS Lambda Java Runtime, or you can build a native executable and use Lambda Custom Runtime if you want a smaller memory footprint and faster cold boot startup time. Unresolved directive in funqy-aws-lambda.adoc - include::{includes}/extension-status.adoc[]

Prerequisites

include::{includes}/prerequisites.adoc[]* 阅读有关 Funqy Basics 的部分。这部分内容篇幅很短!* An Amazon AWS account* AWS CLI* AWS SAM CLI,用于本地测试

Unresolved directive in funqy-aws-lambda.adoc - include::{includes}/prerequisites.adoc[] * Read about Funqy Basics. This is a short read! * An Amazon AWS account * AWS CLI * AWS SAM CLI, for local testing

Funqy AWS Lambdas 构建于我们的 Quarkus AWS Lambda support 之上。

Funqy AWS Lambdas build off of our Quarkus AWS Lambda support.

Installing AWS bits

安装所有 AWS 位可能是这个指南最困难的部分。请务必遵循所有步骤来安装 AWS CLI。

Installing all the AWS bits is probably the most difficult thing about this guide. Make sure that you follow all the steps for installing AWS CLI.

The Quickstart

克隆 Git 存储库: git clone {quickstarts-clone-url},或下载 {quickstarts-archive-url}[存档]。

Clone the Git repository: git clone {quickstarts-clone-url}, or download an {quickstarts-archive-url}[archive].

解决方案位于 funqy-amazon-lambda-quickstart directory 中。

The solution is located in the funqy-amazon-lambda-quickstart directory.

The Code

代码没什么特别的,更重要的是没有什么内容是特定于 AWS 的。Funqy 函数可以部署到许多环境,而 AWS Lambda 就是其中之一。Java 代码实际上与 funqy-http-quickstart 的代码完全相同。

There is nothing special about the code and more importantly nothing AWS specific. Funqy functions can be deployed to many environments and AWS Lambda is one of them. The Java code is actually the same exact code as the funqy-http-quickstart.

Choose Your Function

每个 AWS Lambda 部署只能导出一个 Funqy 函数。如果您在项目中定义了多个函数,那么您需要选择 Quarkus application.properties 中的函数:

Only one Funqy function can be exported per AWS Lambda deployment. If you have multiple functions defined within your project, then you will need to choose the function within your Quarkus application.properties:

quarkus.funqy.export=greet

您可以看到快速入门是如何在自己的 application.properties 中完成的。

You can see how the quickstart has done it within its own application.properties.

或者,您可以在使用 aws cli 创建 AWS Lambda 时设置 QUARKUS_FUNQY_EXPORT 环境变量。

Alternatively, you can set the QUARKUS_FUNQY_EXPORT environment variable when you create the AWS Lambda using the aws cli.

Deploy to AWS Lambda Java Runtime

要想在 AWS Lambda 上运行您的 Funqy 函数,有一些步骤。快速入门 maven 项目会生成一个有用的脚本,用于创建、更新、删除和调用纯 Java 函数和原生部署。此脚本在构建时生成。

There are a few steps to get your Funqy function running on AWS Lambda. The quickstart maven project generates a helpful script to create, update, delete, and invoke your functions for pure Java and native deployments. This script is generated at build time.

Build and Deploy

使用 Maven 构建项目:

Build the project using Maven:

Unresolved directive in funqy-aws-lambda.adoc - include::{includes}/devtools/build.adoc[]

这将编译和打包你的代码。

This will compile and package your code.

Create an Execution Role

查看 Getting Started Guide 以使用 AWS CLI 部署 Lambda。具体来说,请确保你已经创建了一个 Execution Role。你将需要在你的个人资料或控制台窗口中定义一个 LAMBDA_ROLE_ARN 环境变量,或者,你可以在构建生成的 manage.sh 脚本中编辑并将角色值直接放在其中:

View the Getting Started Guide for deploying a lambda with AWS CLI. Specifically, make sure you have created an Execution Role. You will need to define a LAMBDA_ROLE_ARN environment variable in your profile or console window, Alternatively, you can edit the manage.sh script that is generated by the build and put the role value directly there:

LAMBDA_ROLE_ARN="arn:aws:iam::1234567890:role/lambda-role"

Extra Build Generated Files

在你运行构建后,quarkus-funqy-amazon-lambda 扩展会生成一些额外的文件。这些文件在构建目录中:target/ 用于 Maven,build/ 用于 Gradle。

After you run the build, there are a few extra files generated by the quarkus-funqy-amazon-lambda extension. These files are in the build directory: target/ for maven, build/ for gradle.

  • function.zip - lambda deployment file

  • manage.sh - wrapper around aws lambda cli calls

  • bootstrap-example.sh - example bootstrap script for native deployments

  • sam.jvm.yaml - (optional) for use with sam cli and local testing

  • sam.native.yaml - (optional) for use with sam cli and native local testing

Create the function

target/manage.sh 脚本用于使用 AWS Lambda Java 运行时管理你的 Funqy 函数。该脚本仅为你的方便提供。如果你想要了解用于创建、删除和更新你的函数的 aws 命令,请检查 manage.sh 脚本的输出。

The target/manage.sh script is for managing your Funqy function using the AWS Lambda Java runtime. This script is provided only for your convenience. Examine the output of the manage.sh script if you want to learn what aws commands are executed to create, delete, and update your functions.

manage.sh 支持四种操作:createdeleteupdate`和 `invoke

manage.sh supports four operation: create, delete, update, and invoke.

为了验证你的设置,你已经安装了 AWS CLI,执行了 AWS 访问密钥的 aws configure,并且设置了 LAMBDA_ROLE_ARN 环境变量(如上所述),请执行 manage.sh,而没有任何参数。一个用法声明将被打印给你相应地提供指导。

To verify your setup, that you have the AWS CLI installed, executed aws configure for the AWS access keys, and set up the LAMBDA_ROLE_ARN environment variable (as described above), please execute manage.sh without any parameters. A usage statement will be printed to guide you accordingly.

以下载 usage 陈述,以及验证 AWS 配置:

To see the usage statement, and validate AWS configuration:

sh target/manage.sh

你可以使用以下命令 create 你的函数:

You can create your function using the following command:

sh target/manage.sh create

或者,如果你尚未在这个 shell 中定义 LAMBDA_ROLE_ARN

or if you do not have LAMBDA_ROLE_ARN already defined in this shell:

LAMBDA_ROLE_ARN="arn:aws:iam::1234567890:role/lambda-role" sh target/manage.sh create

不要更改处理程序开关。这必须硬编码为 io.quarkus.funqy.lambda.FunqyStreamHandler::handleRequest。这个特殊的处理程序是 Funqy 与 AWS Lambda 的集成点。

Do not change the handler switch. This must be hardcoded to io.quarkus.funqy.lambda.FunqyStreamHandler::handleRequest. This special handler is Funqy’s integration point with AWS Lambda.

如果有任何问题在创建函数时发生,你需要使用 delete 删除它,然后重新运行 create 命令。

If there are any problems creating the function, you must delete it with the delete function before re-running the create command.

sh target/manage.sh delete

命令也可以被叠加:

Commands may also be stacked:

sh target/manage.sh delete create

Invoke the function

使用 invoke 命令来调用你的函数。

Use the invoke command to invoke your function.

sh target/manage.sh invoke

示例函数使用通过 --payload 开关传递的输入,该开关指向项目根目录中的一个 JSON 文件。

The example function takes input passed in via the --payload switch which points to a json file in the root directory of the project.

使用 SAM CLI 还可以像这样本地调用函数:

The function can also be invoked locally with the SAM CLI like this:

sam local invoke --template target/sam.jvm.yaml --event payload.json

如果使用的是本机映像构建,则只需将模板名称替换为本机版本:

If you are working with your native image build, simply replace the template name with the native version:

sam local invoke --template target/sam.native.yaml --event payload.json

Update the function

您可以根据需要更新 Java 代码。重建后,您可以通过执行 update 命令重新部署函数。

You can update the Java code as you see fit. Once you’ve rebuilt, you can redeploy your function by executing the update command.

sh target/manage.sh update

Deploy to AWS Lambda Custom (native) Runtime

如果您想要 Funqy 函数更低的内存占用和更快的初始化时间,可以将 Java 代码编译为本机可执行文件。请确保使用 -Dnative 开关重建项目。

If you want a lower memory footprint and faster initialization times for your Funqy function, you can compile your Java code to a native executable. Just make sure to rebuild your project with the -Dnative switch.

对于 Linux 主机,执行以下操作:

For Linux hosts execute:

Unresolved directive in funqy-aws-lambda.adoc - include::{includes}/devtools/build-native.adoc[]

如果您在非 Linux 系统上进行构建,还需要传入一个属性,指示 Quarkus 使用 Docker 来构建,因为 AmazonLambda 需要 Linux 二进制文件。可以通过将此属性传入构建来实现: -Dnative-image.docker-build=true。但是,这需要在本地安装 Docker。

If you are building on a non-Linux system, you will need to also pass in a property instructing Quarkus to use a Docker build as Amazon Lambda requires Linux binaries. You can do this by passing this property to your build: -Dnative-image.docker-build=true. This requires you to have Docker installed locally, however.

Unresolved directive in funqy-aws-lambda.adoc - include::{includes}/devtools/build-native-container.adoc[]

上述命令都将编译并创建一个本机可执行文件。它还生成一个 zip 文件 target/function.zip。此 zip 文件包含重命名为 bootstrap 的本机可执行映像文件。这是 AWS LambdaCustom(提供)Runtime 的要求。

Either of these commands will compile and create a native executable. It also generates a zip file target/function.zip. This zip file contains your native executable image renamed to bootstrap. This is a requirement of the AWS Lambda Custom (Provided) Runtime.

此处的说明与上面完全相同,只有一处更改:您需要将 native 添加为 manage.sh 脚本的第一个参数:

The instructions here are exactly as above with one change: you’ll need to add native as the first parameter to the manage.sh script:

sh target/manage.sh native create

与上面一样,可以堆叠命令。唯一的要求是,如果您希望使用本机映像构建,则第一个参数必须是 native。该脚本将负责管理本机映像函数部署所需的其余详细信息。

As above, commands can be stacked. The only requirement is that native be the first parameter should you wish to work with native image builds. The script will take care of the rest of the details necessary to manage your native image function deployments.

如果您想知道执行 aws 命令以创建、删除和更新函数,请检查 manage.sh 脚本的输出。

Examine the output of the manage.sh script if you want to learn what aws commands are executed to create, delete, and update your functions.

关于本机创建命令要注意的一件事是 aws lambda create-function 调用必须设置一个特定的环境变量:

One thing to note about the create command for native is that the aws lambda create-function call must set a specific environment variable:

--environment 'Variables={DISABLE_SIGNAL_HANDLERS=true}'

Examine the POM

除了将 quarkus-funqy-amazon-lambda 扩展作为依赖关系之外,POM 没有其他特殊之处。该扩展会自动生成 lambda 部署所需的一切。

There is nothing special about the POM other than the inclusion of the quarkus-funqy-amazon-lambda extension as a dependency. The extension automatically generates everything you might need for your lambda deployment.

Integration Testing

Funqy AWS Lambda 支持利用 Quarkus AWS Lambda 测试框架,以便您可以对 Funqy 函数进行单元测试。这适用于 JVM 和本机模式。该测试框架提供与 SAM CLI 类似的功能,而无需 Docker 的开销。

Funqy AWS Lambda support leverages the Quarkus AWS Lambda test framework so that you can unit tests your Funqy functions. This is true for both JVM and native modes. This test framework provides similar functionality to the SAM CLI, without the overhead of Docker.

如果您打开 FunqyTest.java,您会看到该测试复制了 AWS 执行环境。

If you open up FunqyTest.java you’ll see that the test replicates the AWS execution environment.

package org.acme.funqy;

import io.quarkus.amazon.lambda.test.LambdaClient;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

@QuarkusTest
public class FunqyTest {
    @Test
    public void testSimpleLambdaSuccess() throws Exception {
        Friend friend = new Friend("Bill");
        Greeting out = LambdaClient.invoke(Greeting.class, friend);
        Assertions.assertEquals("Hello Bill", out.getMessage());
    }
}

Testing with the SAM CLI

AWS SAM CLI 允许您在模拟的 Lambda 环境中在自己的笔记本电脑上本地运行函数。这需要安装 docker。这是一个可选途径,如果您选择使用,应予以考虑。否则,Quarkus JUnit 集成应该可以满足您的大部分需求。

The AWS SAM CLI allows you to run your functions locally on your laptop in a simulated Lambda environment. This requires docker to be installed. This is an optional approach should you choose to take advantage of it. Otherwise, the Quarkus JUnit integration should be sufficient for most of your needs.

已为 JVM 和本机执行模式生成了一个 starter 模板。

A starter template has been generated for both JVM and native execution modes.

运行以下 SAM CLI 命令以在本地测试函数,传入适当的 SAM templateevent 参数采用任何 JSON 文件,在本例中为示例 payload.json

Run the following SAM CLI command to locally test your function, passing the appropriate SAM template. The event parameter takes any JSON file, in this case the sample payload.json.

sam local invoke --template target/sam.jvm.yaml --event payload.json

还可以使用 sam.native.yaml 模板在本地测试本机映像:

The native image can also be locally tested using the sam.native.yaml template:

sam local invoke --template target/sam.native.yaml --event payload.json

Modifying function.zip

有时您可能不得不在构建生成的文件中添加额外的条目到 function.zip lambda 部署。要执行此操作,请在 src/main 内创建一个 zip.jvmzip.native 目录。如果要执行纯 Java,请创建 zip.jvm/。如果要执行本机部署,请创建 zip.native/

There are times when you may have to add additional entries to the function.zip lambda deployment that is generated by the build. To do this, create a zip.jvm or zip.native directory within src/main. Create zip.jvm/ if you are doing a pure Java. zip.native/ if you are doing a native deployment.

您在 zip 目录下创建的任何文件和目录都将包含在 function.zip

Any you files and directories you create under your zip directory will be included within function.zip

Custom bootstrap script

有时您可能希望在 lambda 调用您的本机 Funqy 部署时设置特定的系统属性或其他参数。如果您在 bootstrap 中包含 zip.native 脚本文件,Funqy 扩展将自动将可执行文件重命名为 runnerfunction.zip 中并将 bootstrap 脚本的 Unix 模式设置为可执行。

There are times you may want to set specific system properties or other arguments when lambda invokes your native Funqy deployment. If you include a bootstrap script file within zip.native, the Funqy extension will automatically rename the executable to runner within function.zip and set the unix mode of the bootstrap script to executable.

如果您包含自定义 bootstrap 脚本,则必须将本机可执行文件引用为 runner

The native executable must be referenced as runner if you include a custom bootstrap script.

该扩展将在 target/bootstrap-example.sh 中生成一个示例脚本来。

The extension generates an example script within target/bootstrap-example.sh.