Create portable script in Powershell to list EIPs

0

Create a script List EC2 Elastic IP addresses for eu-west-1 and us-west-2 regions for a given account and assign a tag 'TBA' to all of them. The output should be a confirmation of Tag update and Public IP. I'm having difficulty in the script: Get-EC2Address -AccountId 123456789 $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "Name" $tag.Value = "TBA" Get-EC2Address -Filter @{Name="tag:Category";Values="Prod"}

I was trying different sources, but still cant get proper commands please help

AWS
asked 2 years ago305 views
1 Answer
1
Accepted Answer

The example you provided is lacking a command New-EC2Tag to add a tag to an elastic IP allocation. Additionally, you are not using the right filter to get IPs with a tag (Name:TBA). Please see the below for a sample script. You can customize it as needed.

# set regions
$regions = @("eu-west-1", "us-west-2")

# AWS PowerShell documentation https://docs.aws.amazon.com/powershell/latest/reference/
foreach ($region in $regions) {
    # https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Address.html
    # Extracting elastic IP allocation IDs
    $allocationIds = Get-EC2Address -Region $region | ForEach-Object { $_.AllocationId }

    # Prepare a Tag to Add
    $tag = New-Object Amazon.EC2.Model.Tag 
    $tag.Key = "Name" 
    $tag.Value = "TBA"
    
    # Tag all the elasticIPs
    try {
        # https://docs.aws.amazon.com/powershell/latest/reference/items/New-EC2Tag.html
        # add a new tag
        New-EC2Tag -Resource $allocationIds -Tag $tag
    } catch {
        # Failed to add a tag
        Write-Error $_.ErrorDetails
        throw
    }
    # if tagging is successful, get IPs again
    Get-EC2Address -Filter @{Name="tag:Name";Values="TBA"}
}

AWS
Taka_M
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions