How can I connect to a database from a Fargate ECS task? AWS

Before you go any further, make sure you have an Amazon ECS cluster and an Amazon Relational Database Service (Amazon RDS) database running on Fargate.

Make a connection between your task and the database.

  • Create a Python script that connects to your MySQL database first.
  • After that, write a Dockerfile with the commands needed to build an image. As an illustration:
  • Make sure your script and Dockerfile are in the same directory.
  • Create an Amazon ECR repository and upload the Docker image to it.
  • Add the Docker image from step 2 as the container image to a task definition.
  • Set the settings for the ENDPOINT, USER, and PASS environment variables in your job definition.
  • These values can be passed as environment variables or retrieved using AWS Secrets Manager secrets.
  • Then, from the navigation pane of the Amazon ECS interface, select Task Definitions.
  • Choose your task definition, then Actions, and finally Run Task.7.For Launch type, choose FARGATE.
  • Choose a cluster for your task specification in Cluster.
  • Enter the number of tasks you want to be replicated in the Number of tasks field.
  • In the VPC and security groups section, select your Amazon Virtual Private Cloud Cluster VPC (Amazon VPC).
  • Choose your subnets in the Subnets section.
  • Choose at least one security group from the drop-down menu.
  • Finally, select Run Task.
  • The task is stopped by the script, which returns the following message:

“Essential container in task exited.”

 Make sure your task is linked to your database.

  • Open the Amazon ECS console first, Amazon Relational Database Service
  • Select Clusters from the navigation menu, and then your cluster.
  • Select the Tasks tab from the drop-down menu.
  • Choose Halted for Desired task status to show a list of tasks that have been stopped.
  • Select the task that has been halted.
  • In the Containers section of the Details page of your halted job, select the expander icon.
  • Finally, select View logs in CloudWatch from the drop-down menu.
  • In the AWS CloudWatch console, you should receive the following message:

At Velan, our server support engineers can help to connect the RDS database with ECS. If you are interested in our service, please fill the Quick connect form to get in touch with us.


Quick Connect With Us