Skip to content
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

Merged
merged 8 commits into from
Aug 28, 2017
Merged

Implement delete old AMIs feature #4

merged 8 commits into from
Aug 28, 2017

Conversation

s2504s
Copy link

@s2504s s2504s commented Aug 22, 2017

What

  • Implement delete old AMIs

Why

  • Old AMIs should be deleted after retention period

@s2504s s2504s requested review from osterman and const-bon August 22, 2017 18:19
@s2504s s2504s self-assigned this Aug 22, 2017
ami_cleanup.py Outdated
).get(
'Reservations', []
)
images = ec2.images.filter(Owners=[os.environ['ami_owner']], Filters=[{'Name': 'tag-key', 'Values': ['AMIDeleteOn']}])
Copy link
Member

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)"
Copy link
Member

@osterman osterman Aug 24, 2017

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)

Copy link
Author

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"

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"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace instances with AMIs.

Copy link
Author

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace withg with with.

Copy link
Author

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line duplicates.

Copy link
Author

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete unused variable.

Copy link
Author

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete unused variable.

Copy link
Author

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')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete unused code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted

Copy link
Member

@osterman osterman left a 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.

@const-bon
Copy link

@s2504s was it tested?

@const-bon
Copy link

Backup and Cleanup has been successfully tested manually:

  1. Manually trigger backup.
  2. Change AMIDeleteOn tag of newly created AMI.
  3. Manually trigger cleanup.

@const-bon const-bon changed the title Implement delete old AMIs Implement delete old AMIs feature Aug 28, 2017
@const-bon const-bon merged commit 5866299 into master Aug 28, 2017
@const-bon const-bon deleted the fix_cleanup branch August 28, 2017 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants