A Tag is an user defined metadata or label to an AWS resource to identify and manage it efficiently. Tag consists of a Key and an optional Value. It helps us to identify resources in Cloud environment quickly. For Example, a tag for “Environment” will help us to identify all resources in a particular environment. Different automation jobs can be run based on tag values. We can use it to get billing details segregated by tag (Use Cost Allocation Tag). Refer my Amazon Resource Billing Blog to get more details on how to use it.
In this post I will provide a small script block to tag Amazon EC2 Volumes associated with EC2 instances. For this example, we will use Environment tag and will try to use the name of EC2 Instance to which this Volume is attached. By checking the matching pattern with the EC2 Instance name, attached volume will be tagged for Environment.
This may not be a perfect example, however, it will give you the idea of how to use related resource information to tag the resource.
Let’s assume all my EC2 instances have shorthand environment name embedded in the instance name. Example, “mydvinstance001” has “dv” and that means it is a Development instance. similarly, “my-qa-instance001” or “my-prd-instance001” suggest those are QA and Production instance respectively. If the Volume is not attached to any EC2 Instance, we will set Environment tag as “NA”.
To run this script you need to install AWS Tools for Powershell and configure AWS credentials. I am assuming you have already set up your system. If you still have any issue, refer following documents by Amazon :
- https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html
- https://aws.amazon.com/powershell/
Get-EC2Volume | ForEach-Object{
$volId = $instanceId = $instanceDetails = $appliedTags = $instanceName = ""
$volId = $_.VolumeId
if($_.Attachments.count -eq 1){
$instanceId = $_.Attachments.InstanceId
}
$Tags = @()
$envTag = New-Object Amazon.EC2.Model.Tag
$envTag.Key = "Environment"
if(!([string]::IsNullOrEmpty($instanceId))){
$instanceDetails = Get-EC2Instance -InstanceId $instanceId | Select-Object Instances -ExpandProperty Instances
$appliedTags = $instanceDetails.Tags
if($appliedTags.Key -eq "Name"){
$instanceName = $appliedTags | Where-Object { $_.Key -eq "Name" } | Select-Object -expand Value
$instanceName = $instanceName.ToLower()
}
if($instanceName.Contains("dev") -or $instanceName.Contains("dv")){
$envTag.Value = "Development"
}
elseif($instanceName.Contains("qa")){
$envTag.Value = "QA"
}
elseif($instanceName.Contains("prod") -or $instanceName.Contains("prd")){
$envTag.Value = "Production"
}
else{
$envTag.Value = "NA"
}
}
else{
$envTag.Value = "NA"
}
$Tags += $envTag
New-EC2Tag -Resource $volId -Tag $Tags
}
That’s it! Once you run this script , it will set Environment tag to all EC2 Volumes associated with EC2 instances in your account. However, as I mentioned above, this automation is not full-proof.You can also use a reference file or any other source and match with it while setting the tag value.
Hope this post will help you in your tagging process!