本文介绍了aws-sam-local环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遵循自述文件: https://github.com/awslabs/aws-sam -本地

I am following the readme here: https://github.com/awslabs/aws-sam-local

我有一个用python 3.6编写的lambda,其类似于此处的helloworld示例: https://github.com/awslabs/aws-sam-local/tree/develop/samples/hello-world/python

I have a lambda written in python 3.6 and its similar to the helloworld example here : https://github.com/awslabs/aws-sam-local/tree/develop/samples/hello-world/python

template.yml看起来像这样:

template.yml looks as such:

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

我在lambda中有环境变量,但是在启动时无法连接它们.文档说我可以创建一个environment.json文件,并将以下内容附加到invoke命令上:使用invoke的--env-vars参数

I have environment variables within the lambda but not able to wire them up on start up. Documentation says I can create a environments.json file and append the following on the invoke command : Use --env-vars argument of invoke

我的环境文件类似于示例,但出现错误: Unable to find environment variable: api_key

My environment file looks like the example and I get an error: Unable to find environment variable: api_key

environment.json看起来像这样:

environment.json looks as such:

{
  "MyFunction1": {
    "api_key": "123456789",
    "BUCKET_NAME": "testBucket"
  }
}

我运行的

命令是这样的:

command I run is as such:

sam local invoke MyFunction1 --env-vars environment_variables.json -e event.json

任何人都可以提供其他见解吗?

Can anyone provide additional insight?

推荐答案

要以这种方式与SAM Local一起使用的任何环境变量都必须存在于SAM模板中.来自此GitHub问题:

Any environment variables you want to use with SAM Local in this manner need to exist in your SAM template. From this GitHub issue:

在模板中,您可以不提供任何值,一个空字符串或选择合理的默认值.

In the template, you can provide no value, an empty string, or choose a sensible default.

模板的一部分,包括环境变量:

A portion of a template including environment variables:

Resources:
  MyFunction1:
    Type: 'AWS::Serverless::Function'
    Properties:
      .....
      Environment:
        Variables:
          api_key:
          BUCKET_NAME:


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


You can think of the environment variables file as a mechanism to override environment variables that the template "knows" about. It does not work as a mechanism to inject arbitrary environment variables into the local runtime.

这篇关于aws-sam-local环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:47