Skip to content

Instantly share code, notes, and snippets.

@tom-butler
Last active January 18, 2018 05:20
Show Gist options
  • Save tom-butler/063eb70f18a99ad6acfa18141ddbb22c to your computer and use it in GitHub Desktop.
Save tom-butler/063eb70f18a99ad6acfa18141ddbb22c to your computer and use it in GitHub Desktop.
ubuntu aws log exporter
[general]
state_file = /var/awslogs/state/agent-state
[/var/log/syslog]
file = /var/log/syslog
log_group_name = appname
log_stream_name = {instance_id}-/var/log/syslog
datetime_format = %b %d %H:%M:%S
[/var/log/tomcat8/catalina.out]
file = /var/log/tomcat8/catalina.out
log_group_name = appname
log_stream_name = {instance_id}-/var/log/tomcat8/catalina.out
datetime_format = %b %d %H:%M:%S
[/var/log/apache2/error.log]
file = /var/log/apache2/error.log
log_group_name = appname
log_stream_name = {instance_id}-/var/log/apache2/error.log
datetime_format = %b %d %H:%M:%S
# Install cloudwatch monitoring scripts requirements
sudo apt-get install -y python3-pip
sudo pip3 install --upgrade awscli
echo ==============================================================================
echo Configure Log Export
echo ==============================================================================
cd /tmp/
# Download the agent installer
curl https://s3.amazonaws.com//aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -O
chmod +x ./awslogs-agent-setup.py
# Run it passing our config
sudo python3 ./awslogs-agent-setup.py -n -r ap-southeast-2 -c /tmp/files/cloudwatch-log-config
# The policy to allow access to cloudwatch
resource "aws_iam_role_policy" "cloudwatch_logs_policy" {
name = "${var.stack_name}_${var.environment}_logs_policy"
# You will have to change this to point to your role.
role = "${aws_iam_role.install_script_role.id}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams"
],
"Resource": [
"arn:aws:logs:*:*:*"
]
}
]
}
EOF
}
@tom-butler
Copy link
Author

Using packer send your config file to /tmp/files/

Add the install script to your packer install script.

The server you deploy will need permissions to write to cloudwatch (see terraform script)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment