AWS EC2日志用户数据输出到CloudWatch日志

发布于 2025-01-31 09:27:39 字数 1136 浏览 4 评论 0原文

我正在使用EC2执行预处理任务。

我使用UserData变量执行Shell命令。我的UserData的最后一行具有sudo关闭-H。因此,一旦完成预处理任务完成,该实例就会自动终止。

这就是我的代码的样子。

import boto3


userdata = '''#!/bin/bash
pip3 install boto3 pandas scikit-learn
aws s3 cp s3://.../main.py .
python3 main.py
sudo shutdown now -h
'''


def launch_ec2():
    ec2 = boto3.resource('ec2',
                         aws_access_key_id="", 
                         aws_secret_access_key="",
                         region_name='us-east-1')
    instances = ec2.create_instances(
        ImageId='ami-0c02fb55956c7d316',
        MinCount=1,
        MaxCount=1,
        KeyName='',
        InstanceInitiatedShutdownBehavior='terminate',
        IamInstanceProfile={'Name': 'S3fullaccess'},
        InstanceType='m6i.4xlarge', 
        UserData=userdata,
        InstanceMarketOptions={
            'MarketType': 'spot',
            'SpotOptions': {
                'SpotInstanceType': 'one-time',
            }
        }
    )
    print(instances)


launch_ec2()

问题是,当我的Python脚本中存在错误时,脚本死亡并且实例终止。

有没有办法收集错误/信息日志并在实例终止之前将其发送到CloudWatch?这样,我会知道出了什么问题。

I'm doing pre-processing tasks using EC2.

I execute shell commands using the userdata variable. The last line of my userdata has sudo shutdown now -h. So the instance gets terminated automatically once the pre-processing task completed.

This is how my code looks like.

import boto3


userdata = '''#!/bin/bash
pip3 install boto3 pandas scikit-learn
aws s3 cp s3://.../main.py .
python3 main.py
sudo shutdown now -h
'''


def launch_ec2():
    ec2 = boto3.resource('ec2',
                         aws_access_key_id="", 
                         aws_secret_access_key="",
                         region_name='us-east-1')
    instances = ec2.create_instances(
        ImageId='ami-0c02fb55956c7d316',
        MinCount=1,
        MaxCount=1,
        KeyName='',
        InstanceInitiatedShutdownBehavior='terminate',
        IamInstanceProfile={'Name': 'S3fullaccess'},
        InstanceType='m6i.4xlarge', 
        UserData=userdata,
        InstanceMarketOptions={
            'MarketType': 'spot',
            'SpotOptions': {
                'SpotInstanceType': 'one-time',
            }
        }
    )
    print(instances)


launch_ec2()

The problem is, sometime when there is an error in my python script, the script dies and the instance get terminated.

Is there a way I can collect error/info logs and send it to cloudwatch before the instance get terminated? This way, I would know what went wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2025-02-07 09:27:39

您可以通过利用 bash 功能,您可以实现所需的行为。
实际上,您可以为UserData的整个执行创建一个日志文件,并且可以使用trap来确保在终止错误之前将日志文件复制到S3。

以下是:

#!/bin/bash -xe
exec &>> /tmp/userdata_execution.log

upload_log() {
  aws s3 cp /tmp/userdata_execution.log s3://... # use a bucket of your choosing here
}

trap 'upload_log' ERR

pip3 install boto3 pandas scikit-learn
aws s3 cp s3://.../main.py .
python3 main.py
sudo shutdown now -h

一个日志文件(/tmp/userdata_execution.log),其中包含stdout和stderr将为userData生成;如果在执行UserData期间存在错误,则日志文件将上传到S3存储桶。

如果愿意,您当然也可以将日志文件传输到CloudWatch,但是要这样做,您必须在实例上安装CloudWatch代理并进行相应的配置。我相信,对于您的用例,将日志文件上传到S3是最好的解决方案。

You can achieve the desired behavior by leveraging functionality.
You could in fact create a log file for the entire execution of the UserData, and you could use trap to make sure that the log file is copied over to S3 before terminating if an error occurs.

Here's how it could look:

#!/bin/bash -xe
exec &>> /tmp/userdata_execution.log

upload_log() {
  aws s3 cp /tmp/userdata_execution.log s3://... # use a bucket of your choosing here
}

trap 'upload_log' ERR

pip3 install boto3 pandas scikit-learn
aws s3 cp s3://.../main.py .
python3 main.py
sudo shutdown now -h

A log file (/tmp/userdata_execution.log) that contains stdout and stderr will be generated for the UserData; if there is an error during the execution of the UserData, the log file will be upload to an S3 bucket.

If you wanted to, you could of course also stream the log file to CloudWatch, however to do so you would have to install the CloudWatch agent on the instance and configure it accordingly. I believe that for your use case uploading the log file to S3 is the best solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文