got*_*012 18 aws-lambda aws-sam-cli serverless
我在这里阅读自述文件: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)
有人能提供额外的见解吗?
Mik*_*ick 33
您希望以这种方式与SAM Local一起使用的任何环境变量都需要存在于SAM模板中.从这个GitHub问题:
... SAM Local仅解析SAM模板中定义的环境变量.
在模板中,您不能提供任何值,空字符串或选择合理的默认值.
包含环境变量的模板的一部分:
Resources:
MyFunction1:
Type: 'AWS::Serverless::Function'
Properties:
.....
Environment:
Variables:
api_key:
BUCKET_NAME:
Run Code Online (Sandbox Code Playgroud)
您可以将环境变量文件视为覆盖模板"知道" 的环境变量的机制.它不能作为将任意环境变量注入本地运行时的机制.
JLa*_*rky 25
模板文件
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Timeout: 3
Parameters:
SomeVar:
Type: String
Description: My SomeVar
Default: default value
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Environment:
Variables:
SOME_VAR: !Ref SomeVar
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
Outputs:
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
Run Code Online (Sandbox Code Playgroud)
来自https://github.com/awslabs/aws-sam-cli/issues/1163#issuecomment-557874976
然后在代码中
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Timeout: 3
Parameters:
SomeVar:
Type: String
Description: My SomeVar
Default: default value
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Environment:
Variables:
SOME_VAR: !Ref SomeVar
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
Outputs:
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
Run Code Online (Sandbox Code Playgroud)
当你运行时sam local start-api它会打印default value
当你运行时sam local start-api --parameter-overrides SomeVar=other_value它会打印other_value
那么如果你env.json用这个内容创建文件
{ "PreviewsFunction": { "SOME_VAR": "123" } }
Run Code Online (Sandbox Code Playgroud)
当你运行时sam local start-api --env-vars env.json它会打印123
ps 它将start-api/start-lambda/invoke以相同的方式与所有人一起使用,但它看起来sam deploy只适用于--parameter-overrides SomeVar=other_value而不适用--env-vars
小智 6
我遇到过同样的问题。当我跑的时候
sam local start-api --env-vars env.json
Run Code Online (Sandbox Code Playgroud)
env.json 中的值未被读取。对我来说解决这个问题的是在 env.json 中使用以下格式
"Parameters": {
"PARAM_NAME": "VALUE"
}
Run Code Online (Sandbox Code Playgroud)
另一种格式对我不起作用:
{ "function": { "PARAM_NAME": "VALUE" } }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14642 次 |
| 最近记录: |