通过 ECS 在 EC2 上将 SSM Parameter Store 与 Containerized Spring Boot 运行 结合使用

Using SSM Parameter Store with a Containerised Spring Boot running on EC2 through ECS

我一直致力于容器化应用程序,目的是使用 ECS 管理应用程序到 EC2 的创建和部署。

我遵循的指南包括:

The Spring guide for containerising applications

Deploying Spring Boot on ECS

A guide on adding Parameter Store

Another guide on adding Parameter Store

这还没有完全解决,我相信我已经将其缩小到 Parameter Store 配置作为问题的根源。

目前,pom.xml 非常简单,但我发现根据情况需要更多配置:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
    <version>3.1.2</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>

运行 在本地,在容器之外,我可以用非常少的 bootstrap 配置访问参数:

spring.application.name=PxTest
#I would usually provide this...
#cloud.aws.credentials.profile-name=default
#...but it seems like Spring assumes this.

我将它容器化后,它就失败了。然后我补充说:

cloud.aws.stack.auto=false
cloud.aws.region.static=us-east-1
cloud.aws.region.auto=false

据我了解,这 应该 帮助 Spring 找到合适的参数 - 但我在描述通过 SSM 的文档或文章中看到的很少参数将属性存储到 ECS 提供的 EC2 上的 Spring 引导应用程序 运行。

我可以在 EC2 上访问的错误消息谈到 Docker 进入混杂状态,然后是阻塞状态,然后重复。鉴于它在没有 SSM 的情况下工作正常,我怀疑这是 Spring 应用程序启动、失败,然后被反复触发。

运行 在本地,我得到的错误是:

com.amazonaws.SdkClientException: Failed to connect to service endpoint:
(short version, at EC2ResourceFetcher readResource)
Caused by: java.net.ConnectException: Connection refused
Factory method 'ssmClient' threw Exception; nested exception is com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.

然后是关于无法实例化 bean 的连锁异常,因为它无法访问 SSM 服务。

如上所述,我在 bootstrap.properties 中提供了一个区域,但它似乎在本地忽略了这一点 - 但由于它在部署时将成为应用程序堆栈的一部分,所以我认为它不太可能与在 AWS 上看到的本地错误相同吗?

有没有人以前完成过这个,或者有任何资源可能对我需要将哪些信息传递到容器以允许它与 SSM 对话有用?

我建议使用 the built-in support ECS 将来自 SSM Parameter Store(或 Secrets Manager)的机密作为环境变量注入。这样你的代码就更加与环境无关,你可以 运行 通过设置环境变量而不是将它连接到外部依赖项(如 SSM Parameter Store)来本地