spring-boot-starter-aws-parameter-store 프로젝트는 AWS SSM Parameter Store 를 액세스 하는 spring-boot 의 Auto Configuration 구성을 지원 합니다.
이 모듈은 Spring Framework 제공 하는 확장 기능 중 하나인 BeanPostProcessor 을 이용 하여,
@SsmParameterValue
어노테이션에 해당하는 속성 값을 Spring Bean 에 자동적으로 주입합니다.
/ssm/parameters/rds/symple/password
경로의 값을 바인딩 예시
@SsmParameterValue("/ssm/parameters/rds/symple/username")
private String username;
이 결과로, Bean 클래스의 username 속성은 /ssm/parameters/rds/symple/username 경로에 지정된 보안 문자열 (예: "admin")값이 바인딩 될 수 있습니다.
/ssm/parameters/rds/symple
경로에 포함된 모든 값들을 Map 으로 바인딩 예시
@SsmParameterValue(value = "/ssm/parameters/rds/symple", type = ValueType.MAP)
private Map<String, String> info;
이 결과로, Bean 클래스의 info 속성은 /ssm/parameters/rds/symple 경로에 포함된 모든 보안 속성 (예: {database=portal, password=encrypted_secured_value, username=symplesims} )이 Map 객체로 바인딩 될 수 있습니다.
AWS SSM Parameter Store 를 액세스 하려면 Spring Boot 애플리케이션이 제대로 동작 하도록 spring-boot-starter-aws-parameter-store 를 추가 하기만 하면 됩니다.
- Maven
<dependencies>
<dependency>
<groupId>io.symplesims.spring.aws</groupId>
<artifactId>spring-boot-starter-aws-parameter-store</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
- Gradle
dependencies {
implementation 'io.symplesims.spring.aws:spring-boot-starter-aws-parameter-store:1.0.0'
}
spring-boot 의 application-properties 설정 방식과 동일 하게 설정 합니다.
application.yaml
또는 application.properties
설정 파일에 아래와 같이 AWS Parameter Store 를 액세스 할 수 있도록 "spring.cloud.aws.ssm.provider-type" 속성을 설정 합니다.
Production 서비스 환경을 위해 "spring.cloud.aws.ssm.provider-type" 값을 "default" 으로 설정 합니다.
이렇게 하면, EC2, ECS, Lambda 와 같은 애플리케이션을 구현 했을 때 인증을 위해 내부적으로 AssumeRole 을 사용 하게 되고, DefaultCredentialsProvider 을 통해 자동적으로 인증 하게 됩니다. 이렇게 하면 소스 코드 에서 accessKey 가 노출 되지 않고 안전 하게 액세스 할 수 있습니다.
spring:
cloud:
aws:
ssm:
provider-type: default
로컬 테스트 환경을 위해선 아래와 같이 "provider-type" 과 "profile" 속성을 설정 하고 AWS SSM Parameter Store 에 저장된 경로의 보안 값을 액세스 할 수 있는지 확인 할 수 있습니다.
AWS Profile 에 관련된 설정은 AWS Configuration and credential file settings 가이드를 참고 합니다.
spring:
cloud:
aws:
ssm:
provider-type: profile
profile: <your_profile>
아래 SimpleComponent
와 같이 쉽게 사용할 수 있습니다.
import io.symplesims.spring.aws.ssm.autoconfigure.SsmParameterValue;
import io.symplesims.spring.aws.ssm.autoconfigure.ValueType;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class SimpleComponent {
@SsmParameterValue("/ssm/parameters/rds/symple/username")
private String username;
@SsmParameterValue(value = "/ssm/parameters/rds/symple", type = ValueType.MAP)
private Map<String, String> info;
public String getUsername() {
return username;
}
public Map<String, String> getInfo() {
return info;
}
}
AWS SSM 파라미터 값을 참조하는 기능은 spring-cloud 프로젝트의 spring-cloud-aws-starter-parameter-store 모듈에서 이미 훌륭하고 안정적으로 구현해 놓았습니다.
참고로, 해당 기능은 PropertySource 에 Parameter Value 값을 주입하고 @Value 어노테이션으로 액세스 합니다.