首页 > 解决方案 > 具有 Springboot lambda 函数的 application.properties 的 AWS 秘密

问题描述

我创建了一个 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。为了测试,我添加了一个密钥作为环境和值作为我想在我的控制器中检索的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

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

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

标签: javaspringamazon-web-servicesspring-bootaws-secrets-manager

解决方案


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

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


推荐阅读