-
-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement delete old AMIs feature #4
Conversation
ami_cleanup.py
Outdated
).get( | ||
'Reservations', [] | ||
) | ||
images = ec2.images.filter(Owners=[os.environ['ami_owner']], Filters=[{'Name': 'tag-key', 'Values': ['AMIDeleteOn']}]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not filter on instance id. Won't there be multiple lambdas deployed at the same time? How do we keep them from stepping on each other? @comeanother
main.tf
Outdated
@@ -112,7 +120,7 @@ resource "aws_lambda_function" "ami_backup" { | |||
resource "aws_lambda_function" "ami_cleanup" { | |||
filename = "${path.module}/ami_cleanup.zip" | |||
function_name = "${module.label_cleanup.id}" | |||
description = "Cleanup old AMI backups" | |||
description = "Automatically removal AMI images of EC2 instance (remove AMI)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Automatically remove AMIs that have expired (delete AMI)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified
README.md
Outdated
@@ -17,8 +17,7 @@ Notes: | |||
|
|||
``` | |||
module "lambda_ami_backup" { | |||
source = "git::https://github.com/cloudposse/tf_lambda_ami_backup.git?ref=master" | |||
|
|||
source = "git::https://github.com/cloudposse/tf_lambda_ami_backup.git?ref=master" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace tf_lambda_ami_backup
with tf_ami_backup
ami_cleanup.py
Outdated
@@ -2,13 +2,22 @@ | |||
# | |||
# @author Robert Kozora <[email protected]> | |||
# | |||
# This script will search for all instances having a tag with "Backup" or "backup" | |||
# This script will search for all instances having a tag with "AMIDeleteOn" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace instances
with AMIs
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced
ami_cleanup.py
Outdated
# on it. As soon as we have the instances list, we loop through each instance | ||
# and reference the AMIs of that instance. We check that the latest daily backup | ||
# succeeded then we store every image that's reached its DeleteOn tag's date for | ||
# deletion. We then loop through the AMIs, deregister them and remove all the | ||
# snapshots associated with that AMI. | ||
|
||
# snapshots associated withg that AMI. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace withg
with with
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced
ami_cleanup.py
Outdated
from __future__ import print_function | ||
from __future__ import print_function | ||
from __future__ import print_function | ||
from __future__ import print_function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove line duplicates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
ami_cleanup.py
Outdated
if image.name.startswith(label_id + '-' + instance_id): | ||
|
||
# Count this image's occurance | ||
imagecount = imagecount + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete unused variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleted
ami_cleanup.py
Outdated
|
||
print "About to process the following AMIs:" | ||
print imagesList | ||
imagecount = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete unused variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleted
ami_cleanup.py
Outdated
delete_date = False | ||
|
||
today_time = datetime.datetime.now().strftime('%m-%d-%Y') | ||
# today_fmt = today_time.strftime('%m-%d-%Y') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete unused code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM... I think the AMI
prefix on AMIDeleteOn
is redundant since the tag is applied to an AMI it does not disambiguate in any useful way.
@s2504s was it tested? |
Backup and Cleanup has been successfully tested manually:
|
What
Why