python 一个lambda函数,用于拍摄区域中所有EC2实例的快照,并在设定的天数后清除快照。这个

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 一个lambda函数,用于拍摄区域中所有EC2实例的快照,并在设定的天数后清除快照。这个相关的知识,希望对你有一定的参考价值。

import boto3
import logging
import datetime
import re
import time

#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.ERROR)

#define the connection
ec2 = boto3.resource('ec2', region_name="us-west-2")

#set the snapshot removal offset
cleanDate = datetime.datetime.now()-datetime.timedelta(days=5)

#Set this to True if you don't want the function to perform any actions
debugMode = False

def lambda_handler(event, context):
    
    if debugMode == True:
        print("-------DEBUG MODE----------")
 
    #snapshop the instances
    for vol in ec2.volumes.all():
        tempTags=[]
        
        #Prepare Volume tags to be importated into the snapshot
        if vol.tags != None:
            for t in vol.tags:
                
                #pull the name tag
                if t['Key'] == 'Name':
                    instanceName =  t['Value']
                    tempTags.append(t)
                else:
                    tempTags.append(t)
        else:
            print("Issue retriving tag")
            instanceName = "NoName"
            t['Key'] = 'Name'
            t['Value'] = 'Missing'
            tempTags.append(t)
        
        description = str(datetime.datetime.now()) + "-" + instanceName + "-" + vol.id + "-automated"
        
        if debugMode != True:
            #snapshot that server
            snapshot = ec2.create_snapshot(VolumeId=vol.id, Description=description)
            
            #write the tags to the snapshot
            tags = snapshot.create_tags(
                    Tags=tempTags
                )
            print("[LOG] " + str(snapshot))
            
        else:
            print("[DEBUG] " + str(tempTags))
    
    print "[LOG] Cleaning out old entries starting on " + str(cleanDate)
    
    #clean up old snapshots
    for snap in ec2.snapshots.all():

        #veryify results have a value
        if snap.description.endswith("-automated"): 
            
            #Pull the snapshot date
            snapDate = snap.start_time.replace(tzinfo=None)
            if debugMode == True:
                print("[DEBUG] " + str(snapDate) +" vs " + str(cleanDate)) 
            
            #Compare the clean dates
            if cleanDate > snapDate:
                print("[INFO] Deleteing: " + snap.id + " - From: " + str(snapDate))
                if debugMode != True:
                    try:
                        snapshot = snap.delete()
                    except:
                        
                        #if we timeout because of a rate limit being exceeded, give it a rest of a few seconds
                        print("[INFO]: Waiting 5 Seconds for the API to Chill")
                        time.sleep(5)
                        snapshot = snap.delete()
                    print("[INFO] " + str(snapshot))

以上是关于python 一个lambda函数,用于拍摄区域中所有EC2实例的快照,并在设定的天数后清除快照。这个的主要内容,如果未能解决你的问题,请参考以下文章

AWS Lambda - 在区域之间自动复制 EC2 快照?

使用 Lambda@Edge 在边缘站点自定义内容

任何人都可以解释python中的lambda函数吗? [复制]

Python3中lambda表达式介绍

Python3中lambda表达式介绍

如何将 AWS 区域传递给 lambda 函数?