Last active
September 26, 2020 14:11
-
-
Save lethaldose/4103a64320b93475d7b308523b4a6c68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Timezone | |
ln -sf /usr/share/zoneinfo/Australia/Sydney /etc/localtime | |
#Using script from http://docs.aws.amazon.com/AmazonECS/latest/developerguide/using_cloudwatch_logs.html | |
# Install awslogs and the jq JSON parser | |
yum install -y awslogs jq aws-cli | |
# ECS config | |
${ecs_config} | |
{ | |
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}/var/log/dmesg | |
log_stream_name = ${cluster_name}/{container_instance_id} | |
[/var/log/messages] | |
file = /var/log/messages | |
log_group_name = ${cloudwatch_prefix}/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}/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}/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}/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}/var/log/ecs/audit.log | |
log_stream_name = ${cluster_name}/{container_instance_id} | |
datetime_format = %Y-%m-%dT%H:%M:%SZ | |
EOF | |
# 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 | |
# Write the awslogs bootstrap script to /usr/local/bin/bootstrap-awslogs.sh | |
cat > /usr/local/bin/bootstrap-awslogs.sh <<- EOF | |
#!/usr/bin/env bash | |
exec 2>>/var/log/ecs/cloudwatch-logs-start.log | |
set -x | |
until curl -s http://localhost:51678/v1/metadata | |
do | |
sleep 1 | |
done | |
# Set the region to send CloudWatch Logs data to (the region where the container instance is located) | |
cp /etc/awslogs/awscli.conf /etc/awslogs/awscli.conf.bak | |
region=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region) | |
sed -i -e "s/region = .*/region = $region/g" /etc/awslogs/awscli.conf | |
# 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 | |
cp /etc/awslogs/awslogs.conf /etc/awslogs/awslogs.conf.bak | |
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 | |
EOF | |
# Write the bootstrap-awslogs systemd unit file to /etc/systemd/system/bootstrap-awslogs.service | |
cat > /etc/systemd/system/bootstrap-awslogs.service <<- EOF | |
[Unit] | |
Description=Bootstrap awslogs agent | |
Requires=ecs.service | |
After=ecs.service | |
Before=awslogsd.service | |
[Service] | |
Type=oneshot | |
RemainAfterExit=yes | |
ExecStart=/usr/local/bin/bootstrap-awslogs.sh | |
[Install] | |
WantedBy=awslogsd.service | |
EOF | |
chmod +x /usr/local/bin/bootstrap-awslogs.sh | |
systemctl daemon-reload | |
systemctl enable bootstrap-awslogs.service | |
systemctl enable awslogsd.service | |
systemctl start awslogsd.service --no-block | |
#Custom userdata script code | |
${custom_userdata} | |
echo "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment