我已经创建了一个 Spring 启动应用程序,我想在其中将 AWS secret 用于 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 服务中,我创建了一个“其他类型的 key ”类型的新 key ,并为其命名为/secret/myservice。为了测试,我添加了一个 key 作为环境,并将值添加为 aws,我想在我的 Controller 中检索它。我不清楚的部分是我需要在 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
并尝试在 Controller 中检索环境值:
@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;
}
}
最佳答案
挣扎着同样的问题。通过在 lambda 函数本身中定义环境变量,然后使用 AWS Secrets Manager 填充这些变量来解决此问题。
通过这种方式,您可以在 application.properties 文件中使用像 ${property_1} 这样的占位符,这将被 Lambda 函数中定义的环境变量替换。
关于java - 带有 Spring Boot lambda 函数的 application.properties 的 AWS secret ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62433938/