Skip to content

Instantly share code, notes, and snippets.

@PanosGreg
Last active July 18, 2025 15:52
Show Gist options
  • Save PanosGreg/264b7f67e2f4d426cdae9f0569863aa8 to your computer and use it in GitHub Desktop.
Save PanosGreg/264b7f67e2f4d426cdae9f0569863aa8 to your computer and use it in GitHub Desktop.
Parse an Amazon Resource Name (ARN) into its individual parts
function Resolve-AmazonArn {
<#
.SYNOPSIS
Parse an Amazon Resource Name (ARN) into its individual parts.
.EXAMPLE
'arn:aws:iam::111222333444:role/MyRole' | Resolve-AmazonArn
.EXAMPLE
Resolve-AmazonArn arn:aws:s3:::MyBucket/MyFolder/*
.EXAMPLE
Resolve-AmazonArn s3://MyBucket/MyFolder/MyFile.json
This will error out, because the provided string is not a valid ARN.
.NOTES
Author: Panos Grigoriadis
Date: 13-Jul-2025
Version: 1.0
#>
[CmdletBinding()]
[OutputType([psobject])] # <-- PSCustomObject with TypeName of "Amazon.ARN"
param (
[Parameter(Mandatory,ValueFromPipeline)]
[Alias('ARN')]
[string]$InputString # <-- the ARN string
)
# helper function to handle nullability for strings
function Try-NullString($string) {if ([string]::IsNullOrWhiteSpace($string)) {$null} else {$string}}
# regex pattern match for ARN strings
$Pattern = '^(?<FullName>arn:(?<Partition>[^:\n]*):(?<Service>[^:\n]*):(?<Region>[^:\n]*):(?<AccountID>[^:\n]*):(?<Ignore>(?<ResourceType>[^:\/\n]*)[:\/])?(?<Resource>.*))$'
$Regex = [System.Text.RegularExpressions.Regex]::Match($InputString,$Pattern)
# assemble the output object
if ($Regex.Success) {
$Grp = $Regex.Groups
$out = [pscustomobject]@{
PSTypeName = 'Amazon.ARN'
FullName = $Grp['FullName'].Value
Partition = $Grp['Partition'].Value
Service = Try-NullString $Grp['Service'].Value
Region = Try-NullString $Grp['Region'].Value
AccountID = Try-NullString $Grp['AccountID'].Value
ResourceType = Try-NullString $Grp['ResourceType'].Value
Resource = Try-NullString $Grp['Resource'].Value
}
}
else {
throw "Could not parse the provided ARN ($InputString)"
}
# add a method to get the AWS Region Endpoint object
$MethodValue = {
if ([string]::IsNullOrWhiteSpace($this.Region) -or $this.Region -eq '*') {$null}
elseif ('Amazon.RegionEndpoint' -as [type]) {
try {[Amazon.RegionEndpoint]::GetBySystemName($this.Region)}
catch {[Amazon.RegionEndpoint]::GetBySystemName('Unknown')}
}
else {Write-Warning 'The [Amazon.RegionEndpoint] class was not found'}
}
$out | Add-Member -MemberType ScriptMethod -Name GetRegionEndpoint -Value $MethodValue
# add a method to get the AWS Service object
$MethodValue = {
if ([string]::IsNullOrWhiteSpace($this.Service) -or $this.Service -eq '*') {$null}
elseif (Get-Command -Name Get-AWSService -EA 0) {Get-AWSService -Service $this.Service}
else {Write-Warning 'The Get-AWSService command was not found'}
}
$out | Add-Member -MemberType ScriptMethod -Name GetAwsService -Value $MethodValue
# overwrite the ToString() method
$out | Add-Member -MemberType ScriptMethod -Name ToString -Value {$this.FullName} -Force
Write-Output $out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment