Public/SDK Wrappers/Invoke-VSPackage.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
function Invoke-VSPackage {
    <#
    .SYNOPSIS
    Packages the local artifacts (local paths) that your AWS CloudFormation template references.
    
    .DESCRIPTION
    Packages the local artifacts (local paths) that your AWS CloudFormation template references. The command uploads local artifacts, such as source code for an AWS Lambda function or a Swagger file for an AWS API Gateway REST API, to an S3 bucket. The command returns a copy of your template, replacing references to local artifacts with the S3 location where the command uploaded the artifacts.
    
    .PARAMETER TemplateBody
    A JSON or YAML string containing the template body.
    
    .PARAMETER TemplateFile
    The path to the local file containing the template.
    
    .PARAMETER S3Bucket
    The name of the S3 bucket where this command uploads the artifacts that are referenced in your template.
    
    .PARAMETER S3Prefix
    A prefix name that the command adds to the artifacts' name when it uploads them to the S3 bucket. The prefix name is a path name (folder name) for the S3 bucket.
    
    .PARAMETER KMSKeyId
    The ID of an AWS KMS key that the command uses to encrypt artifacts that are at rest in the S3 bucket.
    
    .PARAMETER OutputTemplateFile
    The path to the file where the command writes the output AWS CloudFormation template. If you don't specify a path, the command writes the template to the standard output.
    
    .PARAMETER UseJson
    Indicates whether to use JSON as the format for the output AWS CloudFormation template. YAML is used by default (if cfn-flip is available).
    
    .PARAMETER Force
    Indicates whether to override existing files in the S3 bucket. Specify this flag to upload artifacts even if they match existing artifacts in the S3 bucket.
    
    .PARAMETER ProfileName
    The name of the configuration profile to deploy the stack with. Defaults to $env:AWS_PROFILE, if set.
    
    .FUNCTIONALITY
    Vaporshell
    #>

    [cmdletbinding(DefaultParameterSetName = "TemplateFile")]
    Param
    (
        [parameter(Mandatory = $true,Position = 0,ParameterSetName = "TemplateBody",ValueFromPipeline = $true)]
        [String]
        $TemplateBody,
        [parameter(Mandatory = $true,Position = 0,ParameterSetName = "TemplateFile")]
        [ValidateScript( {
                Test-Path $_
            })]
        [String]
        $TemplateFile,
        [parameter(Mandatory = $true,Position = 1)]
        [String]
        $S3Bucket,
        [parameter(Mandatory = $false)]
        [System.String]
        $S3Prefix,
        [parameter(Mandatory = $false)]
        [System.String]
        $KMSKeyId,
        [parameter(Mandatory = $false)]
        [System.String]
        $OutputTemplateFile,
        [parameter(Mandatory = $false)]
        [Switch]
        $UseJson,
        [parameter(Mandatory = $false)]
        [Alias("ForceUpload")]
        [Switch]
        $Force,
        [parameter(Mandatory = $false)]
        [String]
        $ProfileName = $env:AWS_PROFILE
    )
    Begin {
        $prof = @{}
        if ($ProfileName) {
            $prof["ProfileName"] = $ProfileName
        }
        $typeHash = @{
            "AWS::ApiGateway::RestApi"                  = "BodyS3Location"
            "AWS::Lambda::Function"                     = "Code"
            "AWS::Serverless::Function"                 = "CodeUri"
            "AWS::Serverless::Api"                      = "DefinitionUri"
            "AWS::ElasticBeanstalk::ApplicationVersion" = "SourceBundle"
            "AWS::CloudFormation::Stack"                = "TemplateUrl"
        }
        $s3Params = @{}
        if ($S3Prefix) {
            $s3Params["BucketName"] = "$($S3Bucket)/$($S3Prefix)"
            $baseUrl = "$($S3Bucket)/$($S3Prefix)"
        }
        else {
            $s3Params["BucketName"] = $S3Bucket
            $baseUrl = $($S3Bucket)
        }
        if ($KMSKeyId) {
            $s3Params["KMSKeyId"] = $KMSKeyId
        }
        try {
            Write-Verbose "Checking if bucket '$S3Bucket' exists"
            $bucketLoc = Get-VSS3BucketLocation -BucketName "$S3Bucket" @prof -Verbose:$false
            Write-Verbose "Bucket '$S3Bucket' found in $($bucketLoc.Value)"
        }
        catch {
            if ($Force) {
                Write-Verbose "Creating new bucket '$S3Bucket'"
                New-VSS3Bucket -BucketName "$S3Bucket" @prof -Verbose:$false
            }
            else {
                $PSCmdlet.ThrowTerminatingError($_)
            }
        }
    }
    Process {
        Add-Type -AssemblyName "System.IO.Compression.Filesystem"
        if ($PSCmdlet.ParameterSetName -eq "TemplateFile") {
            Write-Verbose "Getting TemplateBody from TemplateFile path"
            $templateFilePath = (Resolve-Path $TemplateFile).Path
            $tempParent = (Get-Item $templateFilePath).Directory.FullName
            $TemplateBody = [System.IO.File]::ReadAllText($templateFilePath)
        }
        $tempPSON = Import-Vaporshell -TemplateBody $TemplateBody
        $resourceIds = $tempPSON.Resources | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
        foreach ($res in $resourceIds) {
            $Resource = $tempPSON.Resources.$res
            try {
                $propName = $typeHash["$($Resource.Type)"]
                if ($propName -and $Resource.Properties.$propName) {
                    Write-Verbose "Checking '$($Resource.Type)' resource --- property '$($propName)'"
                    if (($Resource.Properties.$propName -notlike "s3://*") -and ($Resource.Properties.$propName -notlike "http*")) {
                        $found = $true
                        if (Test-Path ("$tempParent\$($Resource.Properties.$propName.TrimStart('.'))")) {
                            $filePath = (Resolve-Path "$tempParent\$($Resource.Properties.$propName.TrimStart('.'))").Path
                            Write-Verbose "File found in template directory at: $filePath"
                        }
                        elseif (Test-Path $Resource.Properties.$propName) {
                            $filePath = (Resolve-Path $Resource.Properties.$propName).Path
                            if ($filePath -like "$($pwd.Path)*") {
                                Write-Verbose "File found in current working directory at: $filePath"
                            }
                            else {
                                Write-Verbose "File found at: $filePath"
                            }
                        }
                        else {
                            $found = $false
                        }
                        if ($found) {
                            $fileInfo = Get-Item $filePath
                            if ($fileInfo.PSIsContainer) {
                                if ($S3Prefix) {
                                    $key = "$($S3Prefix)/$($fileInfo.BaseName).zip"
                                }
                                else {
                                    $key = "$($fileInfo.BaseName).zip"
                                }
                                $outFile = Join-Path $fileInfo.Parent.FullName $key
                                if (Test-Path $outFile) {
                                    Remove-Item $outFile -Force
                                }
                                [System.IO.Compression.Zipfile]::CreateFromDirectory($filePath,$outFile)
                            }
                            else {
                                if ($S3Prefix) {
                                    $key = "$($S3Prefix)/$($fileInfo.Name)"
                                }
                                else {
                                    $key = "$($fileInfo.Name)"
                                }
                                $outFile = $filePath
                            }
                            if ($Force) {
                                Write-Verbose "Uploading object!"
                                $obj = New-VSS3Object -Key $key -FilePath $outFile @s3Params @prof -Verbose:$false
                            }
                            else {
                                Write-Verbose "Checking if object exists in bucket"
                                $existsMeta = Get-VSS3ObjectMetadata -BucketName $baseUrl -Key $key -ErrorAction SilentlyContinue -Verbose:$false
                                if (!$existsMeta) {
                                    Write-Verbose "Object not found -- uploading!"
                                    $obj = New-VSS3Object -Key $key -FilePath $outFile @s3Params @prof -Verbose:$false
                                }
                                elseif ($existsMeta.ContentLength -eq (Get-Item $outFile).Length) {
                                    Write-Verbose "Object '$key' already exists in bucket and is the same size. No action apparently necessary -- If this file needs to be reuploaded, re-run this command with the Force parameter included."
                                }
                                else {
                                    Write-Verbose "Object already exists at this location and Force parameter not used. No action taken to prevent accidental overwrites. -- If this object needs to be overwritten, re-run this command with the Force parameter included."
                                }
                            }
                            $Resource.Properties.$propName = "s3://$baseUrl/$key"
                        }
                        else {
                            Write-Warning "$propName value '$($Resource.Properties.$propName)' does not appear to be an S3 URL but is also not locatable in the current working directory or the direct of the template (if provided). Please specify the full path of the local $propName to upload to the S3 bucket '$baseUrl'"
                        }
                    }
                }
                $tempPSON.Resources.$res = $Resource
            }
            catch {
                Write-Error $_
            }
        }
        $finalParams = @{}
        if ($OutputTemplateFile) {
            $finalParams["Path"] = $OutputTemplateFile
        }
        if ($UseJson) {
            $finalParams["As"] = "JSON"
        }
        else {
            $finalParams["As"] = "YAML"
        }
    Write-Verbose "Exporting resolved template"
    Export-Vaporshell -VaporshellTemplate $tempPSON @finalParams -Force
    }
}