Skip to content

Instantly share code, notes, and snippets.

@eefahy
Last active April 15, 2019 15:14
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 eefahy/d4d96394f89973bedfa3974cba605b1f to your computer and use it in GitHub Desktop.
Save eefahy/d4d96394f89973bedfa3974cba605b1f to your computer and use it in GitHub Desktop.
#!/bin/bash
# Got most of this from http://docs.aws.amazon.com/AmazonECS/latest/developerguide/using_cloudwatch_logs.html
# and https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using_awslogs.html
# Install awslogs and the jq JSON parser
yum install -y awslogs jq aws-cli nfs-utils
# ECS config
# ECS_AVAILABLE_LOGGING_DRIVERS is needed if you're not using the ECS optimized ami
{
echo "ECS_CLUSTER=${cluster_name}"
echo "ECS_AVAILABLE_LOGGING_DRIVERS=${ecs_logging}"
} >> /etc/ecs/ecs.config
# Inject the CloudWatch Logs configuration file contents
cat > /etc/awslogs/awslogs.conf <<- EOF
[general]
state_file = /var/lib/awslogs/agent-state
[/var/log/dmesg]
file = /var/log/dmesg
log_group_name = ${cloudwatch_prefix}-${cluster_name}-/var/log/dmesg
log_stream_name = ${cluster_name}/{container_instance_id}
[/var/log/messages]
file = /var/log/messages
log_group_name = ${cloudwatch_prefix}-${cluster_name}-/var/log/messages
log_stream_name = ${cluster_name}/{container_instance_id}
datetime_format = %b %d %H:%M:%S
[/var/log/docker]
file = /var/log/docker
log_group_name = ${cloudwatch_prefix}-${cluster_name}-/var/log/docker
log_stream_name = ${cluster_name}/{container_instance_id}
datetime_format = %Y-%m-%dT%H:%M:%S.%f
[/var/log/ecs/ecs-init.log]
file = /var/log/ecs/ecs-init.log.*
log_group_name = ${cloudwatch_prefix}-${cluster_name}-/var/log/ecs/ecs-init.log
log_stream_name = ${cluster_name}/{container_instance_id}
datetime_format = %Y-%m-%dT%H:%M:%SZ
[/var/log/ecs/ecs-agent.log]
file = /var/log/ecs/ecs-agent.log.*
log_group_name = ${cloudwatch_prefix}-${cluster_name}-/var/log/ecs/ecs-agent.log
log_stream_name = ${cluster_name}/{container_instance_id}
datetime_format = %Y-%m-%dT%H:%M:%SZ
[/var/log/ecs/audit.log]
file = /var/log/ecs/audit.log.*
log_group_name = ${cloudwatch_prefix}-${cluster_name}-/var/log/ecs/audit.log
log_stream_name = ${cluster_name}/{container_instance_id}
datetime_format = %Y-%m-%dT%H:%M:%SZ
[/var/lib/docker/containers/]
file = /var/lib/docker/containers/*/*
log_group_name = ${cloudwatch_prefix}-${cluster_name}-/var/lib/docker/containers
log_stream_name = ${cluster_name}/{container_instance_id}
datetime_format = %Y-%m-%dT%H:%M:%SZ
EOF
# Set the region to send CloudWatch Logs data to (the region where the container instance is located)
region=$(curl 169.254.169.254/latest/meta-data/placement/availability-zone | sed s'/.$//')
sed -i -e "s/region = us-east-1/region = $region/g" /etc/awslogs/awscli.conf
# Set the ip address of the node
container_instance_id=$(curl 169.254.169.254/latest/meta-data/local-ipv4)
sed -i -e "s/{container_instance_id}/$container_instance_id/g" /etc/awslogs/awslogs.conf
cat > /etc/init/awslogjob.conf <<- EOF
#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
service awslogs start
chkconfig awslogs on
end script
EOF
start ecs
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment