In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
This section takes a look at how to manage snapshots of EC2 through boto3. In the actual production environment, beans use the solution EBS Snapshot Scheduler provided by AWS, which is directly imported into the stack of Cloudformation. The Lambda function and DynamoDB database are automatically configured, and then we can set them through tags. From a learning point of view, let's go straight to an ultra-simple version. We can write two Lambda functions directly, one to create and the other to delete.
First, set a tag tag
Next, create the Lambda function
The configuration of IAM Role is as follows
{"Version": "2012-10-17", "Statement": [{"Effect": "Allow", "Action": ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"], "Resource": "arn:aws:logs:*"}, {"Effect": "Allow" "Action": ["ec2:CreateSnapshot", "ec2:CreateTags", "ec2:DeleteSnapshot", "ec2:Describe*", "ec2:ModifySnapshotAttribute", "ec2:ResetSnapshotAttribute"], "Resource": "*"}
The specific functions are as follows:
From datetimeimport datetimeimport boto3def lambda_handler (event, context): ec2_client = boto3.client ('ec2') # get the names of all region regions = [region [' RegionName'] for region in ec2_client.describe_regions () ['Regions']] # loop every region Find all instances labeled backup for region in regions: print ('Instances in EC2 Region {0}:' .format (region)) ec2 = boto3.resource ('ec2', region_name=region) instances = ec2.instances.filter (Filters= [{' Name': 'tag:backup') 'Values': [' true']}]) # get the timestamp # ISO 8601 timestamp, i.e. 2019-01-31T14:01:58 timestamp = datetime.utcnow (). Replace (microsecond=0). Isoformat () # for each volume of each instance All create a snapshot for i in instances.all (): for v in i.volumes.all (): desc = 'Backup of {0}, volume {1}, created {2}' .format (i.id, v.id) Timestamp) print (desc) snapshot = v.create_snapshot (Description=desc) print ("Created snapshot:", snapshot.id)
Then set a scheduled task in Cloudwatch and execute this function on a regular basis
This is a schematic diagram of binding Role and trigger.
After execution, you can view the snapshot
Looking at the output log of print in Cloudwatch, you can see that it has been successfully executed.
In the same way, we can create a Lambda function to delete the snapshot
The specific functions are as follows:
Import boto3def lambda_handler (event, context): # sts returns a dictionary and obtains the ownerId of the current account through get If failed, None account_id = boto3.client ('sts') .get_caller_identity () .get (' Account') ec2 = boto3.client ('ec2') ": type: pyboto3.ec2" regions = [region [' RegionName'] for region in ec2.describe_regions () ['Regions']] for region in regions: print ("Region:", region) ec2 = boto3.client (' ec2') Region_name=region) "": type: pyboto3.ec2 "" response = ec2.describe_snapshots (OwnerIds= [account _ id]) snapshots = response ["Snapshots"] print (snapshots) # Snapshot is a long list Each element is a dictionary structure Sort specifies sorting by time # the following is equivalent to # def sortTime (x): # return x ["StartTime"] # snapshots.sort (key=sortTime) # Sort snapshots by date ascending snapshots.sort (key=lambda x: X ["StartTime"]) # Remove snapshots we want to keep (i.e.3 most recent) snapshots = snapshots [:-3] for Snapshot in snapshots: id = snapshot ['SnapshotId'] try: print ("Deleting snapshot:" Id) ec2.delete_snapshot (SnapshotId=id) except Exception as e: print ("Snapshot {} in use, skipping." .format (id)) continue
You can also create scheduled task execution functions
Print log in CloudWatch after execution
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.