Skip to content
Menu
Tech Automation Blog
  • About Author
  • Contact
Tech Automation Blog

AWS EC2 Instance Tagging

Posted on January 17, 2020February 27, 2022

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 today’s post I will provide a small script block to tag Amazon EC2 instances. For this example, we will use Environment tag and will try to use a naming pattern to identify the tag value for Environment.

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.

Note that above assumption is not always full-proof and there are chances of miss-tagging due to the logic I am following. This is just an example to give you some idea. We can also use a source file, like a CSV File or database table to get information and automate the tagging using Powershell.

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/

I will run a for loop to run through each EC2 instance in the account and identify the name of the EC2 instance through it’s name tag. If the instance does not have a name tag, I will set the Environment tag as “NA” just for completeness of the script.

Get-EC2Instance | ForEach-Object {$_.Instances | ForEach-Object {
    $instanceId = $instanceName = $appliedTags = ""
    $Tags = @()
    $envTag = New-Object Amazon.EC2.Model.Tag
    $envTag.Key = "Environment"
    $instanceId = $_.InstanceId
    $appliedTags = $_.Tags
    if(!([string]::IsNullOrEmpty($appliedTags))){
        if($appliedTags.Key -eq "Name"){
            $instanceName =  $appliedTags | Where-Object { $_.Key -eq "Name" } | Select-Object -expand Value
            $instanceName = $instanceName.ToLower()
        }
        else{$instanceName = "noname"}    
    }
    else{
        $instanceName = "noname"
    }
    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"
    }
    $Tags += $envTag

    New-EC2Tag -ResourceId $instanceId -Tag $Tags
    }
}

That’s it! Once you run this script , it will set Environment tag to all 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!

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
June 2025
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« May    

Recent Posts

  • Monitor and alert Azure Service Health issues May 5, 2020
  • AWS IAM User access review May 3, 2020
  • Integrate Azure Security Center with Event Hub April 28, 2020
  • Add Tags to Azure Subscription April 24, 2020
  • Automate Azure billing report in Excel March 6, 2020

Categories

©2025 Tech Automation Blog | Powered by SuperbThemes