使用AWS Lambda在7天后删除快照

时间:2016-11-04 16:41:02

标签: amazon-web-services lambda

我尝试使用以下脚本为 标记 名称 [备份或备份] 根据https://serverlesscode.com/post/lambda-schedule-ebs-snapshot-backups/&& https://serverlesscode.com/post/lambda-schedule-ebs-snapshot-backups-2/ 我已成功创建了带有DeleteOn标记的快照,如第一个和第二个链接中所述。第二个链接的后半部分解释了如何在指定日期删除这些快照。基于该代码,我有以下内容在7天后删除快照。

这是代码:

public class RowMenu implements Callback<TableView<Person>, TableRow<Person>> {

    @Override
    public TableRow<Person> call(TableView<Person> table){
        final TableRow row = new TableRow();
        final ContextMenu contextMenu = new ContextMenu();
        final MenuItem removeMenuItem = new MenuItem("Remove");
        removeMenuItem.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println(row.getItem());
                System.out.println(table.getItems().remove(row.getItem()));

            }
        });
        contextMenu.getItems().addAll(removeMenuItem);
        row.setContextMenu(contextMenu);
        row.contextMenuProperty().bind(
                Bindings.when(row.emptyProperty())
                        .then((ContextMenu)null)
                        .otherwise(contextMenu)
        );
        return row;
    }
}

通过这样做,我收到以下错误:

import boto3
import re
import datetime

ec = boto3.client('ec2')
iam = boto3.client('iam')

def lambda_handler(event, context):
    account_ids = list('123456789011')
    try:       
        iam.get_user()
    except Exception as e:
        account_ids.append(re.search(r'(arn:aws:sts::)([0-9]+)', str(e)).groups()[1])


    delete_on = datetime.date.today().strftime('%Y-%m-%d')
        filters = [
        {'Name': 'tag-key', 'Values': ['DeleteOn']},
        {'Name': 'tag-value', 'Values': [delete_on]},
    ]
    snapshot_response = ec.describe_snapshots(OwnerIds=account_ids, Filters=filters)


    for snap in snapshot_response['Snapshots']:
        print "Deleting snapshot %s" % snap['SnapshotId']
        ec.delete_snapshot(SnapshotId=snap['SnapshotId'])

1 个答案:

答案 0 :(得分:0)

我通过以下方式更新上述代码的一部分来解决它:

def lambda_handler(event, context):
account_ids = ['123456789011']