aws-sam-local环境变量

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)

您可以将环境变量文件视为覆盖模板"知道" 的环境变量的机制.它不能作为将任意环境变量注入本地运行时的机制.

  • 我在寻找使用环境变量的参考时从谷歌找到了这个。根据 [关于 sam 本地环境变量的文档](https://github.com/awslabs/aws-sam-cli/blob/develop/docs/advanced_usage.md#lambda-environment-variables):`Environment` 键定义变量和默认值。可以使用 `--env-vars` cli 参数来定义一个 json 文件来覆盖这些值。我将使用此信息更新答案。 (2认同)

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

  • 谢谢!这太棒了...终于可以理智地处理 envvars 和 SAM 了。这样做的好处是,当您运行“sam deploy --guided”时,系统会提示您添加您指定的任何环境变量。正是我所需要的。 (3认同)

小智 12

确保变量在 template.yml 中声明。配置文件会覆盖变量,但当原始模板中不存在变量时,不会创建变量。


小智 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)