Private/S3/Write-WorkspaceBucketTagging.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function Write-WorkspaceBucketTagging
{
<#
    .SYNOPSIS
        Write workspace bucket tags if not present

    .PARAMETER BucketName
        Bucket to tag
#>

    param
    (
        [string]$BucketName
    )

    if ((Get-S3BucketTagging -BucketName $BucketName | Measure-Object).Count -eq 0)
    {
        try
        {
            $module = (Get-Command (Get-PSCallStack | Select-Object -First 1).Command).Module
            Write-S3BucketTagging -BucketName $BucketName -TagSet @(
                @{
                    Key   = 'CreatedBy'
                    Value = $module.Name
                }
                @{
                    Key   = 'ProjectURL'
                    Value = $module.ProjectUri.ToString()
                }
                @{
                    Key   = 'Purpose'
                    Value = 'Workspace bucket for SSM Command output'
                }
            )
        }
        catch
        {
            Write-Warning "Unable to tag S3 bucket $($bucketName): $($_.Exception.Message)"
        }
    }
}