Spring Bootã®ã¢ããªã±ã¼ã·ã§ã³èµ·åæã«ãAWS Systems Manager ãã©ã¡ã¼ã¿ã¹ãã¢ãããã©ã¡ã¼ã¿ãåå¾ã§ããã¿ãããªã®ã§è©¦ãã¦ã¿ã¾ããã
AWS Systems Manager ãã©ã¡ã¼ã¿ã¹ãã¢ã¨ã¯
è¨å®ãã¼ã¿ç®¡çã¨æ©å¯ç®¡çã®ããã®å®å ¨ãªé層åã¹ãã¬ã¼ã¸ãæä¾ãã¦ããã¦ãã¦ããã¹ã¯ã¼ãããã¼ã¿ãã¼ã¹æååãAMI IDãã©ã¤ã»ã³ã¹ã³ã¼ããªã©ã®ãã¼ã¿ããã©ã¡ã¼ã¿å¤ã¨ãã¦ä¿åã§ãã¾ããå¤ã¯ãã¬ã¼ã³ããã¹ãã¾ãã¯æå·åããããã¼ã¿ã¨ãã¦ä¿åã§ãã¾ãã詳ãã説æã¯ãå ¬å¼ããã¥ã¡ã³ããè¦ã¦ãã ããã
ãµã³ãã«
ç°å¢
Spring Bootçã®ãã¼ã¸ã§ã³ã¯æ¬¡ã®éãã§ãã
Java 11 Gradle 6.6.1 Spring Boot 2.3.4.RELEASE Spring Cloud Hoxton.SR8
ãã©ã¡ã¼ã¿ã®ç»é²
ç»é²ãããã©ã¡ã¼ã¿ã®key
ããã©ã«ãã ã¨æ¬¡ã®ãããª3é層ã§è¨å®ããããã«ãªã£ã¦ãã¾ãã[prefix]/[name]_[ã¢ããªã±ã¼ã·ã§ã³ã®profile(defaultã®å ´åã¯çç¥å¯)]/[key]
è¨å®å¤ã®æç²ã§ããï¼å¤ã¯ããã©ã«ãå¤ï¼
- ãã©ã¡ã¼ã¿ã¹ãã¢ããåå¾ãããå ¨ããããã£ã§å ±æããããã¬ãã£ã¯ã¹ã第ï¼é層ã®å¤ã - aws.paramstore.prefix=/config - å ¨ãµã¼ãã¹ã§å ±æãããã³ã³ããã¹ãåã第ï¼é層ã®å¤ã - aws.paramstore.default-context=application - ã³ã³ããã¹ãåã¨ãããã¡ã¤ã«ã®åºåãæå. - aws.paramstore.profile-separator=_ - ãã©ã¡ã¼ã¿ã¹ãã¢ãå©ç¨ãããã©ãã - aws.paramstore.enabled=true
åå¾ãããã©ã¡ã¼ã¿ãçµã¿ç«ã¦ã¦ãããã¸ãã¯ã¯ AwsParamStorePropertySourceLocator#PropertySource<?> locate(Environment) ã«ããã¾ãã
ãã©ã¡ã¼ã¿ã¹ãã¢ã¸ã®ç»é²
ãã©ã¡ã¼ã¿ã¹ãã¢ã«ãã©ã¡ã¼ã¿ãç»é²ãã¾ããä»åã¯ãDBã¸ã®ã¢ã¯ã»ã¹æ å ±ãç»é²ãã¾ãã
/config/sample/spring.datasource.url=jdbc:mysql://localhost:33306/sample /config/sample/spring.datasource.username=docker /config/sample/spring.datasource.password=docker
ä¾åé¢ä¿
ãã©ã¡ã¼ã¿ã¹ãã¢ãå©ç¨ããããã«è¿½å ããä¾åé¢ä¿ã¯ããã ãã§ãã
build.gradle
dependencies { ... implementation 'org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config:2.2.4.RELEASE' ... }
è¨å®ãã¡ã¤ã«
appliaction.yml(application.properties)ã«ã¯ä½ãæ¸ããªãã¦OKã§ãããbootstrap.ymlã«ããã¤ãè¨å®ãæ¸ãå¿ è¦ãããã¾ãã
bootstrap.ymlã®å ¨ä½
spring: datasource: url: username: password: cloud: aws: stack: auto: false region: auto: false static: ap-northeast-1 aws: paramstore: region: ${cloud.aws.region.static} default-context: sample logging: level: com: amazonaws: util: EC2MetadataUtils: error
ãã©ã¡ã¼ã¿ã¹ãã¢ããåå¾ããã®ã§keyã®ã¿æ¸ãã¾ãã
spring: datasource: url: username: password:
ãã¼ã«ã«ç°å¢ã§Spring Cloud AWSã使ãããã®ãã¾ããªãã§ããã¢ããªã±ã¼ã·ã§ã³èµ·åæã«ã¡ã¿ãã¼ã¿ãããªã¼ã¸ã§ã³çãåå¾ããã®ã§ããããã¼ã«ã«ç°å¢ã ã¨åå¾ã«å¤±æãã¦ã¢ããªã±ã¼ã·ã§ã³ãèµ·åããªããªã£ã¦ãã¾ãããã«ç¡å¹ã«ãã¦ãã¾ãã詳ããã¯ãConfiguring region 㨠CloudFormation configuration in Spring Boot ã«ããã¾ãã
cloud: aws: stack: auto: false region: auto: false static: ap-northeast-1
spring-cloud-starter-aws-parameter-store-configã®è¨å®ã§ãã
aws: paramstore: region: ${cloud.aws.region.static} default-context: sample
EC2MetadataUtilsãAWSä¸ã§åãã¦ãããåå¾ãã¾ãããAWSç°å¢ä»¥å¤ã§ã®ã¢ããªã±ã¼ã·ã§ã³èµ·åæã«ã¨ã©ã¼ãã°ãåºãã¦ãã¾ã£ã¦æ°æã¡æªãã®ã§è¨å®ãã¾ããï¼ãªãã¦ãåä½ã«ã¯ä½ãåé¡ãªãã§ãï¼
logging: level: com: amazonaws: util: EC2MetadataUtils: error
ã¢ããªã±ã¼ã·ã§ã³ã®èµ·å
ãã¨ã¯ã¢ããªã±ã¼ã·ã§ã³ãèµ·åããã ãã§ãã
ã©ã®ãã©ã¡ã¼ã¿ããã©ã¡ã¼ã¿ã¹ãã¢ããåå¾ãã¦ãããã確èªãããå ´åã¯ã次ã®è¨å®ãbootstrap.ymlã«æ¸ãã¦ãã ããã
logging: level: org: springframework: cloud: aws: paramstore: AwsParamStorePropertySource: debug
è¨å®ããã¨ãã°ã«ãããªæãã§åºåããã¾ãã
2020-10-11 18:35:27.847 DEBUG 55136 --- [ main] o.s.c.a.p.AwsParamStorePropertySource : Populating property retrieved from AWS Parameter Store: spring.datasource.password 2020-10-11 18:35:27.849 DEBUG 55136 --- [ main] o.s.c.a.p.AwsParamStorePropertySource : Populating property retrieved from AWS Parameter Store: spring.datasource.url 2020-10-11 18:35:27.849 DEBUG 55136 --- [ main] o.s.c.a.p.AwsParamStorePropertySource : Populating property retrieved from AWS Parameter Store: spring.datasource.username
ãããã¡ã¤ã«ã§è¨å®å¤ãåãã
ä¸è¬çã«ãdefaultãããã¡ã¤ã«ããåå¨ããªããã¨ã¯ãªããã¨æãã¾ããtestãããã¡ã¤ã«ã§ã¯ããã¹ãç°å¢ç¨ã®DBã¢ã¯ã»ã¹æ å ±ãåå¾ããã¿ãããªãã¨ãå¯è½ã§ãã
ãã©ã¡ã¼ã¿ã¹ãã¢ã¸ã®ç»é²
ãã©ã¡ã¼ã¿ã¹ãã¢ã¸ã®ç»é²ã®ä»æ¹ã«ã¤ãã¦ã¯ä¸ã§èª¬æããã®ã§çç¥ãã¾ãã ããããç»é²ãã¾ãã
/config/sample_test/spring.datasource.url=jdbc:mysql://localhost:33307/sample /config/sample_test/spring.datasource.username=test /config/sample_test/spring.datasource.password=test
ã¢ããªã±ã¼ã·ã§ã³ã®èµ·å
ã¢ã¯ãã£ããããã¡ã¤ã«ãtestã«ãã¦ãèµ·åããã ãã§ã /config/sample_test/*
ã®ãã©ã¡ã¼ã¿ãåå¾ãã¦ããã¾ãã
ããã©ã«ããããã¡ã¤ã«ã®/config/sample/
ãèªã¿è¾¼ãã§ãã®ãã¨ã«/config/sample_test
ã§ä¸æ¸ãããããã§ãã
ã¾ã¨ã
æã£ã¦ããããã¯ç°¡åã«ã§ãã¾ããï¼AWSã®ãããã¡ã¤ã«ã¯default
ã使ãããã®ã§å¤æ´ãããå ´åã¯èªåã§è¨å®æ¸ããªãã¨ãããªãã¿ããã§ãã
ã¡ãªã¿ã«è¨å®å¤ã®åªå é ä½ã¯ ãã©ã¡ã¼ã¿ã¹ã㢠> bootstram.yml > application.yml ã§ãããbootstrap.ymlã¨ãapplication.ymlã«åãè¨å®æ¸ãã¦ãã£ã¦ããã©ã¡ã¼ã¿ã¹ãã¢ã«è¨å®ãã¦ããå ´åã¯ããã©ã¡ã¼ã¿ã¹ãã¢ã®å¤ãåªå ããã¾ãã