带有 Springboot lambda 函数的 application.properties 的 AWS 秘密

Moh*_*224 7 java spring amazon-web-services spring-boot aws-secrets-manager

我已经创建了一个 Spring 启动应用程序,我想在其中将 AWS 机密用于 application.properties。我正在使用 spring boot 2.2.6.RELEASE 并且根据文档我在我的 pom 中添加了以下依赖项:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-context</artifactId>
        <version>2.2.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId>
        <version>2.2.2.RELEASE</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

在 AWS Secrets Manager 服务中,我创建了一个“其他类型的密钥”类型的新密钥,并为其命名为/secret/myservice。为了测试,我添加了一个密钥作为环境,并将值添加为aws,我想在我的控制器中检索它。我不清楚的部分是我需要在 bootstrap.yml 文件中创建的条目,因为我对Spring Cloud AWS 文档中的说明感到困惑有人可以提供一些正确的说明,因为我无法正确使用此功能。作为参考,我在 bootstrap.yml 文件中添加了这个:

aws:
    secretsmanager:
      name: myservice
      prefix: /secret
      enabled: true
      defaultContext: application
      failFast: true
cloud:
    aws:
      region:
        static: us-east-1
Run Code Online (Sandbox Code Playgroud)

并尝试在控制器中检索环境值:

@RestController
@EnableWebMvc
public class PingController {

 @Value(value = "${environment}")
 private String environment;

 @RequestMapping(path = "/ping", method = RequestMethod.GET)
 public Map<String, String> ping() {
    Map<String, String> pong = new HashMap<>();
    pong.put("pong", "Hello, World!" + "This is " + environment + " environment...");
    return pong;
 }
}
Run Code Online (Sandbox Code Playgroud)

man*_*a_m 1

遇到同样的问题。通过在 lambda 函数本身中定义环境变量,然后使用 AWS Secrets Manager 填充这些变量来解决此问题。

这样,您可以在 application.properties 文件中使用类似 ${property_1} 的占位符,该占位符将被 Lambda 函数中定义的环境变量替换。