【问题标题】:Delete old snapshots on AWS删除 AWS 上的旧快照
【发布时间】:2021-03-18 03:45:28
【问题描述】:

我创建了一个 AWS cloudwatch,它将每天创建自动 EBS 快照。因此,cloudwatch 每天都会创建昨天的快照。

现在我想创建一些可以删除旧快照的东西。
我没有找到任何相关的东西,而且 AWS 支持也没有帮助我解决这种情况。

谁能帮我说明如何自动删除 aws ec2 EBS 快照?

例如,假设我只想保留最近 7 天的备份快照,并自动删除所有其余的。

如何通过 aws Lambda 或 cloudwatch 实现?

谢谢!

【问题讨论】:

标签: linux amazon-web-services amazon-ec2


【解决方案1】:

如果您想继续使用当前使用的相同工具,例如用于运行计划事件的 CloudWatch,您还可以创建一个 CloudWatch 事件来触发 lambda 函数(或一组)。 使用 lambda,您可以编写代码来实现您选择的任何逻辑,例如: 使用 AWS 开发工具包,列出所有 +7 天的快照,然后将其删除。

有用的链接:

Schedule AWS Lambda Functions Using CloudWatch Events

AWS SDK for JS

AWS SDK for Python

这是一篇非常好的博客文章,关于如何实现这一点 - Automating Amazon EBS Snapshot Management with AWS Step Functions and Amazon CloudWatch Events

【讨论】:

    【解决方案2】:

    关于设置 EBS 快照管理的答案很好,但如果您有一次性需要不费吹灰之力地摆脱一堆旧快照,您可以使用 AWS CLI 命令。

    我有一个大型的 AWS EC2 快照清理工作要做,有数以万计的旧快照要删除。这是我在 linux vbox 上使用的脚本。您可能想要更改的值在“TODO”中列出。

    
        # AWS Snapshot cleanup
        # Delete all snapshots before 2021 that include "SNAPSHOT OF my-env" in the beginning of the description
        # Fortified with aws profile, region, and output parameters for scripts that run more than 12 hours so it can be run again even if the cached credentials are stale.
        # This fixes the error "an error occurred (requestexpired) when calling the describesnapshots operation: request has expired."
        # If your organization uses multi-factor authentication (MFA) be sure you have an active authenticated session when running this script.
        #
        # TODO: replace the description, StartTime, profile, and region with values appropriate to your situation
        #  You can add --dry-run to the delete-snapshot command if you want to do a trial run
        #
        aws --profile prod --region us-east-1 --output text ec2 describe-snapshots --filters Name=description,Values='SNAPSHOT OF my-env*' --query "Snapshots[?(StartTime<='2021-01-01')].[SnapshotId]" | awk \
        '{snapid=$0; \
        print "Delete snapshot: " snapid; \
        system("aws --profile prod --region us-east-1 ec2 delete-snapshot --snapshot-id "snapid)\
        count++; \
        } \
         END { \
         print "Finished at:"; system("date"); \
         print "Total snapshots deleted: " count \
         }'
    
    

    【讨论】:

      猜你喜欢
      • 2018-06-15
      • 1970-01-01
      • 2015-05-05
      • 2021-01-29
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多