Amazon ECS
Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service that helps you easily deploy, manage, and scale containerized applications.
With the Infrastructure Agent (Infra Agent), you can effectively monitor your ECS containers and tasks running on ECS EC2 instances and ECS Fargate in your cloud, on-prem, and hybrid architectures.
Database Integrations and the Amazon ECS Anywhere approach are not currently supported.
Fargate Setup
Use the Middleware and AWS Fargate integration to monitor your applications without having to manage servers.
Create a Sidecar
For Each ECS Task that you want to monitor, add the following sidecar container in your task definition.
{ "name": "mw-agent", "image": "ghcr.io/middleware-labs/mw-host-agent:master", "cpu": 256, "portMappings": [ { "name": "8006-tcp", "containerPort": 8006, "hostPort": 8006, "protocol": "tcp",
Collect Metrics
Adding the sidecar container mentioned above will automatically collect metrics data from your ECS tasks and containers. The Infra Agent uses the AWS ECS Task Metadata Endpoint to fetch metrics data.
Collect Logs
Forward your ECS Task logs to Middleware. The mw-agent
sidecar, mentioned above, must be running before proceeeding.
Step 1: Add a Fluent sidecar in your ECS task
JSON
{ "name": "log_router", "image": "amazon/aws-for-fluent-bit:stable", "cpu": 0, "portMappings": [], "essential": true, "environment": [], "mountPoints": [], "volumesFrom": [], "user": "0",
Step 2: Add a log Configuration in your main container
The Infra Agent sidecar will fetch these logs and send them to Middleware.
JSON
"logConfiguration": { "logDriver": "awsfirelens", "options": { "Host": "127.0.0.1", "Name": "forward", "Port": "8006" } }
Parsing JSON Structured Logs [Optional]
Firelens by default sends a JSON blob which Middleware displays as the log body. Parsing the JSON logs allows the Message
of the log being displayed while the attributes will be viewable on expansion.
To change the behavior you will need to add the below options to the firelensConfiguration
created in Step 1.
The JSON structured logs generated need to have a Message
field. Otherwise Middleware will not know what should be shown as a body of the log.
JSON
"firelensConfiguration": { "type": "fluentbit", "options": { "config-file-type": "file", "config-file-value": "/fluent-bit/configs/parse-json.conf", "enable-ecs-log-metadata": "true" } }
EC2 Setup
Step 1: Create & Configure a Infra Agent ECS Task Definition
Begin by creating a Task Definition for the Infra Agent container.
JSON
{ "family": "mw-agent-task", "containerDefinitions": [ { "name": "mw-agent", "image": "ghcr.io/middleware-labs/mw-host-agent:master", "cpu": 100, "memory": 512, "portMappings": [ {
Step 2: Register Your Task Definition File
You can register your Task Definition File using the the AWS CLI or the Amazon Web Console.
Execute the following command to register your Task Definition File in AWS. Learn more about the Amazon ECS CLI here.
aws ecs register-task-definition --cli-input-json file://<mw-ecs-agent.json>
Complete the following steps in the AWS Management Console to register your Task Definition File.
- Log in to your AWS Management Console and navigate to the Elastic Container Service (ECS) section
- In the left-hand menu, click on Task Definitions
- Create a new Task Definition under the JSON tab
- Copy and paste the configuration from your Task Definition file
- Save your Task Definition JSON file
- Click Create to register the Task Definition in AWS
Step 3: Schedule the Infra Agent as a Daemon Service
Set up the Infra Agent Task Definition as a Daemon Service to ensure only one Infra Agent container is running on each EC2 instance of the ECS cluster
Log in to your AWS Management Console and navigate to the Elastic Container Service (ECS) section
Choose the ECS cluster you will run the Infra Agent on
Create a new service within the selected cluster
Setup the Environment section
a. Select Launch type under compute options
b. Select EC2 as the Launch type
Setup the Deployment Configureation section
a. Select Service as the Application type
b. Specify the Task Definition you created earlier under the Family section
c. Provide a unique Service Name
d. Choose DAEMON as the Service Type
Proceed to create the service
Container Trace and Log Collection
Begin collecting container trace and log data in your EC2 instance. The Infra Agent will collect ECS container logs emitted to the stdout
and stderr
log stream and receive traces from your application and send to your Middleware account.
The Infra Agent must be running as an app before setting up collection. If not, attempted trace collection will throw an error while connecting to the agent and logs will not be sent at all.
Modify your Application Task Definition
You can setup container trace and log monitoring by modifying your Task Definition JSON file or following the below steps in the AWS Management Console.
- To collect traces, configure the
environment
variable in the Task Definition - To collect logs, configure the
logConfiguration
variable to enable thefluentd
logging driver
You do not need to collect container traces and log data at the same time.
JSON
"containerDefinitions": [ { (...) "environment": [ { "name": "MW_AGENT_SERVICE", "value": "172.17.0.1" } ], "logConfiguration": {
Navigate to the Amazon Elastic Container Service section in the AWS Management Console
Click on Task Definition and select your desired Task Definition
Create a new revision
Skip steps 4 or 5 if you do not want to enable container traces or logs
Enable container traces in your application
a. Under the
Environment Variables
section, selectAdd environment variable
b. Add
MW_AGENT_SERVICE
environment variable and set the value to172.17.0.1
Enable container logging in your application
a. Navigate to Logging
b. Check
Use logging collection
c. Add Parameters as seen below
Click Create to update your Task Definition
Update the Services/Daemon definition to use the latest version of the Task Definition
Visualize Your Data
Quickly access your Amazon ECS data with Middleware’s default ECS dashboard. Navigate to the Dashboard Builder and select the AWS ECS Dashboard.
Your AWS ECS data comes from the awsecscontainermetricsreceiver whereas your container data comes from the dockerstatsreceiver.
Troubleshooting
Missing Integrations Menu
If you do not see the Integrations Menu in Middlware, that means your account has not been granted Installation
permissions. Contact your system administrator to add the Installation
permission to your user role in Settings.
Need assistance or want to learn more about Middleware? Contact our support team in Slack.