【问题标题】:AWS Secrets for application.properties with Springboot lambda functions具有 Springboot lambda 函数的 application.properties 的 AWS 秘密
【发布时间】:2021-07-30 22:20:26
【问题描述】:

我创建了一个 Spring Boot 应用程序,我想在其中将 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>

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

aws:
    secretsmanager:
      name: myservice
      prefix: /secret
      enabled: true
      defaultContext: application
      failFast: true
cloud:
    aws:
      region:
        static: us-east-1

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

@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;
 }
}

【问题讨论】:

  • 你能解决这个问题吗?
  • 你解决了吗?
  • @sar 检查答案

标签: java spring amazon-web-services spring-boot aws-secrets-manager


【解决方案1】:

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

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

【讨论】:

  • 好的,谢谢。您能否也分享一下您如何在 yaml 中描述属性名称以及您如何使用 bootstrap.yam
  • 对于这种方法,您不需要 bootstrap.yaml。您只需要在 Lambda 环境变量部分创建密钥,然后在属性文件中使用具有该名称的占位符,例如 ${property_1}
猜你喜欢
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
  • 1970-01-01
相关资源
最近更新 更多