Hello Team, Need some help with running database m...
# typescript
a
Hello Team, Need some help with running database migrations using Pulumi. I am running a nodejs service using Pulumi on AWS Fargate. The server is connected to a Postresql RDS on AWS. What is the best way to run db migrations using Pulumi whenever a nodejs service deployment is done? Also what should I do if I want to run some database seeders as well? Any pointers on this is appreciated.
g
I'd probably run a separate container in the Task instead of using pulumi.
a
So I currently deploy the infra on Fargate with Pulumi. So If I create another task definition for db migration, how can I ensure that the container exits after the migration is applied? Do you have any guide that you can point me to?
a
Thanks I'll go through the resources 👍
I managed to get another container up in the same Fargate task definition to take care of migrations. Now the migrations are running, but the problem is that it does not stop. The same container is re-ran every time it exits and now the task definition is caught it a loop. This is also affecting the main service I am running and making the whole system a mess. Is there a way to define the migration container as a
one-off
execution so that it executes and exits and never really comes back up again.?
c
Fargate assumes that containers in a task definition are essential and therefore need to be restarted when they exit regardless of exit code. Set the
essential
property to
false
for your migration container to prevent it from being restarted. However, I think if your migration container exits on failure it won't stop the other container from starting up, assuming you want that sort of control. You might need to achieve that through some other means by detecting that the migration container exited with failure status code and handle that however that's appropriate to you.
a
Thanks @clever-sunset-76585 for the insight. I'll try it out. In general is it better to may be write a Lambda function to take care of this db migration or is running db migration within Fargate a good practice?
c
Running it using Lambda may complicate your architecture. It depends on your setup, though. You could spin up the Lambda inside your VPC such that it can access your DB, assuming your DB is only accessible within your VPC. But you can do that with a task definition too. Basically, run a task definition that only has your migrations container with
essential
set to false and detect if the container exited with a failure status code. You'd have do that detection no matter which service you choose -- Lambda or Fargate. As for which is better, it's hard to say. It depends on a few factors. Each has its benefits of being "better" than the other. Choose what makes sense to you and what you need right now.
a
yeah...that makes sense... thanks @clever-sunset-76585
c