10

i've been trying to use the AWS Update-LMFunctionCode to deploy my file to an existing lambda function in AWS.

Differing from the Publish-LMFunction where I can provide just a path to the zipFile (-FunctionZip), the Update-LMFunction wants a memorystream for its -Zipfile argument.

is there an example of loading a local zipfile from disk into a memory stream that works? My initial calls are getting errors that the file can't be unzipped...

$deployedFn =  Get-LMFunction -FunctionName $functionname
        "Function Exists - trying to update"
        try{
            [system.io.stream]$zipStream = [system.io.File]::OpenRead($zipFile)
        [byte[]]$filebytes = New-Object byte[] $zipStream.length
        [void] $zipStream.Read($filebytes, 0, $zipStream.Length)
            $zipStream.Close()
            "$($filebytes.length)"
        $zipString =  [System.Convert]::ToBase64String($filebytes)
        $ms = new-Object IO.MemoryStream
        $sw = new-Object IO.StreamWriter $ms
        $sw.Write($zipString)
        Update-LMFunctionCode -FunctionName $functionname -ZipFile $ms
            }
        catch{
             $ErrorMessage = $_.Exception.Message
            Write-Host $ErrorMessage
            break
        }

docs for the Powershell function is here: http://docs.aws.amazon.com/powershell/latest/reference/items/Update-LMFunctionCode.html although it wants to live in a frame...

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
Jeff Martin
  • 10,812
  • 7
  • 48
  • 74
  • (fyi, that block is in a larger try/catch that catches when the Get-LMFunction fails to find the functionname.) – Jeff Martin Aug 21 '15 at 16:37

1 Answers1

12

Try using the CopyTo method to copy from one stream to another:

try {
    $zipFilePath = "index.zip"
    $zipFileItem = Get-Item -Path $zipFilePath
    $fileStream = $zipFileItem.OpenRead()
    $memoryStream = New-Object System.IO.MemoryStream
    $fileStream.CopyTo($memoryStream)

    Update-LMFunctionCode -FunctionName "PSDeployed" -ZipFile $memoryStream
}
finally {
    $fileStream.Close()
}
James
  • 11,721
  • 2
  • 35
  • 41
  • That worked great, thanks, not sure why the AWS docs were mentioning the base64Encoding part – Jeff Martin Aug 21 '15 at 18:02
  • 4
    I think the text is copied from the underlying [Lambda API Reference for UpdateFunctionCode](http://docs.aws.amazon.com/lambda/latest/dg/API_UpdateFunctionCode.html). Of course, that doesn't explain why their PowerShell cmdlet can't just take a file path and figure out the rest :). – James Aug 21 '15 at 18:06
  • especially when the Publish does just that, thanks - if you want some credit on the AWS forums - my question is still there too: https://forums.aws.amazon.com/thread.jspa?threadID=214760&tstart=0 – Jeff Martin Aug 21 '15 at 18:14
  • @James this worked great for the path to memorystream conversion, but now I'm getting 'Update-LMFunctionCode : Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.' after about 30 seconds when trying to upload. They really just need to make it accept a path like Publish does. Any ideas on why the connection is timing out? – 3z33etm Feb 29 '16 at 19:34
  • It might be a setting problem. Did you double-check your region, permissions, S3 bucket/key, etc? – James Feb 29 '16 at 22:00