I want to connect to a database from an Amazon Elastic Container Service (Amazon ECS) task on AWS Fargate.
Resolution
Prerequisites:
Note: The following example uses MySQL as the engine type. For more information on engine types, see Getting started with Amazon RDS.
Connect your task to your database
Complete the following steps:
- Create a Python script that connects to your MySQL database. The following example rds.py script sends the results of the connection to the database to Amazon CloudWatch:
import pymysqlimport os
Database_endpoint = os.environ['ENDPOINT']
Username = os.environ['USER']
Password = os.environ['PASS']
try:
print("Connecting to " + Database_endpoint)
db = pymysql.connect(host = Database_endpoint, user = Username, password = Password)
print("Connection successful to " + Database_endpoint)
db.close()
except Exception as e:
print("Connection unsuccessful due to " + str(e))
Note: Replace ENDPOINT, USER, and PASS with your database values.
- To assemble an image, create a Dockerfile that includes the required commands. For example:
FROM pythonRUN pip install pymysql cryptography
COPY rds.py /
CMD [ "python", "/rds.py" ]
Important: Put your rds.py script and Dockerfile in the same folder.
- Create an Amazon ECR repository, and then push the Docker image to that repository.
- Create a task definition, and then add the Docker image as the container image. For example:
{
"executionRoleArn": "arn:aws:iam::account_ID:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"name": "sample-app",
"image": "YOUR-ECR-Repository-URL",
"essential": true
}
],
"requiresCompatibilities": [
"FARGATE"
],
"networkMode": "awsvpc",
"cpu": "256",
"memory": "512",
"family": "sample-app"
}
Note: In your task definition, set the values for the ENDPOINT, USER, and PASS environment variables. You can directly pass these values as environment variables or retrieve the values from secrets in AWS Secrets Manager. For more information, see How can I pass secrets or sensitive information securely to containers in an Amazon ECS task?
- Open the Amazon ECS console.
- In the navigation pane, choose Task Definitions.
- Select your task definition, choose Actions, and then choose Run Task.
- For Launch type, choose FARGATE.
- For Cluster, choose the cluster for your task definition.
- For Number of tasks, enter the number of tasks that you want copied.
- In the VPC and security groups section, for Cluster VPC, choose your Amazon Virtual Private Cloud (Amazon VPC).
- For Subnets, choose your subnets.
- For Security groups, select at least one security group.
- Choose Run Task.
The rds.py script stops the task and returns the following message:
"Essential container in task exited."
Confirm that your task is connected to your database
Complete the following steps:
- Open the Amazon ECS console.
- In the navigation pane, choose Clusters, and then choose your cluster.
- Choose the Tasks tab.
- For Desired task status, to see a list of stopped tasks, choose Stopped.
- Choose your stopped task.
- On the Details tab of your stopped task, in the Containers section, expand the section.
- Choose View logs in CloudWatch.
You see the following message in the CloudWatch console:
"Connection successful to [Your Endpoint]"