LoginSignup
3
2

More than 3 years have passed since last update.

spring-cloud-starter-aws-parameter-store-configを利用してAWS SSMパラメータストアからパラメータを取得する

Posted at

SpringCloudを利用してAWS SSMパラメータストアに設定したパラメータを取得する方法のメモ

目的

セキュリティの観点からアプリケーション内のプロパティファイルに認証情報を保持しないようにするため、起動時のSSMパラメータストアから必要なプロパティ値を取得できるようにする。

前提

EC2で起動するSpringBootアプリケーションがRDSへの接続情報をSSMパラメータストアから読み込む

ライブラリ設定

build.gradleのdependenciesに下記追加

implementation 'org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config'

プロパティファイル

bootstrap.properties
cloud.aws.stack.auto=false
cloud.aws.region.auto=false
cloud.aws.region.static=ap-northeast-1

# ParameterStore の設定
aws.paramstore.region=${cloud.aws.region.static}
aws.paramstore.default-context={アプリケーション名}
aws.paramstore.enabled=true
aws.paramstore.prefix=/config
aws.paramstore.name={アプリケーション名}
# アプリケーション名とプロファイルのセパレータ
aws.paramstore.profileSeparator=_

# Datasource
spring.datasource.driver-class-name=
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=

ローカル起動(開発時)の場合は無効とする設定を入れる

bootstrap-develop.properties
aws.paramstore.enabled=false

SSMパラメータストアの設定

下記のように各プロパティ値をSSMパラメータストアにて設定する

/config/{アプリケーション名}_{プロファイル名}/spring.datasource.driver-class-name
/config/{アプリケーション名}_{プロファイル名}/spring.datasource.url
/config/{アプリケーション名}_{プロファイル名}/spring.datasource.username
/config/{アプリケーション名}_{プロファイル名}/spring.datasource.password

EC2に対するポリシー設定

IAMロールを用意して、下記ポリシーを設定する。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "ssm:DescribeParameters",
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "ssm:GetParametersByPath",
                "ssm:GetParameters"
            ],
            "Resource": "arn:aws:ssm:ap-northeast-1:[アカウントID]:parameter/config/[アプリケーション]*"
        }
    ]
}
3
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
3
2