Skip to content

Instantly share code, notes, and snippets.

@wolfeidau
Created September 11, 2017 01:57
Show Gist options
  • Save wolfeidau/80a179f658ac3dae12ed6ed9cc5473ba to your computer and use it in GitHub Desktop.
Save wolfeidau/80a179f658ac3dae12ed6ed9cc5473ba to your computer and use it in GitHub Desktop.
cloudwatchlogs setup userdata script
#!/bin/bash -e
yum update -y
yum install -y aws-cfn-bootstrap git aws-cli
# Install the files and packages from the metadata
/opt/aws/bin/cfn-init -v --stack "{{ aws_stack_name }}" \
--resource ECSInstanceLaunchConfiguration \
--configsets ConfigCluster \
--region "{{ ref('AWS::Region') }}"
# Install awslogs and the jq JSON parser
yum install -y awslogs jq
# 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 = {{ ref('Environment') }}#ecs#dmesg
log_stream_name = {cluster}/{container_instance_id}
[/var/log/messages]
file = /var/log/messages
log_group_name = {{ ref('Environment') }}#ecs#messages
log_stream_name = {cluster}/{container_instance_id}
datetime_format = %b %d %H:%M:%S
[/var/log/docker]
file = /var/log/docker
log_group_name = {{ ref('Environment') }}#ecs#docker
log_stream_name = {cluster}/{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 = {{ ref('Environment') }}#ecs#ecs-init.log
log_stream_name = {cluster}/{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 = {{ ref('Environment') }}#ecs#ecs-agent.log
log_stream_name = {cluster}/{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 = {{ ref('Environment') }}#ecs#audit.log
log_stream_name = {cluster}/{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
cat > /etc/init/ecs-awslogs.conf <<- 'EOF'
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" /etc/awslogs/awslogs.conf
sed -i -e "s/{container_instance_id}/$container_instance_id/g" /etc/awslogs/awslogs.conf
# Start and enable the CloudWatch Logs agent
service awslogs start
chkconfig awslogs on
end script
EOF
start ecs-awslogs
# install the SSM agent
yum install -y "https://amazon-ssm-${region}.s3.amazonaws.com/latest/linux_amd64/amazon-ssm-agent.rpm"
# Signal the status from cfn-init
/opt/aws/bin/cfn-signal -e $? \
--stack "{{ aws_stack_name }}" \
--resource ECSInstanceAutoScalingGroup \
--region "{{ ref('AWS::Region') }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment