SAM CLI,版本0.39.0如何使其工作?我该怎么办?解决方案您可以浏览本文,其中包含一些示例,这些示例使用AWS SAM创建不同的AWS资源.您所遇到的问题可能是参数名称DATABASE_URI包含非字母数字字符(在您的示例中为下划线).尝试将其重命名为DatabaseUri,并在sam invoke local命令中执行相同的操作.应该可以.此外,您需要在template.yaml中进行更改,而不是在packaged.yaml中进行更改.当您运行sam package命令时,packaged.yaml会自动生成.进行这些更改时,以下template.yaml对我有用.Parameters: DatabaseUri: # Changed this to remove underscore Description: 'Required. MongoDB connection URL' Type: 'String'Resources: BUDAuthorizeUserHandler: Type: AWS::Serverless::Function Properties: FunctionName: BUDAuthorizeUserHandler Handler: index.pingWithEnvVariable # Ignore this change, my test function is at this location Runtime: nodejs10.x Environment: Variables: MONGODB_URI: !Ref DatabaseUri # Removed underscore from here as well, obviously index.js(在输出中返回env变量的值以进行测试)exports.pingWithEnvVariable = async event => { const response = {}; response.statusCode = 200; const env = process.env.MONGODB_URI; response.body = JSON.stringify({ env }); return response;};使用了与您相同的env.json $ sam local invoke BUDAuthorizeUserHandler --env-vars env.jsonSTART RequestId: 6a9d398c-fecd-1b07-c9a0-d9fe4293cfe1 Version: $LATESTEND RequestId: 6a9d398c-fecd-1b07-c9a0-d9fe4293cfe1REPORT RequestId: 6a9d398c-fecd-1b07-c9a0-d9fe4293cfe1 Init Duration: 211.76 ms Duration: 5.66 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 43 MB{"statusCode":200,"body":"{\"env\":\"mongodb+srv://USER:PASSWORD@HOST/bud?retryWrites=true\u0026w=majority\"}"}还可以使用参数替代$ sam local invoke BUDAuthorizeUserHandler --parameter-overrides 'ParameterKey=DatabaseUri,ParameterValue=mongodb+srv://USER:PASSWORD@HOST/bud?retryWrites=true&w=majority'START RequestId: e0415251-2655-139b-e5df-5f9db658ca01 Version: $LATESTEND RequestId: e0415251-2655-139b-e5df-5f9db658ca01REPORT RequestId: e0415251-2655-139b-e5df-5f9db658ca01 Init Duration: 163.06 ms Duration: 6.88 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 43 MB{"statusCode":200,"body":"{\"env\":\"mongodb+srv://USER:PASSWORD@HOST/bud?retryWrites=true\u0026w=majority\"}"}I want to get rid off hardcoded passwords in my lambda that is deployed to AWS. I found I shall modify packaged.yaml:Parameters: DATABASE_URI: Description: 'Required. MongoDB connection URL' Type: 'String'Resources: BUDAuthorizeUserHandler: Type: AWS::Serverless::Function Properties: FunctionName: BUDAuthorizeUserHandler Handler: src/handlers/users/authorizeUser.handler Runtime: nodejs10.x Environment: Variables: MONGODB_URI: !Ref DATABASE_URIThis is the usage:const MONGODB_URI = process.env.MONGODB_URI;console.log(MONGODB_URI);So far so good and according to the specification. But I spent two hours trying to make it work locally.Configuration file env.json{ "BUDAuthorizeUserHandler": { "MONGODB_URI": "mongodb+srv://USER:PASSWORD@HOST/bud?retryWrites=true&w=majority" }}I tried these options but the environment variable was never defined:sam local start-api --env-vars env.jsonsam local start-api --parameter-overrides ParameterKey=DATABASE_URI,ParameterValue="mongodb+srv://USER:PASSWORD@HOST/bud?retryWrites=true&w=majority"I have walked through these pages:https://github.com/awslabs/aws-sam-cli/issues/1163aws-sam-local environment variablesHow do I specify template parameters when running AWS SAM Local?Setting environmental variables with !Ref in AWS SAM?SAM CLI, version 0.39.0How to make it work? What do I do wrong? 解决方案 You can go through this article which has some examples to use AWS SAM to create different AWS resources.The problem in your case might be that the Parameter name DATABASE_URI contains non-alphanumeric characters (underscore in your example). Try renaming it to DatabaseUri and doing the same in your sam invoke local command. It should work.Also, you need the changes in template.yaml, not packaged.yaml. packaged.yaml is auto-generated when you run the sam package command.On making these changes, following template.yaml works for me.Parameters: DatabaseUri: # Changed this to remove underscore Description: 'Required. MongoDB connection URL' Type: 'String'Resources: BUDAuthorizeUserHandler: Type: AWS::Serverless::Function Properties: FunctionName: BUDAuthorizeUserHandler Handler: index.pingWithEnvVariable # Ignore this change, my test function is at this location Runtime: nodejs10.x Environment: Variables: MONGODB_URI: !Ref DatabaseUri # Removed underscore from here as well, obviouslyindex.js (returning the value of env variable in the output for testing)exports.pingWithEnvVariable = async event => { const response = {}; response.statusCode = 200; const env = process.env.MONGODB_URI; response.body = JSON.stringify({ env }); return response;};used the same env.json as yours$ sam local invoke BUDAuthorizeUserHandler --env-vars env.jsonSTART RequestId: 6a9d398c-fecd-1b07-c9a0-d9fe4293cfe1 Version: $LATESTEND RequestId: 6a9d398c-fecd-1b07-c9a0-d9fe4293cfe1REPORT RequestId: 6a9d398c-fecd-1b07-c9a0-d9fe4293cfe1 Init Duration: 211.76 ms Duration: 5.66 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 43 MB{"statusCode":200,"body":"{\"env\":\"mongodb+srv://USER:PASSWORD@HOST/bud?retryWrites=true\u0026w=majority\"}"}Works with parameter overrides as well$ sam local invoke BUDAuthorizeUserHandler --parameter-overrides 'ParameterKey=DatabaseUri,ParameterValue=mongodb+srv://USER:PASSWORD@HOST/bud?retryWrites=true&w=majority'START RequestId: e0415251-2655-139b-e5df-5f9db658ca01 Version: $LATESTEND RequestId: e0415251-2655-139b-e5df-5f9db658ca01REPORT RequestId: e0415251-2655-139b-e5df-5f9db658ca01 Init Duration: 163.06 ms Duration: 6.88 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 43 MB{"statusCode":200,"body":"{\"env\":\"mongodb+srv://USER:PASSWORD@HOST/bud?retryWrites=true\u0026w=majority\"}"} 这篇关于AWS SAM本地和环境参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-06 09:47