7

I have created a Spring boot application where I want to use AWS secrets for application.properties. I am using spring boot 2.2.6.RELEASE and as per the documentation I have added following dependencies in my 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>

From AWS Secrets Manager service I created a new secret of type "Other types of secrets" and gave it a name /secret/myservice. For testing I added a secret key as environment and value as aws which I want to retrieve in my controller. The part which is not clear to me is the entry I need to make in my bootstrap.yml file as I am confused with the instructions in Spring Cloud AWS documentation. Could someone please provide some proper instructions as I am not able to use this feature properly. For reference I added this in my bootstrap.yml file:

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

and trying to retrieve the environment value in the 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;
 }
}
Mohit224
  • 443
  • 1
  • 10
  • 24

1 Answers1

1

Struggled with the same problem. Solved this by defining environment variables in the lambda function itself and then populating those with AWS Secrets Manager.

This way you can use a placeholder like ${property_1} in the application.properties file and this will be replaced by the Environment variable defined in the Lambda Function.

Dharman
  • 30,962
  • 25
  • 85
  • 135
manuka_m
  • 331
  • 2
  • 12
  • ok thanks. Could you please also share how you described the properties name in yaml and how you used bootstrap.yam – sar Aug 02 '21 at 05:17
  • For this approach, you don't need the bootstrap.yaml. You just need to create the secrets in the Lambda Environment Variables section and just use a placeholder with that name in the properties file like ${property_1} – manuka_m Aug 03 '21 at 07:06