本地调用sam模板时解决secretsmanager

如何解决本地调用sam模板时解决secretsmanager

我正在尝试使用 sam local invoke 在本地调用 lambda。该函数调用正常,但我的秘密的环境变量无法解析。当您部署函数时,秘密会按预期解析。但我想避免我的本地代码和我部署的代码有任何不同。那么有没有办法在本地调用时将这些秘密解析为实际的秘密值?目前我只从环境变量中获取字符串值。代码如下。

模板.yaml

    # This is the SAM template that represents the architecture of your serverless application
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-basics.html

# The AWstemplateFormatVersion identifies the capabilities of the template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/format-version-structure.html
AWstemplateFormatVersion: 2010-09-09
Description: >-
  onConnect

# Transform section specifies one or more macros that AWS CloudFormation uses to process your template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-section-structure.html
Transform:
- AWS::Serverless-2016-10-31

# Resources declares the AWS resources that you want to include in the stack
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
Resources:
  # Each Lambda function is defined by properties:
  # https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction

  # This is a Lambda function config associated with the source code: hello-from-lambda.js
  helloFromLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/handlers/onConnect.onConnect
      Runtime: nodejs14.x
      MemorySize: 128
      Timeout: 100
      Environment:
        Variables:
          WSS_ENDPOINT: '{{resolve:secretsmanager:prod/wss/api:SecretString:endpoint}}'

onConnect.js

/**
 * A Lambda function that returns a static string
 */
exports.onConnect = async () => {
    const endpoint = process.env.WSS_ENDPOINT;
    console.log(endpoint);
    // If you change this message,you will need to change hello-from-lambda.test.js
    const message = 'Hellddfdsfo from Lambda!';

    // All log statements are written to CloudWatch
    console.info(`${message}`);
    
    return message;
}

解决方法

我想出了一个变通办法,让我拥有一个代码库并在本地“解析”秘密/参数。

我创建了一个非常基本的 lambda 层,如果环境设置为 LOCAL,它的唯一工作就是获取机密。 导入 boto3

obs

我使用 lambda 中的一个参数设置了环境,该参数将调用该层。顺便说一句,这一层最终将解决多个秘密,这就是为什么嵌套的 if 可能看起来有点奇怪。这就是我设置环境的方式:

def get_secret(env,type,secret):
    client = boto3.client('ssm')
    if env == 'LOCAL':
        if type == 'parameter':
            return client.get_parameter(
                Name=secret,)['Parameter']['Value']
    else:
        return secret

现在我可以简单地在我的 lambda 中调用 get_secret 方法,并且取决于我将 Env 设置为秘密的内容,将在运行时获取或从环境变量中返回。

Resources:
  ...
  GetWSSToken:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: get_wss_token
      CodeUri: get_wss_token/
      Handler: app.lambda_handler
      Runtime: python3.7
      Timeout: 30
      Layers:
        - arn:aws:lambda:********:layer:SecretResolver:8
      Environment:
        Variables:
          ENVIRONMENT: !Ref Env
          JWT_SECRET: !FindInMap [ Map,!Ref Env,jwtsecret ]
     ...

Mappings:
  Map:
    LOCAL:
      jwtsecret: jwt_secret
    PROD:
      jwtsecret: '{{resolve:ssm:jwt_secret}}'
    STAGING:
      jwtsecret: '{{resolve:ssm:jwt_secret}}'

Parameters:
  ...
  Env:
    Type: String
    Description: Environment this lambda is being run in.
    Default: LOCAL
    AllowedValues:
      - LOCAL
      - PROD
      - STAGING

我希望这可以帮助那些试图解决这个问题的人。这里的主要问题是将秘密隐藏在代码库之外,并且能够使用即将投入生产的相同代码在本地进行测试。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?