Install and Configure AWS CloudWatch in Sixty Seconds or Less

Install and configure AWS CloudWatch in under sixty seconds.  Here's one of my goto scripts to install and configure AWS CloudWatch.

################################################################################################################
# cloud watch agent
#
#
sudo wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm
# install the agent
sudo rpm -U ./amazon-cloudwatch-agent.rpm

# start it - it seems i need to start it one time before creating my config file below
# if i create the file first, it seems to get deleted on the intial start
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a start

# define the path
readonly CLOUD_WATCH_AGENT_CONFIG_PATH="/opt/aws/amazon-cloudwatch-agent/etc"

# may need create the directory
# the install or start should have done that - but just be aware (just in case)
#mkdir -p $CLOUD_WATCH_AGENT_CONFIG_PATH

# define the cloudwatch agent config file
readonly CLOUD_WATCH_AGENT_CONFIG_FILE_TEMP="${CLOUD_WATCH_AGENT_CONFIG_PATH}/temp-config.json"
readonly CLOUD_WATCH_AGENT_CONFIG_FILE="${CLOUD_WATCH_AGENT_CONFIG_PATH}/amazon-cloudwatch-agent.json"

echo "Making Primary Config File (temp and final path)"
echo "${CLOUD_WATCH_AGENT_CONFIG_FILE_TEMP}"
echo "${CLOUD_WATCH_AGENT_CONFIG_FILE}"



# define the contents of the file. use the temp file incase the main file gets wiped out
# __PROJECT_NAME__ will get replaced in the next section
sudo cat > "${CLOUD_WATCH_AGENT_CONFIG_FILE_TEMP}" << 'EOF'
{
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log",
"log_group_name": "__ENVIRONMENT__/__PROJECT_NAME__",
"log_stream_name": "cloudwatch-agent-{instance_id}",
"timezone": "UTC"
},
{
"file_path": "/var/log/__PROJECT_NAME__/*",
"log_group_name": "__ENVIRONMENT__/__PROJECT_NAME__",
"log_stream_name": "general-{instance_id}",
"timezone": "UTC"
},
{
"file_path": "/var/log/user-data.log",
"log_group_name": "__ENVIRONMENT__/__PROJECT_NAME__",
"log_stream_name": "user-data-{instance_id}",
"timezone": "UTC"
}
]
}
},
"force_flush_interval" : 15
}
}
EOF



# this should be loaded as an environment variable, or you can set it here
if [ -z $PROJECT ]
then
# not found so set the variable to one of your choosing
export PROJECT="project_name_unknown"
# write it out to the file to persist on reboots
echo "export PROJECT=${PROJECT}" >> /etc/profile.d/export_instance_tags.sh
fi

if [ -z $ENVIRONMENT ]
then
# not found so set the variable to one of your choosing
export ENVIRONMENT="dev"
# write it out to the file to persist on reboots
echo "export ENVIRONMENT=${ENVIRONMENT}" >> /etc/profile.d/export_instance_tags.sh
fi


# replace the __PLACEHOLDERS__ with the $VAR values
# IMPORTANT remove spaces from project name
sudo sed -i "s|__PROJECT_NAME__|${PROJECT// /}|g" "${CLOUD_WATCH_AGENT_CONFIG_FILE_TEMP}"
sudo sed -i "s|__ENVIRONMENT__|${ENVIRONMENT}|g" "${CLOUD_WATCH_AGENT_CONFIG_FILE_TEMP}"
cp ${CLOUD_WATCH_AGENT_CONFIG_FILE_TEMP} ${CLOUD_WATCH_AGENT_CONFIG_FILE}


# restart it to load the new configuration
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a stop
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a start

# run a status check, so it will appear in the user-data logs
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status

#
#
# / cloud watch agent
################################################################################################################

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.