How to integrate Spring Boot with AWS parameter store

292 Views Asked by At

Spring Boot does not work with AWS parameter store. In the AWS parameter store I have set the name /test/security/jdbc.passwd and the value is asdf1234 . On the /test page, I want to output asdf1234, the value of jdbc.passwd stored in AWS Parameter Store. I would really appreciate it so much if you could tell me how to solve it

error that occurs

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
[2m2023-06-02T17:14:41.197+09:00[0;39m [31mERROR[0;39m [35m8244[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.boot.SpringApplication              [0;39m [2m:[0;39m Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'infoController': Injection of autowired dependencies failed
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:488) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.9.jar:6.0.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.9.jar:6.0
application.yml

server:
     port: 8001

spring:
 datasource:
     driver-class-name: com.mysql.cj.jdbc.Driver
     url: jdbc:mysql://localhost:3306/world?serverTimeZone=UTC&CharacterEncoding=UTF-8
     username: username
     password: passwd
     
aws:
  paramstore:
    enabled: true
    prefix: /test
    name: security
    profile-separator: _
InfoController.java

package com.chase.test.pssb.info;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chase.test.pssb.info.model.City;
import com.chase.test.pssb.info.model.Project;


@RestController
public class InfoController {
    
    @Value("${jdbc.passwd}")
    private String passwd;
    
    @GetMapping("/info")
    public Object projectInfo() {
        Project project = new Project();
        project.projectName = "preword";
        project.author = "hello-bryan";
        project.createdDate = new Date();
        return project;
    }

    @GetMapping("/cityList")
    public Object cityList() {
        List<City> cityList = InfoService.getCityList();
        return cityList;
    }
    
    @GetMapping("/test")
    public String getJdbcPasswd() {
        return passwd;
    }
}
build.gradle
plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.0'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'groovy'
}

group = 'com.pssb'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

ext {
    set('awsSpringVersion', '3.0.0')
}
  
dependencyManagement {
    imports {
      mavenBom "io.awspring.cloud:spring-cloud-aws-dependencies:${awsSpringVersion}"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.2'
    implementation 'org.projectlombok:lombok'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.mysql:mysql-connector-j'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.apache.groovy:groovy:4.0.11'
    implementation 'com.mysql:mysql-connector-j:8.0.33'
    //database
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    //AWS
    implementation 'org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config:2.2.6.RELEASE'
}

tasks.named('test') {
    useJUnitPlatform()
}

An error occurred and the value of the AWS parameter store could not be loaded.

0

There are 0 best solutions below