未加星标

Tag AWS EC2 EBS volumes with the instance name tag

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Tagging in AWS is an extraordinarily powerful way to manage your assets. You can use tags for everything from searches in the AWS Console to preparing billing reports. To maximize the usefulness of the tagging system, you need ― surprise! ― to tag the AWS resources. But that’s not always automatic, especially when you are creating an asset that creates additional items. For example, creating an EC2 instance in the console also creates EBS volumes to support that instance. However, there is no way in the console to give the EBS volumes that are created a tag that matches the instance.

Instead you haveto go and find the volumes associated with the instance and tag them separately. This can be done in the Tag Editor. But I wanted a way to iterate through all the EBS volumes in an account and make sure they are named after the owning instance.

I’ve previously published PowerShell scripts that use the AWS PowerShell cmdlets to create nicely tagged snapshots of EBS volumes , one that creates AMIs by name tag and one that syncs up tags between your custom AMIs and its snapshots . One of the more popular posts on this blog is about using PowerShell hash tables with Amazon.EC2.Model.Filter . The script below is part of this growing suite of tagging scripts that makes use of Amazon.EC2.Model Filter with Get-Ec2Tag and New-EC2Tag .

It’s no secret I’m a big PowerShell fan, especially when PowerShell is combined with the AWS PowerShell cmdlets. The “everything is an object” nature of PowerShell just seems “right” when used with AWS. And in this script, I think you’ll see the power of the PowerShell pipeline in action as the Get-EC2Instance cmdlet passes everything I need to tag blank EBS volumes in the pipeline.

Two notes on this script:

If there’s a tag on an EBS volume, it is not replaced. Only blank tags are replaced with the value of the Name key on the instance’s tag EBS volumes that are not attached to an instance are not processed.

I hope this helps you and, as always, I look forward to your comments and feedback.

<# .SYNOPSIS If an EBS volume attached to an instance has a blank Name tag, this script tags the name tag with the instance name .DESCRIPTION (c) 2017 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. .NOTES =========================================================================== Created on: 2017-02-05 12:00 UTC Created by: Alex Neihaus Organization: Air11 Technology LLC Filename: Tag-EC2-EBS-Volumes-With-Name-From-EC2-Instance.ps1 =========================================================================== #> #> Import-Module AWSPowerShell # Be sure to import AWS PowerShell cmdlets. If not using default credentials, add them after this import #Set-AWSCredentials -ProfileName YOUR-STORED-PROFILE-HERE #Set-DefaultAWSRegion -Region YOUR-REGION-HERE (Get-EC2Instance).Instances | ` # Get EC2 instances and pass to pipeline ForEach-Object -Process { # Get the name tag of the current instance ID; Amazon.EC2.Model.Tag is in the Instances object $instanceName = $_.Tags | Where-Object -Property Key -EQ "Name" | Select-Object -ExpandProperty Value $_.BlockDeviceMappings | ` # Pass all the current block device objects down the pipeline ForEach-Object -Process { $volumeid = $_.ebs.volumeid # Retrieve current volume id for this BDM in the current instance # Get the current volume's Name tag $volumeNameTag = Get-EC2Tag -Filter @(@{ name = 'tag:Name'; values = "*" }; @{ name = "resource-type"; values = "volume" }; @{ name = "resource-id"; values = $volumeid }) | Select-Object -ExpandProperty Value if (-not $volumeNameTag) # Replace the tag in the volume if it is blank { New-EC2Tag -Resources $volumeid -Tags @{ Key = "Name"; Value = $instanceName } # Add volume name tag that matches InstanceID } } }

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

主题: PowerShellEBSTIUTUTC
分页:12
转载请注明
本文标题:Tag AWS EC2 EBS volumes with the instance name tag
本站链接:http://www.codesec.net/view/532683.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(50)