提问人:Pau Seglar 提问时间:10/30/2023 最后编辑:Jason AllerPau Seglar 更新时间:10/31/2023 访问量:124
如何将自定义日志从 AL2023 实例流式传输到 cloudwatch
How to stream custom logs to cloudwatch from AL2023 instances
问:
我们使用 amazon-elastic-beanstalk 来部署部分堆栈。 我们刚刚将基础平台从使用 Amazon Linux 2 的 PHP 7.4 迁移到使用 Amazon Linux 2023 的 PHP 8.2。
默认系统日志正在正确流式传输(/var/log/nginx/access.log、/var/log/eb-hooks.log 等),但自定义日志流式传输到 cloudwatch 不起作用。
我 https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/AWSHowTo.cloudwatchlogs.html 遵循本指南,以便在 amazon-linux-2 中启用日志流
(该指南尚未更新到 amazon-linux-2023)
在 AL2 中,我们声明要发送的日志,创建一个 ebextension,该扩展在其中添加了文件,如 awsdocs 中的此示例所述/etc/awslogs/config/logs.conf
此外,我必须通过创建另一个 ebextension 来启用 Cloudwatch 日志流:.ebextension/95-logs-streamtocloudwatch.config
option_settings:
aws:elasticbeanstalk:cloudwatch:logs:
StreamLogs: true
DeleteOnTerminate: false
RetentionInDays: 30
最后,我创建了一个策略,并将其添加到 EC2 实例配置文件中定义的角色,如官方指南中所述:将 Elastic Beanstalk 与 Amazon CloudWatch Logs 结合使用
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams"
],
"Resource": [
"*"
]
}
]
}
来自:官方来源
该配置在 amazon-linux-2 实例中运行良好。但在 Amazon Linux 2023 实例中不起作用
答:
多亏了这个现有的答案,我找到了解决方案
我所做的是编辑 .ebextension 文件,以便在 /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/ 文件夹下设置 cloudwatch 代理的配置。
这是我生成的 .ebextension 文件
option_settings:
aws:elasticbeanstalk:cloudwatch:logs:
StreamLogs: true
DeleteOnTerminate: false
RetentionInDays: 30
packages:
yum:
amazon-cloudwatch-agent: []
files:
"/etc/awslogs/awscli.conf" :
mode: "000600"
owner: root
group: root
content: |
[plugins]
cwlogs = cwlogs
[default]
region = `{"Ref":"AWS::Region"}`
"/etc/awslogs/awslogs.conf" :
mode: "000600"
owner: root
group: root
content: |
[general]
state_file = /var/lib/awslogs/agent-state
"/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/custom_logs.json":
mode: "000600"
owner: root
group: root
content: |
{
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/php-fpm/mycustomlog.log",
"log_group_name": "/aws/elasticbeanstalk/erp-prod-php8/var/log/php-fpm/mycustomlog.log",
"log_stream_name": "{instance_id}"
}
]
}
}
}
}
commands:
"01":
command: systemctl enable amazon-cloudwatch-agent.service
"02":
command: systemctl restart amazon-cloudwatch-agent
评论
amazon-cloudwatch-agent.d
awslogs
评论