Building and Deploying a Fargate Container that runs Python and performs CloudWatch Logging
This guide walks you through building and deploying an AWS Fargate container that runs a Python script. The container will be tagged as the latest version, pushed to Amazon Elastic Container Registry (ECR), and configured to log messages to CloudWatch Logs. The process includes setting up the Dockerfile, creating the Python script, building and pushing the Docker image, and configuring AWS services to ensure your container runs smoothly on Fargate.
By the end of this tutorial, you will have a running Fargate service that logs information to CloudWatch Logs, allowing you to monitor the status and output of your containerized application in real time.
NOTE - In this guide, when you see aws_account_id replace it with your AWS account number and region with your AWS region.
Step-by-Step Guide Overview
Setting up the Dockerfile
Define a Dockerfile that uses Amazon Linux 2023 as a base image, installs Python, sets up the environment, and configures the CloudWatch logging agent.
Writing the Python Script for CloudWatch Logging
Create a Python script that logs a message every 5 seconds to CloudWatch, ensuring you can monitor the container’s health and status.
Building the Docker Image
Build the Docker image using the Dockerfile, tagging it as latest
to make it easy to track and manage.
Tagging the Docker Image
Tag the Docker image with the latest version and prepare it for pushing to ECR by using the appropriate repository URL.
Pushing the Image to Amazon ECR
Push the Docker image to Amazon Elastic Container Registry (ECR) so it can be pulled by Fargate when running the container.
Creating a CloudWatch Log Group
Set up a CloudWatch Log Group where logs from the Fargate container will be streamed for monitoring and troubleshooting.
Creating an ECS Cluster
Set up an ECS cluster in Amazon ECS to serve as the environment where the Fargate task will run.
Creating a Fargate Task Definition
Define the task configuration for Fargate, including resource requirements, the container image, and the CloudWatch logging configuration.
Running the Fargate Service
Deploy the Fargate task as a service within your ECS cluster to run the container continuously.
Monitoring Logs in CloudWatch
Use Amazon CloudWatch to monitor the logs generated by your Fargate container, ensuring that the Python script is running as expected.
Step 1: Setting up the Dockerfile
Dockerfile:
Step 2: Writing the Python Script for CloudWatch Logging
INFO
.Step 3: Build the Docker Image
my-fargate-app
.Step 4: Tag the Docker Image
After building the image, tag it with the appropriate ECR repository URL. This makes it easier to push the image to Amazon ECR.
docker tag my-fargate-app:latest <aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-fargate-app:latest
Step 5: Push the Docker Image to Amazon ECR
Create an ECR Repository:- Go to the Amazon ECR console and create a new repository (e.g.,
my-fargate-app
).
Authenticate Docker to ECR
- Authenticate Docker with the ECR registry using AWS CLI.
Push the Docker Image
Push the image to ECR
docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-fargate-app:latest
Step 6: Create a CloudWatch Log Group
Create a CloudWatch Log Group where the logs from your Fargate container will be stored. This step ensures that all log data generated by the container is centralized for easy monitoring.
- Go to the CloudWatch Console.
- Click Logs, then Create log group.
- Name the log group (e.g., FargateContainerLogs), then click Create.
Step 7: Create an ECS Cluster
- Go to the Amazon ECS Console.
- Click Create Cluster and choose Fargate.
- Name the cluster (e.g.,
MyFargateCluster
), then click Create.
Step 8: Create a Fargate Task Definition
A task definition defines how the container should run in ECS.
- In the ECS Console, click Task Definitions, then Create new Task Definition.
- Choose Fargate as the launch type.
- Name the task definition (e.g.,
MyFargateTask
).
In the Container Definitions section:
- Click Add container.
- Name the container (e.g.,
my-fargate-container
). - Enter the image URL:
<aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-fargate-app:latest
. - Set the memory and CPU requirements (e.g., 0.5 GB memory and 0.25 vCPU).
- For Log Configuration, select awslogs and enter:
- Log group:
FargateContainerLogs
. - Log stream prefix:
my-fargate-stream
.
Click Add, then Create to save the task definition.
Step 9: Run the Fargate Service
- In the ECS Console, go to your cluster (e.g., MyFargateCluster).
- Click Create in the Services tab.
- Choose Fargate as the launch type.
- Select the task definition you created earlier (e.g., MyFargateTask).
- Set the desired number of tasks (e.g., 1), configure networking settings, and click Create Service.
Step 10: Monitor Logs in CloudWatch
These logs are generated every 5 seconds by the Python script.
Summary
In this guide, you've:
- Built a Docker container that runs a Python script logging messages every 5 seconds.
- Tagged and pushed the image to Amazon ECR for deployment.
- Set up CloudWatch to capture the logs from the container.
- Deployed the container to ECS Fargate and monitored the logs through CloudWatch.
By following these steps, you'll have a fully functional Fargate service that continuously logs information to CloudWatch, giving you valuable insights into the health and operation of your application.
Comments
Post a Comment