簡體   English   中英

AWS Secrets for application.properties with Springboot lambda 函數

[英]AWS Secrets for application.properties with Springboot lambda functions

我創建了一個 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>

在 AWS Secrets Manager 服務中,我創建了一個類型為“其他類型的秘密”的新秘密,並為其命名為/secret/myservice 為了測試,我添加了一個密鑰作為環境和值作為我想在我的 controller 中檢索的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

並嘗試檢索 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 function 本身中定義環境變量,然后使用 AWS Secrets Manager 填充這些環境變量來解決此問題。

這樣,您可以在 application.properties 文件中使用像 ${property_1} 這樣的占位符,這將被 Lambda Function 中定義的環境變量替換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM