Skip to content

Instantly share code, notes, and snippets.

@devgrok
Created July 2, 2018 11:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devgrok/9f85ef22030bb690feedd721579da77c to your computer and use it in GitHub Desktop.
Save devgrok/9f85ef22030bb690feedd721579da77c to your computer and use it in GitHub Desktop.
Installs CloudWatch Agent for monitoring ECS-Agent log files
Content-Type: multipart/mixed; boundary="==BOUNDARY=="
MIME-Version: 1.0
--==BOUNDARY==
MIME-Version: 1.0
Content-Type: text/cloud-boothook; charset="us-ascii"
#!/bin/bash +x
# optionally set ECS agent options (increase base container size)
cloud-init-per once docker_options echo 'OPTIONS="${OPTIONS} --storage-opt dm.basesize=100G"' >> /etc/sysconfig/docker
--==BOUNDARY==
MIME-Version: 1.0
Content-Type: text/x-shellscript; charset="us-ascii"
#!/bin/bash
yum install -y unzip jq
mkdir -p /tmp/cw-agent-install
cd /tmp/cw-agent-install
curl -O https://s3.amazonaws.com/amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip
unzip AmazonCloudWatchAgent.zip
./install.sh
rm -rf /tmp/cw-agent-install
# Inject the CloudWatch Logs configuration file contents
cat > /opt/aws/amazon-cloudwatch-agent/etc/config.json <<- EOF
{
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/dmesg",
"log_group_name": "ecs/var/log/dmesg",
"timezone": "UTC"
},
{
"file_path": "/var/log/messages",
"log_group_name": "ecs/var/log/messages",
"timezone": "UTC",
"timestamp_format": "%b %d %H:%M:%S"
},
{
"file_path": "/var/log/docker",
"log_group_name": "ecs/var/log/docker",
"timezone": "UTC",
"timestamp_format": "%Y-%m-%dT%H:%M:%S.%f"
},
{
"file_path": "/var/log/ecs/ecs-init.log",
"log_group_name": "ecs/var/log/ecs/ecs-init.log",
"timezone": "UTC",
"timestamp_format": "%Y-%m-%dT%H:%M:%SZ"
},
{
"file_path": "/var/log/ecs/ecs-agent.log.*",
"log_group_name": "ecs/var/log/ecs/ecs-agent.log",
"timezone": "UTC",
"timestamp_format": "%Y-%m-%dT%H:%M:%SZ"
},
{
"file_path": "/var/log/ecs/audit.log.*",
"log_group_name": "ecs/var/log/ecs/audit.log",
"timezone": "UTC",
"timestamp_format": "%Y-%m-%dT%H:%M:%SZ"
}
]
}
},
"log_stream_name": "{cluster}/{container_instance_id}"
}
}
EOF
--==BOUNDARY==
MIME-Version: 1.0
Content-Type: text/upstart-job; charset="us-ascii"
#upstart-job
description "Configure and start CloudWatch Logs agent on Amazon ECS container instance"
author "Amazon Web Services"
start on started ecs
script
exec 2>>/var/log/ecs/cloudwatch-logs-start.log
set -x
until curl -s http://localhost:51678/v1/metadata
do
sleep 1
done
# Grab the cluster and container instance ARN from instance metadata
cluster=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .Cluster')
container_instance_id=$(curl -s http://localhost:51678/v1/metadata | jq -r '. | .ContainerInstanceArn' | awk -F/ '{print $2}' )
# Replace the cluster name and container instance ID placeholders with the actual values
sed -i -e "s/{cluster}/$cluster/g" /opt/aws/amazon-cloudwatch-agent/etc/config.json
sed -i -e "s/{container_instance_id}/$container_instance_id/g" /opt/aws/amazon-cloudwatch-agent/etc/config.json
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/etc/config.json -s
end script
--==BOUNDARY==--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment