Every so often you may find yourself with an unattached EBS Volume laying around not knowing what instance it belonged to.
You can use CloudTrail to find this information, but if you have not setup anything beyond the default trail you will only have 90 days of history.
Here is a small and simple Lambda that will add a new tag to all your EBS volumes with the the name of the instance they are currently attached to. This will allow you to see the last known instance that a volume was attached to.
Add the following code to a Python (tested with 2.7) Lambda:
We will need to attach the following policy to the Lambda so that it can do its job properly:
The easiest way to schedule this to run is to setup a CloudWatch rule. Much like a cron we can set this to run as often as we would like. We can even use cron expression to schedule it. Then select the Lambda we created as the target.
Once we have created the CloudWatch rule we should see new tags on our EBS volumes with information about their attachment once it has ran. We will need to click the gear icon on in the EC2 console so that it will show our new tag.
Now we should see our new tag(s):