2
2

More than 1 year has passed since last update.

spring bootのプロパティファイルの設定値をawsのparameter storeから取得する

Posted at

概要

spring bootのプロパティファイルの設定値の一部をawsのparameter storeに移管する

環境

・spring boot(2.4.9)
・Java 8

ライブラリの追加

使用するライブラリ
build.gradleの修正

dependenciesに下記を追加

implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:3.0.3'
implementation 'org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config:2.2.6.RELEASE'

プロパティファイルの設定

bootstrap.ymlにParameterStoreの設定を追加
bootstrap.yml
cloud:
  aws:
    stack:
      auto : false
    region:
      auto : false
      static : {リージョン(東京の場合はap-northeast-1)}
aws:
  paramstore:
    region : ${cloud.aws.region.static}
    prefix : {「/」始まりの文字列または空(デフォルトは「/config」)}
    default-context : {任意の文字列(デフォルトは「application」)}
    profileSeparator : {コンテキスト名とプロファイルの区切り文字(デフォルトは「_」)}
    enabled : {無効化する場合はfalse(デフォルトはtrue)}
bootstrap.ymlにParameterStoreから取得したい項目を追加
bootstrap.yml
sapmle:
  param:

値はParameterStoreから取得するため、設定しなくても良い
設定した場合は、ParameterStoreから値が取得できない場合に使用される

ParameterStoreにパラメータを作成

AWS Systems Manager > パラメータストア からパラメータを作成
パラメータ名はbootstrap.ymlに記載した内容を基に以下のルールで指定
[prefix]/[default-context]_[アプリケーションのprofile(共通設定の場合は省略可)]/[key]

例)
profileに依存しない共通設定の場合
/config/application/sapmle.param

profileが「prod」ときの設定の場合
/config/application_prod/sapmle.param

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2