How to tag AWS EC2 AMI snapshots with a tag from the owning AMI

loop
I’ve written before about my efforts to make sure that custom EC2 AMIs have easy-to-understand tags.

That script works for a newly created AMI, neatly tagging the associated S3 snapshots so you can make sense of them in the AWS console.

But what about AMIs that you made before you decided to keep everything nice and neat, at least with respect to tags? Or, what if you accidentally blew away your snapshots’ tags (oops!) and want to re-tag them with something that visually links them to the AMI (besides just the AMI id?)

That’s what this little script does. It simply loops through all your AMIs (note the -Owner self parameter of the Get-EC2Image cmdlet) and then retrieves the “Name” tag key for the current snapshot (I used the “Name” key since the AWS console shows it by default). When it finds a snapshot associated with the current AMI with a tag key of “Name” key that is not a case-sensitive match for the existing “Name” value currently stored with the snapshot, it replaces the current “Name” tag value from the AMI.

It was so much fun to write — embedded loops processing object attributes. And, a bonus triple-hash-table use of Amazon.EC2.Model.Filter! Don’t wake me up from the dream.

I hope you find this useful. You should probably add some error checking and, of course, you might not want to set the snapshot “Name” key to be the same as the AMI “Name” key. But the great thing about this is if you ever violate your own AMI naming scheme, just run this little PowerShell script and in seconds everything is back to way it should be: neatly tagged for humans instead of computers.

(The script below has been updated and simplified as of 2016-05-03.)

<# 
    .NOTES
    ===========================================================================
     Created on:   2016-05-03 16:00 UTC
     Created by:    Alex Neihaus
     Organization:  Air11 Technology LLC
     Filename:      TagEC2SnapshotsWithNameFromAMI.ps1
    ===========================================================================
    .DESCRIPTION
        (c) 2016 Air11 Technology LLC
 
        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this file except in compliance with the License.
        You may obtain a copy of the License at
 
        http://www.apache.org/licenses/LICENSE-2.0
 
        Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License. #>
#>
Import-Module AWSPowerShell # Be sure to import AWS PowerShell cmdlets. If not using default credentials, add them after this import

$o = Get-EC2Image -Owner self # Get all AMIs created by this account. Note "self" is case-sensitive

foreach ($AMI in $o) # Process all our AMIs

{
    $amiBlockDeviceMapping = $AMI.BlockDeviceMapping # Get Amazon.Ec2.Model.BlockDeviceMapping, which contains snapshot IDs for this AMI
    foreach ($snapshotid in $amiBlockDeviceMapping.ebs.snapshotid) # Process each snapshot

    {
        # Next statement calls Get-EC2Tag with an array of filters in hash tables (see https://www.yobyot.com/aws/how-to-use-amazon-ec2-model-filter-with-powershell-to-identify-ec2-instances/2015/01/26/) and selects the snapshot's Name tag
        $snapshotNameTag = Get-EC2Tag -Filter @(@{ name = 'tag:Name'; values = "*" }; @{ name = "resource-type"; values = "snapshot" }; @{ name = "resource-id"; values = $snapshotid }) | Select-Object -ExpandProperty Value
        
        if ($AMI.Name -cne $snapshotNameTag) # Replace the tag in the snapshot if it does NOT match the Name tag in the AMI

        {
            New-EC2Tag -Resources $snapshotid -Tags @{ Key = "Name"; Value = $AMI.Name } # Add/overwrite "Name" tag in each snapshot of this AMI
            Write-Host "Replaced tag in"  $snapshotid "with"  $AMI.Name
        }
    }
}

 


Posted

in

, ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *