我在这里阅读自述文件:https://github.com/awslabs/aws-sam-local
我已经写在python 3.6拉姆达及其类似HelloWorld示例这里:https://github.com/awslabs/aws-sam-local/tree/develop/samples/hello-world/python
template.yml看起来像这样:
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: MyFunction1 API
Resources:
MyFunction1:
Type: AWS::Serverless::Function
Properties:
FunctionName: MyFunction1
Handler: lambda_module.lambda_handler
Runtime: python3.6
CodeUri: lambda.zip
MemorySize: 128
Timeout: 10
Policies:
-AWSLamdbaBasicExecutionRole
Events:
BingLambdaEndpoint:
Type: Api
Properties:
Path: MyFunction1/search
Method: get
Run Code Online (Sandbox Code Playgroud)
我在lambda中有环境变量但在启动时无法连接它们.文档说我可以创建一个environments.json文件并在invoke命令上附加以下内容:使用--env-vars invoke参数
我的环境文件看起来像示例,我收到一个错误:
Unable to find environment variable: api_key
environment.json看起来像这样:
{
"MyFunction1": {
"api_key": "123456789",
"BUCKET_NAME": "testBucket"
}
}
Run Code Online (Sandbox Code Playgroud)
命令我运行是这样的:
sam local invoke MyFunction1 --env-vars environment_variables.json -e event.json
Run Code Online (Sandbox Code Playgroud)
有人能提供额外的见解吗?
使用AWS SAM Local我可以在本地测试我的无服务器应用程序,这很棒.
我也可以部署到AWS,它显然采用相同的标志aws cloudformation deploy,因此我可以传递一个参数文件,例如应用程序机密(API密钥等).
但是,我aws local start-api --help在Github上的文档中或文档中找不到有关如何在本地测试时使用参数文件的内容.
如何在运行时指向要与模板一起使用的参数文件sam local start-api?
我正在使用SAM CLI v0.8.1。我正在尝试将环境变量MY_TABLE_VAR设置为资源(MyTableResource)中表的名称。但是,在本地运行我的应用程序时,未定义MY_TABLE_VAR。您能告诉我模板中有什么问题吗,如何正确设置呢?以下是我的SAM模板:
Globals:
Function:
Timeout: 30
Runtime: nodejs8.10
Environment:
Variables:
MY_TABLE_VAR: !Ref MyTableResource
Resources:
MyTableResource:
Type: AWS::Serverless::SimpleTable
Properties:
TableName: table1
PrimaryKey:
Name: id
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
Run Code Online (Sandbox Code Playgroud)