how to display all snapshots in my aws account using python boto3 - python

the aim of this program is to delete snapshots that are older than 60 days. when run it displays the following error " a=snapshot[s].start_time
AttributeError: 'dict' object has no attribute 'start_time' " This is my code
#!/usr/bin/env python
import boto3
import datetime
client = boto3.client('ec2')
snapshot= client.describe_snapshots()
for s in snapshot:
a=snapshot[s].start_time
b=a.date()
c=datetime.datetime.now().date()
d=c-b
if d.days>60 :
snapshot[s].delete(dry_run=True)

Your error is in the line a=snapshot[s].start_time, use a=s.start_time
Note I would change "snapshot" to "snapshots". then in your for loop:
for snapshot in snapshots:
This makes the code easier to read and clear on what your variables represent.
Another item is that start_time is a string. You will need to parse this to get a number. Here is an example to help you:
delete_time = datetime.utcnow() - timedelta(days=days)
for snapshot in snapshots:
start_time = datetime.strptime(
snapshot.start_time,
'%Y-%m-%dT%H:%M:%S.000Z'
)
if start_time < delete_time:
***delete your snapshot here***

This should do it-
import boto3
import json
import sys
from pprint import pprint
region = 'us-east-1'
ec2 = boto3.client('ec2', region)
resp = ec2.describe_instances()
resp_describe_snapshots = ec2.describe_snapshots(OwnerIds=['*******'])
snapshot = resp_describe_snapshots['Snapshots']
snapshots = [''];
for snapshotIdList in resp_describe_snapshots['Snapshots']:
snapshots.append(snapshotIdList.get('SnapshotId'))
for id in snapshots:
print(id)

Related

File in s3 bucket comes with no values when running python script through lambda in aws

I have the following script but when the file lands in s3 it comes blank.
import json
import urllib.parse
from datetime import datetime
import boto3
import botocore.session as bc
print('Loading function')
client_s3=boto3.client('s3')
def run_athena(BUCKET_NAME, PREFIX):
client=boto3.client('athena')
database = 'data_lake'
query ="SELECT buid, property_id, checkin_date, count(*) number_of_direct_bookings FROM data_lake.vw_qs_bookings_v1 where source_logic in ('sources_phone', 'sources_email', 'sources_website','sources_front_desk') and cancelled_flag=0 and checkin_date= date_add('day', -1, current_date) group by 1,2,3";
s3_output = 's3://gainsight-file/gainsight/'
response = client.start_query_execution(QueryString=query, QueryExecutionContext={'Database': database}, ResultConfiguration={'OutputLocation': s3_output})
today = datetime.today().strftime('%Y-%m-%d')
response_s3 = client_s3.list_objects(
Bucket=BUCKET_NAME,
Prefix=PREFIX,
)
name = response_s3["Contents"][0]["Key"]
client_s3.copy_object(Bucket=BUCKET_NAME, CopySource=BUCKET_NAME+'/'+name, Key=PREFIX+"vw_qs_bookings_v1_"+today)
client_s3.delete_object(Bucket=BUCKET_NAME, Key=name)
def lambda_handler(event, context):
print("Entered lambda_handler")
run_athena('gainsight-file', 'gainsight/')
I assume there is something to do with the query variable.
Thanks for the help.
client.start_query_execution(…) only starts a query execution, it does not wait for it to complete (because that could be up to 30 minutes). You need to call client.get_query_execution(…) until you get a response that says the execution is successful (or failed). Only then is the output available.

Python Boto3 SnapshotNotFound exception. Unable to delete snapshot

I'm new to AWS and I have written a Boto3 script which gets the snapshots by OwnerId and delete older snapshots one by one. I'm having a strange issue that boto client finds all Snapshots and when it reaches client.delete_snapshot(SnapshotId=snapshot.snapshot_id) It throws SnapshotNotFoundException - Unable to delete snapshot with id abcd.
It's weird that when i go to AWS Console and search for the ID, Snapshot is there and I can delete it from the AWS Console and I'm using the same account credentials with Boto3 config file.
[default]
aws_access_key_id=foo
aws_secret_access_key=bar
Here is what i have tried.
Boto3 Script
from datetime import datetime, timedelta, timezone
import boto3
ec2 = boto3.resource('ec2')
cl = boto3.client('ec2')
count=0
snapshots = ec2.snapshots.filter(OwnerIds=['xxxxxxxxxx'])
def if_associated_to_ami(client, snapshot_id):
img = client.describe_images(Filters=[{'Name': 'block-device-mapping.snapshot-id', 'Values': [snapshot_id]}])
try:
ami_id = img['Images'][0]['ImageId']
#print("Snapshot(" + snapshot_id + ") is associated to image(" + ami_id + "). Return True")
return True
except IndexError:
#print("Snapshot(" + snapshot_id + ") is not associated to any image. Return False")
return False
for snapshot in snapshots:
if if_associated_to_ami(cl, snapshot.snapshot_id):
print('Unabble to delete Snapshot with Id = {}. Snapshot is in used! '. format(snapshot.snapshot_id))
else:
start_time = snapshot.start_time
delete_time = datetime.now(tz=timezone.utc) - timedelta(days=90)
if delete_time > start_time:
#snapshot.delete()
cl.delete_snapshot(SnapshotId=snapshot.snapshot_id)
print('Snapshot with Id = {} is deleted '. format(snapshot.snapshot_id))
count+=1
if count == 1000:
break
if you face indentation issues, please check the file here.
https://github.com/DeveloperMujtaba/usual-resources/blob/master/boto3.py
can someone please indicate the issue please? It would be much appreciated. Thanks.
Honestly, just by looking at your code, I cannot tell why it bulks at that but also, b/c you already have the snapshot resource, why not just do:
snapshot.delete()

