Created
May 22, 2019 21:07
-
-
Save chtz/a9e9970ea5bb9d69441ee4169daa30c6 to your computer and use it in GitHub Desktop.
Install AWS CloudWatch log agent on Amazon Linux 2
This file contains hidden or 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 | |
# Pre cond: | |
# | |
# EC2 IAM role with this IAM policy: | |
# { | |
# "Version": "2012-10-17", | |
# "Statement": [ | |
# { | |
# "Effect": "Allow", | |
# "Action": [ | |
# "logs:CreateLogGroup", | |
# "logs:CreateLogStream", | |
# "logs:PutLogEvents", | |
# "logs:DescribeLogStreams" | |
# ], | |
# "Resource": [ | |
# "arn:aws:logs:*:*:*" | |
# ] | |
# } | |
# ] | |
# } | |
# | |
# EC2 IAM role (above) is attached to EC2 instance | |
# Install CloudWatch Logs agend on Amazon Linux 2 | |
sudo yum update -y | |
sudo yum install -y awslogs | |
sudo tee -a /etc/awslogs/awscli.conf > /dev/null << EOL | |
[plugins] | |
cwlogs = cwlogs | |
[default] | |
region = $(ec2-metadata -z | grep -Po "(us|sa|eu|ap)-(north|south|central)?(east|west)?-[0-9]+") | |
EOL | |
sudo tee -a /etc/awslogs/awslogs.conf > /dev/null << EOL | |
[general] | |
state_file = /var/lib/awslogs/agent-state | |
[/var/log/customlog] | |
datetime_format = %b %d %H:%M:%S | |
file = /var/log/customlog | |
buffer_duration = 5000 | |
log_stream_name = {instance_id} | |
initial_position = start_of_file | |
log_group_name = /var/log/customlog | |
EOL | |
sudo systemctl start awslogsd | |
sudo systemctl enable awslogsd.service |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment