How do I specify template parameters when running AWS SAM Local?

♀尐吖头ヾ 提交于 2020-02-21 12:33:20

问题


Using AWS SAM Local I can test my serverless application locally, which is awesome.

I can also deploy to AWS, which apparently takes the same flags as aws cloudformation deploy, so I can pass a parameters file with e.g. application secrets (API keys and such).

However, I can't find anything in aws local start-api --help or in the docs on Github about how to use a parameter file when testing locally.

How do I point to a parameters file to use with my template when running sam local start-api?


回答1:


You can use the --parameter-overrides switch. The syntax is quite long winded, as below:

sam local start-api --parameter-overrides ParameterKey=Key1,ParameterValue=value1 ParameterKey=Key2,ParameterValue=value2

That is, you need to specify the key and value of each pair with comma separation.

And then each pair is separated with a space.


From sam local start-api --help:

  --parameter-overrides       Optional. A string that contains
                              CloudFormation parameter overrides encoded
                              as key=value pairs. Use the same format as
                              the AWS CLI, e.g. 'ParameterKey=KeyPairName,
                              ParameterValue=MyKey ParameterKey=InstanceTy
                              pe,ParameterValue=t1.micro'



回答2:


It seems you can also use the -n or --env-vars parameter to pass environment variables in a JSON file to your functions. See the docs: Test Your Serverless Applications Locally Using SAM CLI (Public Beta)

In short, your JSON file would look like (example copied from documentation):

{
  "MyFunction1": {
    "TABLE_NAME": "localtable",
    "BUCKET_NAME": "testBucket"
  },
  "MyFunction2": {
    "TABLE_NAME": "localtable",
    "STAGE": "dev"
  },
}

And then you can do:

 $ sam local start-api --env-vars env.json

This is specifically for environment variables for your lambda functions though, so it may not be entirely what you're after?




回答3:


You can use the --parameter-overrides in sam deploy just like in aws cloudformation deploy with a small change:

Before:

sam deploy --template-file packaged.yaml --stack-name example-lambda --capabilities CAPABILITY_IAM --parameter-overrides ParameterKey=SourceS3Bucket ParameterValue=test-data-111

After:

sam deploy --template-file packaged.yaml --stack-name example-lambda --capabilities CAPABILITY_IAM --parameter-overrides SourceS3Bucket=test-data-111

Noticeable change: ParameterKey, ParameterValue does not need to be explicitly specified in sam deploy. Helps me in local testing.

Hope it helps. :)



来源:https://stackoverflow.com/questions/49266568/how-do-i-specify-template-parameters-when-running-aws-sam-local

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!