AWS SAM本地和环境参数 [英] AWS SAM local and environment parameters

查看:295
本文介绍了AWS SAM本地和环境参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想摆脱部署到AWS的lambda中的硬编码密码.我发现我将修改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_URI

这是用法:

const MONGODB_URI = process.env.MONGODB_URI;
console.log(MONGODB_URI);

到目前为止,一切都很好,并且符合规范.但是我花了两个小时试图使其在本地运行.

配置文件env.json

{
  "BUDAuthorizeUserHandler": {
     "MONGODB_URI": "mongodb+srv://USER:PASSWORD@HOST/bud?retryWrites=true&w=majority"
   }
}

我尝试了这些选项,但从未定义环境变量:

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

sam local start-api --parameter-overrides ParameterKey=DATABASE_URI,ParameterValue="mongodb+srv://USER:PASSWORD@HOST/bud?retryWrites=true&w=majority"

我已经浏览了以下页面:

https://github.com/awslabs/aws-sam-cli/issues/1163 aws-sam-local环境变量 在运行AWS SAM时如何指定模板参数是本地的吗? 在AWS SAM中使用!Ref设置环境变量吗?

SAM CLI,版本0.39.0

如何使其工作?我该怎么办?

解决方案

您可以浏览

还可以使用参数替代

$ 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: $LATEST
END RequestId: e0415251-2655-139b-e5df-5f9db658ca01
REPORT 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_URI

This 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.json

sam 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/1163 aws-sam-local environment variables How do I specify template parameters when running AWS SAM Local? Setting environmental variables with !Ref in AWS SAM?

SAM CLI, version 0.39.0

How 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, obviously

index.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.json
START RequestId: 6a9d398c-fecd-1b07-c9a0-d9fe4293cfe1 Version: $LATEST
END RequestId: 6a9d398c-fecd-1b07-c9a0-d9fe4293cfe1
REPORT 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: $LATEST
END RequestId: e0415251-2655-139b-e5df-5f9db658ca01
REPORT 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本地和环境参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