How to use Python boto3 to get count of files/object in s3 bucket older than 60 days?

I'm trying to get the count of all object which are older than 60 days? Is there any way to perform a query or any python boto3 method to get this required output?
Here is code to files or object from S3 bucket older than 60 days.
import json
import boto3
import datetime
import time
from time import mktime
client = boto3.client('s3')
response = client.list_objects(Bucket='angularbuildbucket')
print(response)
today_date_time = datetime.datetime.now().replace(tzinfo=None)
print(today_date_time)
for file in response.get("Contents"):
file_name =file.get("Key")
modified_time = file.get("LastModified").replace(tzinfo=None)
difference_days_delta = today_date_time - modified_time
difference_days = difference_days_delta.days
print("difference_days---", difference_days)
if difference_days > 60:
print("file more than 60 days older : - ", file_name)
Note: Make sure if you running this code locally to set AWS CLI environment and pass profile rightly.

python (boto3) program to delete old snapshots in aws by description

So i am following this guide which is tremendously helpful for creating and deleting snapshots that are older than 3 days. the trouble is, it looks like the python script the author posted deletes all snapshots that are older than 10 days. Within my env, I have level 1's that sometimes create manual snapshots for various reasons, so i cant have this lambda function delete those and I want it to filter the snapshots by descriptions that have
"Created by Lambda backup function ebs-snapshots"
Now, the author posted a way to filter snapshot created, so i attempted to mimic that by filtering descriptions for deletion, but I just want someone to check my work, and/or show me a better way, because what i have thus far is:
( Filters=[{'Description':['Created by Lambda backup function ebs-snapshots']}])
TLDR: How do I add a filter in this code that only targets snapshots with said above description
This is the authors code for deletion:
# Delete snapshots older than retention period
import boto3
from botocore.exceptions import ClientError
from datetime import datetime,timedelta
def delete_snapshot(snapshot_id, reg):
print "Deleting snapshot %s " % (snapshot_id)
try:
ec2resource = boto3.resource('ec2', region_name=reg)
snapshot = ec2resource.Snapshot(snapshot_id)
snapshot.delete()
except ClientError as e:
print "Caught exception: %s" % e
return
def lambda_handler(event, context):
# Get current timestamp in UTC
now = datetime.now()
# AWS Account ID
account_id = '1234567890'
# Define retention period in days
retention_days = 10
# Create EC2 client
ec2 = boto3.client('ec2')
# Get list of regions
regions = ec2.describe_regions().get('Regions',[] )
# Iterate over regions
for region in regions:
print "Checking region %s " % region['RegionName']
reg=region['RegionName']
# Connect to region
ec2 = boto3.client('ec2', region_name=reg)
# Filtering by snapshot timestamp comparison is not supported
# So we grab all snapshot id's
result = ec2.describe_snapshots( OwnerIds=[account_id] Filters=[{'Description':['Created by Lambda backup function ebs-snapshots']}])
for snapshot in result['Snapshots']:
print "Checking snapshot %s which was created on %s" % (snapshot['SnapshotId'],snapshot['StartTime'])
# Remove timezone info from snapshot in order for comparison to work below
snapshot_time = snapshot['StartTime'].replace(tzinfo=None)
# Subtract snapshot time from now returns a timedelta
# Check if the timedelta is greater than retention days
if (now - snapshot_time) > timedelta(retention_days):
print "Snapshot is older than configured retention of %d days" % (retention_days)
delete_snapshot(snapshot['SnapshotId'], reg)
else:
print "Snapshot is newer than configured retention of %d days so we keep it" % (retention_days)
The portion I updated was:
result = ec2.describe_snapshots( OwnerIds=[account_id] Filters=[{'Description':['Created by Lambda backup function ebs-snapshots']}])
is this correct syntactically?

Python xmlrpclib.Fault while using NetDNA's API

I am trying to write a Python script that will list all of my Pull Zones. Everytime I run the script I get the following error:
xmlrpclib.Fault: <Fault 620: 'Method "pullzone.list" does not exist'>
The documentation for List Zones is here: http://support.netdna.com/api/#pullzone.listZones
Here is the script:
#! /usr/bin/python
from xmlrpclib import ServerProxy
from hashlib import sha256
from datetime import datetime, timedelta
from pytz import timezone
apiKey = 'sldjlskdjf'
apiUserId = '0000'
def pullzoneListZones():
global apiKey, apiUserId
date = datetime.now(timezone('America/Los_Angeles')).replace(microsecond=0).isoformat() # Must be 'America/Los_Angeles' always!
authString = sha256(date + ":" + apiKey + ":listZones").hexdigest()
sp = ServerProxy('http://api.netdna.com/xmlrpc/pullzone')
return sp.pullzone.list(apiUserId, authString, date)
print pullzoneListZones()
What am I missing? Thanks in advance. Disclaimer: I work for NetDNA but know one here knows Python.
Thanks in advance.
The method is wrongly named - it should be
sp.pullzone.listZones(apiUserId, authString, date)
See http://support.netdna.com/api/#Python for api names.

Categories

Resources