I’m new guy in AWS Amazone ecosystem. I want to deploy 2 services. First one is Spring-Boot
application and second one will contain Mongodb
. Spring microservice contains Mongock
, which is up when application is started and it performs migrations to database. I want to do a simple deploy, using ECS. What I did first:
- created fargate cluster in order to create and start services in ecs there.
- built mongo image, pushed to ECR repo, built Task definition, depoyed Service with minimum 1 task. Ok, database was setup.
- then I copied mongo task public ip, set it as host to spring application properties, built spring image, pushed to ECR, started Service and task.
On this moment all worked fine – mongock connected to mongo service and applied migrations. As we know every task has dynamic ip, and I dont plan to have running tasks non-stop. After googling I found solution – Service Connect and decided to give DNS-names, in order task agent could find where to push requests.
For mongodb service I pushed next config using aws cli
:
{
"enabled": true,
"namespace": "test",
"services": [
{
"portName": "27017mongo",
"discoveryName": "user-mongo",
"clientAliases": [
{
"port": 27017
}
]
}
]
}
After that I checked service-described:
serviceConnectConfiguration": {
"enabled": true,
"namespace": "arn:aws:servicediscovery:eu-north-1:891377330754:namespace/ns-p76w3mzvv5py466a",
"services": [
{
"portName": "27017mongo",
"discoveryName": "user-mongo",
"clientAliases": [
{
"port": 27017,
"dnsName": "user-mongo.test"
}
]
}
]
},
"serviceConnectResources": [
{
"discoveryName": "user-mongo",
"discoveryArn": "arn:aws:servicediscovery:eu-north-1:891377330754:service/srv-rqiink64a7izgkdy"
}
]
},
From what I see, DNS name was registered normally. In service appeared new task container ecs-service-connect
in pair with user-mongo container.
I went next. Set hostname to spring props:
data:
mongodb:
uri: mongodb://user-mongo.test:27017/iAutoService02
auto-index-creation: true
database: iAutoService02
port: 27017
host: user-mongo.test
After that I deployed service and uploaded connect config:
{
"enabled": true,
"namespace": "test",
"services": [
{
"portName": "8083user",
"discoveryName": "user-service",
"clientAliases": [
{
"port": 8083
}
]
}
]
}
So both services are in ‘test’ cluster namespace. Spring
service was updated, ecs-service-connect
appeared too. And I considered things done, but suddenly Spring task fell down. I checked the logs:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'getBuilder' defined in io.mongock.runner.springboot.config.MongockContext: Unsatisfied dependency expressed through method 'getBuilder' parameter 0: Error creating bean with name 'connectionDriver' defined in class path resource [io/mongock/driver/mongodb/springdata/v4/config/SpringDataMongoV4Context.class]: Failed to instantiate [io.mongock.driver.api.driver.ConnectionDriver]: Factory method 'connectionDriver' threw exception with message: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=user-mongo.test:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: user-mongo.test}, caused by {java.net.UnknownHostException: user-mongo.test}}]
From what I see – Service cannot connect to database, because can not resolve
user-mongo.test
host. Unfortunately, I didnt find any solution. What I’m doing wrong? how to make my aws task to see each other?