java 如何将参数传递给 AWS Lambda 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34633308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass parameters to AWS Lambda function
提问by sengbatz
I know that it is possible to pass parameters to a Java program running on AWS Lambda in order to test it. But I do not know how to pass those parameters if this program gets called by a schedule event.
我知道可以将参数传递给在 AWS Lambda 上运行的 Java 程序以对其进行测试。但是如果这个程序被调度事件调用,我不知道如何传递这些参数。
Does anyone know if this is possible? If yes, how? I was not able to find anything about it.
有谁知道这是否可能?如果是,如何?我找不到任何关于它的信息。
Thanks in advance
提前致谢
采纳答案by Yerken
You can pass parameters indirectly, for example by storing those on s3 or dynamo db. On each scheduled cycle, you can read from the predetermined location and pass it to your program. Normally, if I need to launch multiple scheduled lambda functions with shared source code, I bundle a file (any readable format) with the source code itself and read it during the run. @garnaat is right, furthermore it is currently impossible to dynamically launch new lambda functions, which is a great inconvenience.
您可以间接传递参数,例如通过将参数存储在 s3 或 dynamo db 上。在每个预定的周期中,您可以从预定位置读取并将其传递给您的程序。通常,如果我需要使用共享源代码启动多个预定的 lambda 函数,我会将一个文件(任何可读格式)与源代码本身捆绑在一起,并在运行期间读取它。@garnaat 是对的,而且目前无法动态启动新的 lambda 函数,这是一个很大的不便。
回答by Diogo Eichert
You could leverage the environment variables from the lambda configuration. Then you read them on your Java program with:
您可以利用 lambda 配置中的环境变量。然后你在你的 Java 程序中阅读它们:
System.getenv("VAR_NAME")
回答by Sergey Nikitin
You can create CloudWatch rule as a schedule and configure target's input as a constant json value:
您可以将 CloudWatch 规则创建为计划并将目标的输入配置为常量 json 值:
CloudWatch -> Events -> Rules -> Schedule -> Targers
CloudWatch -> 事件 -> 规则 -> 计划 -> 目标
Set Configure inputas Сonstant (JSON text)and pass any valid json-data there.
将配置输入设置为Сonstant(JSON 文本)并在那里传递任何有效的 json-data。
In Lambda you can access constant input as an input object.
在 Lambda 中,您可以将常量输入作为输入对象访问。
回答by Santiago Trias
Another option, if you have automated deploys or CI, is to insert your arguments on build time with maven(or similar) and properties. I guess it's a little more optimized as you don't have to read a file from S3?
如果您有自动部署或 CI,另一种选择是在构建时使用 maven(或类似)和属性插入您的参数。我想它稍微优化了一点,因为您不必从 S3 读取文件?
For example you can have config/env1/app.properties
and config/env2/app.properties
例如,您可以拥有config/env1/app.properties
和config/env2/app.properties
The with maven you create profiles that load the respective config like:
使用 maven,您可以创建加载相应配置的配置文件,例如:
...
<profiles>
<profile>
<id>env1</id>
<build>
<resources>
<resource>
<directory>config/env1</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
<profile>
<id>env2</id>
<build>
<resources>
<resource>
<directory>config/env2</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
</profiles>
...
In your code read the props as usual props.load(ClassLoader.getSystemResourceAsStream("app.properties"));
在你的代码中像往常一样阅读道具 props.load(ClassLoader.getSystemResourceAsStream("app.properties"));
In your automation you can build mvn install -P en1
to get your Jar to deploy.
在您的自动化中,您可以构建mvn install -P en1
以部署 Jar。
回答by garnaat
Currently, the only way to create a scheduled Lambda function is via the AWS Console and it does not provide any way to pass parameters to the Lambda function when the scheduler calls it. Since your Lambda function is being called by the scheduler in AWS, it's not really clear to me how parameter passing would work.
目前,创建计划的 Lambda 函数的唯一方法是通过 AWS 控制台,并且它不提供任何方式在调度程序调用 Lambda 函数时将参数传递给它。由于 AWS 中的调度程序正在调用您的 Lambda 函数,因此我不太清楚参数传递的工作原理。